Mercurial > repos > IBBoard
annotate Xml/XmlTools.cs @ 81:09f71d10c249
* Fix documentation - we're now using ISO codes (or should be, although we do handle invalid ones)
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 18 Apr 2010 14:09:16 +0000 |
parents | 8fe11cd7d3bf |
children | 7ca4acc659bb de0ed24eb961 |
rev | line source |
---|---|
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (XmlTools.cs) is a part of the IBBoard library and is copyright 2009 IBBoard |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 // |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System; |
43
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
7 using System.Globalization; |
40
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
8 using System.Text.RegularExpressions; |
25
148edabc9c73
Re #18 - Migrate XML methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
9 using System.Xml; |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 namespace IBBoard.Xml |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 /// Some basic tools for handling XML files and retrieving their values |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 public class XmlTools |
40
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
17 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
18 private static Regex idRegex; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
19 private static Regex multiUnderscoreRegex; |
43
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
20 private static NumberFormatInfo doubleFormat; |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
21 |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 /// <summary> |
27 | 23 /// Gets the value of an attribute of an element as a boolean. Throws a FormatException if the attribute is not a boolean. |
24 /// </summary> | |
25 /// <param name="elem"> | |
26 /// The <see cref="XmlElement"/> to get the attribute value of | |
27 /// </param> | |
28 /// <param name="attributeName"> | |
29 /// The name of the attribute to get as a boolean | |
30 /// </param> | |
31 /// <returns> | |
32 /// The value of the attribute as an boolean | |
33 /// </returns> | |
34 public static bool GetBoolValueFromAttribute(XmlElement elem, string attributeName) | |
35 { | |
36 try | |
37 { | |
38 return bool.Parse(elem.GetAttribute(attributeName)); | |
39 } | |
40 catch(FormatException) | |
41 { | |
42 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id"))); | |
43 } | |
44 } | |
45 | |
46 /// <summary> | |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 /// Gets the value of an attribute of an element as an integer. Throws a FormatException if the attribute is not an integer. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 /// The <see cref="XmlElement"/> to get the attribute value of |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 /// The name of the attribute to get as an integer |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 /// The value of the attribute as an integer |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 /// </returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 public static int GetIntValueFromAttribute(XmlElement elem, string attributeName) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 try |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 return int.Parse(elem.GetAttribute(attributeName)); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 /// Gets the value of an attribute of an element as a double. Throws a FormatException if the attribute is not a double. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 /// The <see cref="XmlElement"/> to get the attribute value of |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 /// The name of the attribute to get as a double |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 /// The value of the attribute as an double |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 /// </returns> |
26
14f3daf48ba5
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
25
diff
changeset
|
82 public static double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 double doubleVal = double.NaN; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 string attribValue = elem.GetAttribute(attributeName); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 if (attribValue == "INF") |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 doubleVal = double.PositiveInfinity; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 else |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
92 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
93 try |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
94 { |
43
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
95 return double.Parse(attribValue, GetNumberFormatInfo()); |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
96 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
97 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
98 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
99 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 return doubleVal; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
104 } |
62
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
105 |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
106 public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName) |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
107 { |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
108 return GetEnumValueFromAttribute<T>(elem, attributeName, true); |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
109 } |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
110 |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
111 public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName, bool ignoreCase) |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
112 { |
63 | 113 string attribValue = elem.GetAttribute (attributeName); |
114 | |
62
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
115 try |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
116 { |
63 | 117 return EnumTools.ParseEnum<T>(attribValue, ignoreCase); |
62
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
118 } |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
119 catch(ArgumentException) |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
120 { |
63 | 121 throw new FormatException(String.Format("Attribute '{0}' with value {1} for {2} with ID '{3}' was not a valid {4} enum", attributeName, attribValue, elem.Name, elem.GetAttribute("id"), typeof(T).Name)); |
62
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
122 } |
6e46a62ad9b8
Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
123 } |
43
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
124 |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
125 private static NumberFormatInfo GetNumberFormatInfo() |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
126 { |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
127 if (doubleFormat == null) |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
128 { |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
129 doubleFormat = NumberFormatInfo.InvariantInfo; |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
130 } |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
131 |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
132 return doubleFormat; |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
133 } |
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
134 |
40
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
135 private static Regex GetIdRegex() |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
136 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
137 if (idRegex == null) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
138 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
139 idRegex = new Regex("[^a-zA-Z0-9:\\._-]+"); |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
140 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
141 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
142 return idRegex; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
143 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
144 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
145 private static Regex GetMultiUnderscoreRegex() |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
146 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
147 if (multiUnderscoreRegex == null) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
148 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
149 multiUnderscoreRegex = new Regex("_{2,}"); |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
150 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
151 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
152 return multiUnderscoreRegex; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
153 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
154 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
155 /// <summary> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
156 /// Gets a valid XML ID for a given string that does not contain accented and non-ASCII characters. Matches the allowed characters |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
157 /// in the XML spec (http://www.w3.org/TR/xml/#NT-NameStartChar) where the characters do not use Unicode character codes. If the ID |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
158 /// starts with an invalid character then it will be prepended with an underscore. |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
159 /// </summary> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
160 /// <param name="str"> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
161 /// The <see cref="System.String"/> to turn in to a valid ID |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
162 /// </param> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
163 /// <returns> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
164 /// The valid XML ID with all series of invalid characters replaced with an underscore |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
165 /// </returns> |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
166 public static string GetAsciiXmlIdForString(string str) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
167 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
168 string id = GetIdRegex().Replace(str, "_"); |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
169 id = GetMultiUnderscoreRegex().Replace(id, "_"); |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
170 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
171 if (!IdStartsWithValidCharacter(id)) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
172 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
173 id = "_" + id; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
174 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
175 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
176 return id; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
177 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
178 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
179 private static bool IdStartsWithValidCharacter(string id) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
180 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
181 bool valid = false; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
182 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
183 if (id.Length > 0) |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
184 { |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
185 char firstChar = id[0]; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
186 valid = ('A' <= firstChar && firstChar <= 'Z') || ('a' <= firstChar && firstChar <= 'z') || firstChar == '_' || firstChar == ':'; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
187 } |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
188 |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
189 return valid; |
c71855e241fc
* Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents:
27
diff
changeset
|
190 } |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
191 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
192 } |