microsoft-visualbasic-runtime/ComponentModel/Settings/Inf/Section.vb

182 lines
5.7 KiB
VB.net
Raw Normal View History

2019-03-10 18:10:46 +08:00
#Region "Microsoft.VisualBasic::164c15886ef11e6d91df8200793a24fd, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\Section.vb"
2018-12-23 00:49:32 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class Section
'
2019-02-19 18:25:51 +08:00
' Properties: Comment, Items, Name
2018-12-23 00:49:32 +08:00
'
2019-02-19 18:25:51 +08:00
' Function: CreateDocFragment, GetValue, Have, ToString
2018-12-23 00:49:32 +08:00
'
2019-02-19 18:25:51 +08:00
' Sub: appendComments, Delete, SetComments, SetValue
2018-12-23 00:49:32 +08:00
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
2019-02-19 18:25:51 +08:00
Imports System.Runtime.CompilerServices
2018-12-23 00:42:21 +08:00
Imports System.Text
2018-08-02 20:14:48 +08:00
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic.ComponentModel.Collection
2018-12-23 00:18:36 +08:00
Imports Microsoft.VisualBasic.Language
2019-02-19 18:25:51 +08:00
Imports Microsoft.VisualBasic.Language.Default
2018-12-23 00:42:21 +08:00
Imports Microsoft.VisualBasic.Serialization.JSON
2019-02-19 18:25:51 +08:00
Imports HashValue = Microsoft.VisualBasic.Text.Xml.Models.Property
2018-08-02 20:14:48 +08:00
Namespace ComponentModel.Settings.Inf
2018-12-23 00:42:21 +08:00
''' <summary>
''' 一个配置数据区域的抽象模型
''' </summary>
2018-08-02 20:14:48 +08:00
Public Class Section
2018-12-23 00:42:21 +08:00
''' <summary>
''' 区域的名称
''' </summary>
''' <returns></returns>
2018-08-02 20:14:48 +08:00
<XmlAttribute> Public Property Name As String
2018-12-23 00:18:36 +08:00
2019-02-19 18:25:51 +08:00
<XmlText>
Public Property Comment As String
Shared ReadOnly emptyList As New DefaultValue(Of HashValue())(Function() {}, isLazy:=False)
2018-12-23 00:18:36 +08:00
<XmlElement>
2018-12-23 00:42:21 +08:00
Public Property Items As HashValue()
2018-08-02 20:14:48 +08:00
Get
2019-02-19 18:25:51 +08:00
Return configTable.Values.ToArray
2018-08-02 20:14:48 +08:00
End Get
2018-12-23 00:42:21 +08:00
Set(value As HashValue())
2019-02-19 18:25:51 +08:00
configTable = (value Or emptyList).ToDictionary(Function(x) x.name.ToLower)
2018-08-02 20:14:48 +08:00
End Set
End Property
2018-12-23 00:18:36 +08:00
''' <summary>
''' 这个字典之中的所有键名称都是小写形式的
''' </summary>
2019-02-19 18:25:51 +08:00
Dim configTable As Dictionary(Of HashValue)
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function Have(key As String) As Boolean
Return configTable.ContainsKey(key.ToLower)
End Function
2018-08-02 20:14:48 +08:00
Public Function GetValue(Key As String) As String
2018-12-23 00:18:36 +08:00
With Key.ToLower
2019-02-19 18:25:51 +08:00
If configTable.ContainsKey(.ByRef) Then
Return configTable(.ByRef).value
2018-12-23 00:18:36 +08:00
Else
Return ""
End If
End With
2018-08-02 20:14:48 +08:00
End Function
2019-02-19 18:25:51 +08:00
Public Sub Delete(name As String)
With name.ToLower
If configTable.ContainsKey(.ByRef) Then
Call configTable.Remove(.ByRef)
End If
End With
End Sub
2018-08-02 20:14:48 +08:00
''' <summary>
''' 不存在则自动添加
''' </summary>
''' <param name="Name"></param>
''' <param name="value"></param>
2019-02-19 18:25:51 +08:00
Public Sub SetValue(Name$, value$, Optional comment$ = Nothing)
2018-08-02 20:14:48 +08:00
Dim KeyFind As String = Name.ToLower
2019-02-19 18:25:51 +08:00
If configTable.ContainsKey(KeyFind) Then
Call configTable.Remove(KeyFind)
2018-08-02 20:14:48 +08:00
End If
2019-02-19 18:25:51 +08:00
configTable(KeyFind) = New HashValue With {
.name = Name,
.Comment = comment,
.value = value
}
2018-08-02 20:14:48 +08:00
End Sub
2018-12-23 00:42:21 +08:00
2019-02-19 18:25:51 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Sub SetComments(name$, comments$)
SetValue(name, GetValue(name), comments)
End Sub
''' <summary>
''' 利用这个函数所生成的文档片段的格式如下所示
'''
''' ```
''' [name]
''' # comment region of
''' # this section
'''
''' ; comment of this key
''' key=value
''' ; comment of this key
''' key=value
''' ```
''' </summary>
''' <returns></returns>
2018-12-23 00:42:21 +08:00
Public Function CreateDocFragment() As String
Dim sb As New StringBuilder($"[{Name}]")
2019-02-19 18:25:51 +08:00
Call sb.AppendLine()
Call appendComments(sb, Comment, "#")
If Not Comment.StringEmpty Then
Call sb.AppendLine()
End If
For Each item As HashValue In configTable.Values
Call appendComments(sb, item.Comment, ";")
Call sb.AppendLine($"{item.name}={item.value}")
2018-12-23 00:42:21 +08:00
Next
Return sb.ToString
End Function
2019-02-19 18:25:51 +08:00
Private Shared Sub appendComments(sb As StringBuilder, comments$, symbol$)
If Not comments.StringEmpty Then
For Each line As String In comments.LineTokens
Call sb.AppendLine(symbol & " " & line)
Next
End If
End Sub
2018-12-23 00:42:21 +08:00
Public Overrides Function ToString() As String
2019-02-19 18:25:51 +08:00
Return $"[{Name}] with {configTable.Keys.ToArray.GetJson()}"
2018-12-23 00:42:21 +08:00
End Function
2018-08-02 20:14:48 +08:00
End Class
End Namespace