1 | #Region "Microsoft.VisualBasic::deb0fe73b458011a0eee5f2b51528cd5, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\Serialization.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 GenericINIProfile |
35 | ' |
36 | ' Properties: Sections |
37 | ' |
38 | ' Function: __getDefaultPath, Load, Save |
39 | ' |
40 | ' Class ClassName |
41 | ' |
42 | ' Properties: Name |
43 | ' |
44 | ' Constructor: (+1 Overloads) Sub New |
45 | ' Function: ToString |
46 | ' |
47 | ' Class IniMapIO |
48 | ' |
49 | ' Properties: Path |
50 | ' |
51 | ' Constructor: (+1 Overloads) Sub New |
52 | ' Function: ToString |
53 | ' |
54 | ' |
55 | ' /********************************************************************************/ |
56 | |
57 | #End Region |
58 | |
59 | Imports System.Runtime.CompilerServices |
60 | Imports System.Text |
61 | Imports System.Xml.Serialization |
62 | Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem |
63 | Imports Microsoft.VisualBasic.Serialization.JSON |
64 | |
65 | Namespace ComponentModel.Settings.Inf |
66 | |
67 | ''' <summary> |
68 | ''' 通用的配置文件模型 |
69 | ''' </summary> |
70 | Public Class GenericINIProfile : Inherits ITextFile |
71 | |
72 | <XmlElement> Public Property Sections As Section() |
73 | |
74 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
75 | Public Shared Function Load(path As String) As GenericINIProfile |
76 | Return New GenericINIProfile With { |
77 | .FilePath = path, |
78 | .Sections = INIProfile _ |
79 | .PopulateSections(path) _ |
80 | .ToArray |
81 | } |
82 | End Function |
83 | |
84 | Public Overrides Function Save(Optional FilePath As String = "", Optional Encoding As Encoding = Nothing) As Boolean |
85 | Return Sections _ |
86 | .Select(Function(sec) sec.CreateDocFragment) _ |
87 | .SaveTo(getPath(FilePath), Encoding) |
88 | End Function |
89 | |
90 | Protected Overrides Function __getDefaultPath() As String |
91 | Return FilePath |
92 | End Function |
93 | End Class |
94 | |
95 | ''' <summary> |
96 | ''' 定义在Ini配置文件之中的Section的名称 |
97 | ''' </summary> |
98 | <AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> |
99 | Public Class ClassName : Inherits Attribute |
100 | |
101 | Public ReadOnly Property Name As String |
102 | |
103 | ''' <summary> |
104 | ''' Defines the section name in the ini profile data.(定义在Ini配置文件之中的Section的名称) |
105 | ''' </summary> |
106 | ''' <param name="name"></param> |
107 | Sub New(name As String) |
108 | Me.Name = name |
109 | End Sub |
110 | |
111 | Public Overrides Function ToString() As String |
112 | Return Name |
113 | End Function |
114 | End Class |
115 | |
116 | ''' <summary> |
117 | ''' The path parameter can be shortcut by method <see cref="PathMapper.GetMapPath"/>. |
118 | ''' additional, using ``@fileName`` for using <see cref="App.GetFile(String)"/> API. |
119 | ''' </summary> |
120 | <AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> |
121 | Public Class IniMapIO : Inherits Attribute |
122 | |
123 | Public ReadOnly Property Path As String |
124 | |
125 | ''' <summary> |
126 | ''' The path parameter can be shortcut by method <see cref="PathMapper.GetMapPath"/> |
127 | ''' </summary> |
128 | ''' <param name="path"></param> |
129 | Sub New(path As String) |
130 | If path.First = "@"c Then |
131 | Me.Path = App.GetFile(Mid(path, 2)) |
132 | Else |
133 | Me.Path = PathMapper.GetMapPath(path) |
134 | End If |
135 | End Sub |
136 | |
137 | Public Overrides Function ToString() As String |
138 | Return Me.GetJson |
139 | End Function |
140 | End Class |
141 | |
142 | End Namespace |