wasSharp – Blame information for rev 27

Subversion Repositories:
Rev:
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,
26 office 24 @"(" + string.Join(@"|", @"&amp;", @"&lt;", @"&gt;", @"&quot;", @"&apos;") + @")",
7 office 25 @"", RegexOptions.IgnoreCase | RegexOptions.Multiline)
27 office 26 .IndexOfAny(new[] { '&', '<', '>', '"', '\'' })
7 office 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;
27 office 53  
4 zed 54 case ';':
55 if (!t.Count.Equals(0))
56 {
57 t.Enqueue(c);
7 office 58 var special = string.Join("", t.ToArray());
4 zed 59 switch (special)
60 {
61 case "&apos;":
62 m.Append('\'');
63 break;
27 office 64  
4 zed 65 case "&quot;":
66 m.Append('"');
67 break;
27 office 68  
4 zed 69 case "&gt;":
70 m.Append('>');
71 break;
27 office 72  
4 zed 73 case "&lt;":
74 m.Append('<');
75 break;
27 office 76  
4 zed 77 case "&amp;":
78 m.Append('&');
79 break;
27 office 80  
4 zed 81 default: // Unrecognized escape sequence
82 m.Append(special);
83 break;
84 }
85 t.Clear();
86 break;
87 }
88 m.Append(c);
89 break;
27 office 90  
4 zed 91 default:
92 if (!t.Count.Equals(0))
93 {
94 t.Enqueue(c);
95 if (t.Count >= 6)
96 {
97 m.Append(string.Join("", t.ToArray()));
98 t.Clear();
99 }
100 break;
101 }
102 m.Append(c);
103 break;
104 }
105 }
106 return m.ToString();
107 }
108  
109 ///////////////////////////////////////////////////////////////////////////
110 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
111 ///////////////////////////////////////////////////////////////////////////
112 /// <summary>
113 /// Escapes a string to be used in XML.
114 /// </summary>
115 /// <param name="s">the string to escape</param>
116 /// <returns>an XML escaped string</returns>
117 public static string EscapeXML(string s)
118 {
119 if (string.IsNullOrEmpty(s)) return s;
120  
7 office 121 var result = new string[s.Length];
4 zed 122 Parallel.ForEach(Enumerable.Range(0, s.Length), o =>
123 {
124 switch (s[o])
125 {
126 case '&':
127 result[o] = @"&amp;";
128 break;
27 office 129  
4 zed 130 case '<':
131 result[o] = @"&lt;";
132 break;
27 office 133  
4 zed 134 case '>':
135 result[o] = @"&gt;";
136 break;
27 office 137  
4 zed 138 case '"':
139 result[o] = @"&quot;";
140 break;
27 office 141  
4 zed 142 case '\'':
143 result[o] = @"&apos;";
144 break;
27 office 145  
4 zed 146 default:
147 result[o] = s[o].ToString();
148 break;
149 }
150 });
151 return string.Join("", result);
152 }
7 office 153  
154 ///////////////////////////////////////////////////////////////////////////
155 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
156 ///////////////////////////////////////////////////////////////////////////
157 /// <summary>
158 /// Determines whether a string is safe to use in XML
159 /// </summary>
160 /// <param name="data">the string to check</param>
161 /// <returns>true in case the string is safe</returns>
162 public static bool IsSafeXML(string data)
163 {
164 return directIsSafeXML(data);
165 }
166  
167 ///////////////////////////////////////////////////////////////////////////
168 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
169 ///////////////////////////////////////////////////////////////////////////
170 /// <summary>
171 /// Recursively rename a node by name.
172 /// </summary>
173 /// <param name="root">the root from where to start</param>
174 /// <param name="name">the name to replace</param>
175 /// <param name="rename">the name to replace with</param>
176 public static void RenameNodes(XElement root, string name, string rename)
177 {
27 office 178 if (string.Equals(root.Name.LocalName, name, StringComparison.Ordinal))
7 office 179 {
180 root.Name = rename;
181 }
182  
183 foreach (var xElement in root.Elements())
184 {
185 RenameNodes(xElement, name, rename);
186 }
187 }
4 zed 188 }
27 office 189 }