1 | #Region "Microsoft.VisualBasic::a41eeb6f6cf5cdf8dec880c75b34bc1f, Microsoft.VisualBasic.Core\Text\Xml\XmlDoc.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 XmlDoc |
35 | ' |
36 | ' Properties: encoding, rootNode, standalone, version, xmlns |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: __rootString, CreateObject, FromObject, FromXmlFile, Save |
40 | ' SaveTo, (+2 Overloads) ToString |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Imports System.Text |
48 | Imports System.Text.RegularExpressions |
49 | Imports Microsoft.VisualBasic.ComponentModel |
50 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
51 | |
52 | Namespace Text.Xml |
53 | |
54 | ''' <summary> |
55 | ''' 请使用<see cref="XmlDoc.ToString"/>方法获取修改之后的Xml文档 |
56 | ''' </summary> |
57 | Public Class XmlDoc : Implements ISaveHandle |
58 | |
59 | Public Const XmlDeclares As String = "<\?xml.+?>" |
60 | |
61 | ReadOnly xml As String |
62 | |
63 | Public Property version As String |
64 | Public Property standalone As Boolean |
65 | Public Property encoding As XmlEncodings |
66 | |
67 | Public ReadOnly Property rootNode As String |
68 | ''' <summary> |
69 | ''' Xml namespace definitions |
70 | ''' </summary> |
71 | ''' <returns></returns> |
72 | Public Property xmlns As Xmlns |
73 | |
74 | ''' <summary> |
75 | ''' Create a xml tools from xml document text. |
76 | ''' </summary> |
77 | ''' <param name="xml"></param> |
78 | Sub New(xml As String) |
79 | Dim [declare] As New XmlDeclaration( |
80 | Regex.Match(xml, XmlDeclares, RegexICSng).Value) |
81 | version = [declare].version |
82 | standalone = [declare].standalone |
83 | encoding = [declare].encoding |
84 | |
85 | Dim root As NamedValue(Of Xmlns) = |
86 | Xmlns.RootParser(__rootString(xml)) |
87 | rootNode = root.Name |
88 | xmlns = root.Value |
89 | Me.xml = xml |
90 | End Sub |
91 | |
92 | Protected Friend Shared Function __rootString(xml As String) As String |
93 | xml = Regex.Match(xml, XmlDeclares & ".*?<.+?>", RegexICSng).Value |
94 | xml = xml.Replace(Regex.Match(xml, XmlDeclares, RegexICSng).Value, "").Trim |
95 | Return xml |
96 | End Function |
97 | |
98 | ''' <summary> |
99 | ''' 使用这个函数可以得到修改之后的Xml文档 |
100 | ''' </summary> |
101 | ''' <returns></returns> |
102 | Public Overrides Function ToString() As String |
103 | Return ToString(False) |
104 | End Function |
105 | |
106 | ''' <summary> |
107 | ''' 使用这个函数可以得到修改之后的Xml文档 |
108 | ''' </summary> |
109 | ''' <param name="usingDefault_xmlns"><see cref="xmlns.DefaultXmlns"/></param> |
110 | ''' <returns></returns> |
111 | Public Overloads Function ToString(Optional usingDefault_xmlns As Boolean = False) As String |
112 | Dim [declare] As String = Regex.Match(xml, XmlDeclares, RegexICSng).Value |
113 | Dim setDeclare As New XmlDeclaration With { |
114 | .encoding = encoding, |
115 | .standalone = standalone, |
116 | .version = version |
117 | } |
118 | |
119 | Dim doc As New StringBuilder(xml) |
120 | Call doc.Replace([declare], setDeclare.ToString) |
121 | Call xmlns.WriteNamespace(doc, usingDefault_xmlns) |
122 | Return doc.ToString |
123 | End Function |
124 | |
125 | ''' <summary> |
126 | ''' 从新修改过的xml文档之中通过反序列化构建目标对象 |
127 | ''' </summary> |
128 | ''' <typeparam name="T"></typeparam> |
129 | ''' <param name="usingDefault_xmlns"></param> |
130 | ''' <returns></returns> |
131 | Public Function CreateObject(Of T)(Optional usingDefault_xmlns As Boolean = False) As T |
132 | Return ToString(usingDefault_xmlns).LoadFromXml(Of T) |
133 | End Function |
134 | |
135 | Public Shared Function FromObject(Of T As Class)(x As T) As XmlDoc |
136 | Return New XmlDoc(x.GetXml) |
137 | End Function |
138 | |
139 | Public Shared Function FromXmlFile(path As String) As XmlDoc |
140 | Return New XmlDoc(path.ReadAllText) |
141 | End Function |
142 | |
143 | ''' <summary> |
144 | ''' Me.ToString.SaveTo(Path, encoding) |
145 | ''' </summary> |
146 | ''' <param name="Path"></param> |
147 | ''' <param name="encoding"></param> |
148 | ''' <returns></returns> |
149 | Public Function SaveTo(Optional Path As String = "", Optional encoding As Encoding = Nothing) As Boolean Implements ISaveHandle.Save |
150 | Return Me.ToString.SaveTo(Path, encoding) |
151 | End Function |
152 | |
153 | Public Function Save(Optional Path As String = "", Optional encoding As Encodings = Encodings.UTF8) As Boolean Implements ISaveHandle.Save |
154 | Return SaveTo(Path, encoding.CodePage) |
155 | End Function |
156 | End Class |
157 | End Namespace |