| 1 | #Region "Microsoft.VisualBasic::d6278acd072863b4988b7c39be91bc1f, Microsoft.VisualBasic.Core\Text\Xml\XmlDeclaration.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 | ' Enum XmlEncodings |
| 35 | ' |
| 36 | ' GB2312, UTF16, UTF8 |
| 37 | ' |
| 38 | ' |
| 39 | ' |
| 40 | ' |
| 41 | ' |
| 42 | ' Structure XmlDeclaration |
| 43 | ' |
| 44 | ' Properties: [Default] |
| 45 | ' |
| 46 | ' Constructor: (+1 Overloads) Sub New |
| 47 | ' Function: EncodingParser, ToString, XmlEncodingString, XmlStandaloneString |
| 48 | ' |
| 49 | ' |
| 50 | ' /********************************************************************************/ |
| 51 | |
| 52 | #End Region |
| 53 | |
| 54 | Imports System.Text.RegularExpressions |
| 55 | |
| 56 | Namespace Text.Xml |
| 57 | |
| 58 | Public Enum XmlEncodings |
| 59 | UTF8 |
| 60 | UTF16 |
| 61 | GB2312 |
| 62 | End Enum |
| 63 | |
| 64 | Public Structure XmlDeclaration |
| 65 | |
| 66 | Public version As String |
| 67 | Public standalone As Boolean |
| 68 | Public encoding As XmlEncodings |
| 69 | |
| 70 | Sub New(declares As String) |
| 71 | Dim s As String |
| 72 | |
| 73 | s = Regex.Match(declares, "encoding=""\S+""", RegexICSng).Value |
| 74 | encoding = EncodingParser(s.GetStackValue("""", """")) |
| 75 | s = Regex.Match(declares, "standalone=""\S+""", RegexICSng).Value |
| 76 | standalone = s.GetStackValue("""", """").ParseBoolean |
| 77 | s = Regex.Match(declares, "version=""\S+""", RegexICSng).Value |
| 78 | version = s.GetStackValue("""", """") |
| 79 | End Sub |
| 80 | |
| 81 | Public Shared ReadOnly Property [Default] As XmlDeclaration = |
| 82 | New XmlDeclaration With { |
| 83 | .version = "1.0", |
| 84 | .standalone = True, |
| 85 | .encoding = XmlEncodings.UTF16 |
| 86 | } |
| 87 | |
| 88 | ''' <summary> |
| 89 | ''' <?xml version="{<see cref="version"/>}" encoding="{<see cref="encoding"/>}" standalone="{<see cref="standalone"/>}"?> |
| 90 | ''' </summary> |
| 91 | ''' <returns></returns> |
| 92 | Public Overrides Function ToString() As String |
| 93 | If String.IsNullOrEmpty(version) Then |
| 94 | version = "1.0" |
| 95 | End If |
| 96 | Return $"<?xml version=""{version}"" encoding=""{XmlEncodingString(encoding)}"" standalone=""{XmlStandaloneString(standalone)}""?>" |
| 97 | End Function |
| 98 | |
| 99 | Public Shared Function XmlEncodingString(enc As XmlEncodings) As String |
| 100 | Return If(enc = XmlEncodings.UTF8, "utf-8", "utf-16") |
| 101 | End Function |
| 102 | |
| 103 | Public Shared Function EncodingParser(enc As String) As XmlEncodings |
| 104 | If String.Equals(enc, "utf-8", StringComparison.OrdinalIgnoreCase) Then |
| 105 | Return XmlEncodings.UTF8 |
| 106 | Else |
| 107 | Return XmlEncodings.UTF16 |
| 108 | End If |
| 109 | End Function |
| 110 | |
| 111 | Public Shared Function XmlStandaloneString(standalone As Boolean) As String |
| 112 | Return If(standalone, "yes", "no") |
| 113 | End Function |
| 114 | End Structure |
| 115 | End Namespace |