| 1 | #Region "Microsoft.VisualBasic::1122da036990ffe63fc903bb9611ee8c, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\Section.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class Section |
| 35 | ' |
| 36 | ' Properties: Items, Name |
| 37 | ' |
| 38 | ' Function: CreateDocFragment, GetValue, ToString |
| 39 | ' |
| 40 | ' Sub: SetValue |
| 41 | ' |
| 42 | ' |
| 43 | ' /********************************************************************************/ |
| 44 | |
| 45 | #End Region |
| 46 | |
| 47 | Imports System.Text |
| 48 | Imports System.Xml.Serialization |
| 49 | Imports Microsoft.VisualBasic.ComponentModel.Collection |
| 50 | Imports Microsoft.VisualBasic.Language |
| 51 | Imports Microsoft.VisualBasic.Serialization.JSON |
| 52 | Imports HashValue = Microsoft.VisualBasic.Text.Xml.Models.NamedValue |
| 53 | |
| 54 | Namespace ComponentModel.Settings.Inf |
| 55 | |
| 56 | ''' <summary> |
| 57 | ''' 一个配置数据区域的抽象模型 |
| 58 | ''' </summary> |
| 59 | Public Class Section |
| 60 | |
| 61 | ''' <summary> |
| 62 | ''' 区域的名称 |
| 63 | ''' </summary> |
| 64 | ''' <returns></returns> |
| 65 | <XmlAttribute> Public Property Name As String |
| 66 | |
| 67 | <XmlElement> |
| 68 | Public Property Items As HashValue() |
| 69 | Get |
| 70 | Return _internalTable.Values.ToArray |
| 71 | End Get |
| 72 | Set(value As HashValue()) |
| 73 | If value Is Nothing Then |
| 74 | value = New HashValue() {} |
| 75 | End If |
| 76 | |
| 77 | _internalTable = value.ToDictionary(Function(x) x.name.ToLower) |
| 78 | End Set |
| 79 | End Property |
| 80 | |
| 81 | ''' <summary> |
| 82 | ''' 这个字典之中的所有键名称都是小写形式的 |
| 83 | ''' </summary> |
| 84 | Dim _internalTable As Dictionary(Of HashValue) |
| 85 | |
| 86 | Public Function GetValue(Key As String) As String |
| 87 | With Key.ToLower |
| 88 | If _internalTable.ContainsKey(.ByRef) Then |
| 89 | Return _internalTable(.ByRef).text |
| 90 | Else |
| 91 | Return "" |
| 92 | End If |
| 93 | End With |
| 94 | End Function |
| 95 | |
| 96 | ''' <summary> |
| 97 | ''' 不存在则自动添加 |
| 98 | ''' </summary> |
| 99 | ''' <param name="Name"></param> |
| 100 | ''' <param name="value"></param> |
| 101 | Public Sub SetValue(Name As String, value As String) |
| 102 | Dim KeyFind As String = Name.ToLower |
| 103 | |
| 104 | If _internalTable.ContainsKey(KeyFind) Then |
| 105 | Call _internalTable.Remove(KeyFind) |
| 106 | End If |
| 107 | |
| 108 | Call _internalTable.Add(KeyFind, New HashValue(Name, value)) |
| 109 | End Sub |
| 110 | |
| 111 | Public Function CreateDocFragment() As String |
| 112 | Dim sb As New StringBuilder($"[{Name}]") |
| 113 | |
| 114 | For Each item As HashValue In _internalTable.Values |
| 115 | Call sb.AppendLine($"{item.name}={item.text}") |
| 116 | Next |
| 117 | |
| 118 | Return sb.ToString |
| 119 | End Function |
| 120 | |
| 121 | Public Overrides Function ToString() As String |
| 122 | Return $"[{Name}] with {_internalTable.Keys.ToArray.GetJson()}" |
| 123 | End Function |
| 124 | End Class |
| 125 | End Namespace |