wasSharp – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
4 | zed | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | office | 7 | using System; |
4 | zed | 8 | using System.Collections.Generic; |
9 | using System.Linq; |
||
7 | office | 10 | using System.Linq.Expressions; |
4 | zed | 11 | using System.Text; |
7 | office | 12 | using System.Text.RegularExpressions; |
4 | zed | 13 | using System.Threading.Tasks; |
7 | office | 14 | using System.Xml.Linq; |
4 | zed | 15 | |
16 | namespace wasSharp |
||
17 | { |
||
7 | office | 18 | public static class XML |
4 | zed | 19 | { |
7 | office | 20 | private static readonly Func<string, bool> directIsSafeXML = |
21 | ((Expression<Func<string, bool>>) |
||
22 | (data => |
||
23 | Regex.Replace(data, |
||
24 | @"(" + string.Join("|", @"&", @"<", @">", @""", @"'") + @")", |
||
25 | @"", RegexOptions.IgnoreCase | RegexOptions.Multiline) |
||
26 | .IndexOfAny(new[] {'&', '<', '>', '"', '\''}) |
||
27 | .Equals(-1))).Compile(); |
||
28 | |||
4 | zed | 29 | /////////////////////////////////////////////////////////////////////////// |
30 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
31 | /////////////////////////////////////////////////////////////////////////// |
||
32 | /// <summary> |
||
33 | /// Unescapes a string used in XML. |
||
34 | /// </summary> |
||
35 | /// <param name="s">the string to unescape</param> |
||
36 | /// <returns>an XML unescaped string</returns> |
||
37 | public static string UnescapeXML(string s) |
||
38 | { |
||
7 | office | 39 | var t = new Queue<char>(); |
40 | var m = new StringBuilder(); |
||
41 | foreach (var c in s) |
||
4 | zed | 42 | { |
43 | switch (c) |
||
44 | { |
||
45 | case '&': |
||
46 | if (!t.Count.Equals(0)) |
||
47 | { |
||
48 | m.Append(string.Join("", t.ToArray())); |
||
49 | t.Clear(); |
||
50 | } |
||
51 | t.Enqueue(c); |
||
52 | break; |
||
53 | case ';': |
||
54 | if (!t.Count.Equals(0)) |
||
55 | { |
||
56 | t.Enqueue(c); |
||
7 | office | 57 | var special = string.Join("", t.ToArray()); |
4 | zed | 58 | switch (special) |
59 | { |
||
60 | case "'": |
||
61 | m.Append('\''); |
||
62 | break; |
||
63 | case """: |
||
64 | m.Append('"'); |
||
65 | break; |
||
66 | case ">": |
||
67 | m.Append('>'); |
||
68 | break; |
||
69 | case "<": |
||
70 | m.Append('<'); |
||
71 | break; |
||
72 | case "&": |
||
73 | m.Append('&'); |
||
74 | break; |
||
75 | default: // Unrecognized escape sequence |
||
76 | m.Append(special); |
||
77 | break; |
||
78 | } |
||
79 | t.Clear(); |
||
80 | break; |
||
81 | } |
||
82 | m.Append(c); |
||
83 | break; |
||
84 | default: |
||
85 | if (!t.Count.Equals(0)) |
||
86 | { |
||
87 | t.Enqueue(c); |
||
88 | if (t.Count >= 6) |
||
89 | { |
||
90 | m.Append(string.Join("", t.ToArray())); |
||
91 | t.Clear(); |
||
92 | } |
||
93 | break; |
||
94 | } |
||
95 | m.Append(c); |
||
96 | break; |
||
97 | } |
||
98 | } |
||
99 | return m.ToString(); |
||
100 | } |
||
101 | |||
102 | /////////////////////////////////////////////////////////////////////////// |
||
103 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
104 | /////////////////////////////////////////////////////////////////////////// |
||
105 | /// <summary> |
||
106 | /// Escapes a string to be used in XML. |
||
107 | /// </summary> |
||
108 | /// <param name="s">the string to escape</param> |
||
109 | /// <returns>an XML escaped string</returns> |
||
110 | public static string EscapeXML(string s) |
||
111 | { |
||
112 | if (string.IsNullOrEmpty(s)) return s; |
||
113 | |||
7 | office | 114 | var result = new string[s.Length]; |
4 | zed | 115 | Parallel.ForEach(Enumerable.Range(0, s.Length), o => |
116 | { |
||
117 | switch (s[o]) |
||
118 | { |
||
119 | case '&': |
||
120 | result[o] = @"&"; |
||
121 | break; |
||
122 | case '<': |
||
123 | result[o] = @"<"; |
||
124 | break; |
||
125 | case '>': |
||
126 | result[o] = @">"; |
||
127 | break; |
||
128 | case '"': |
||
129 | result[o] = @"""; |
||
130 | break; |
||
131 | case '\'': |
||
132 | result[o] = @"'"; |
||
133 | break; |
||
134 | default: |
||
135 | result[o] = s[o].ToString(); |
||
136 | break; |
||
137 | } |
||
138 | }); |
||
139 | return string.Join("", result); |
||
140 | } |
||
7 | office | 141 | |
142 | /////////////////////////////////////////////////////////////////////////// |
||
143 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
144 | /////////////////////////////////////////////////////////////////////////// |
||
145 | /// <summary> |
||
146 | /// Determines whether a string is safe to use in XML |
||
147 | /// </summary> |
||
148 | /// <param name="data">the string to check</param> |
||
149 | /// <returns>true in case the string is safe</returns> |
||
150 | public static bool IsSafeXML(string data) |
||
151 | { |
||
152 | return directIsSafeXML(data); |
||
153 | } |
||
154 | |||
155 | /////////////////////////////////////////////////////////////////////////// |
||
156 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
157 | /////////////////////////////////////////////////////////////////////////// |
||
158 | /// <summary> |
||
159 | /// Recursively rename a node by name. |
||
160 | /// </summary> |
||
161 | /// <param name="root">the root from where to start</param> |
||
162 | /// <param name="name">the name to replace</param> |
||
163 | /// <param name="rename">the name to replace with</param> |
||
164 | public static void RenameNodes(XElement root, string name, string rename) |
||
165 | { |
||
166 | if (Strings.StringEquals(root.Name.LocalName, name, StringComparison.Ordinal)) |
||
167 | { |
||
168 | root.Name = rename; |
||
169 | } |
||
170 | |||
171 | foreach (var xElement in root.Elements()) |
||
172 | { |
||
173 | RenameNodes(xElement, name, rename); |
||
174 | } |
||
175 | } |
||
4 | zed | 176 | } |
177 | } |