annotate Xml/XmlTools.cs @ 43:2baf3f949cfb

* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe) * Automated cleanups no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Oct 2009 09:47:16 +0000
parents 7e74c7954be9
children 6e46a62ad9b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
23 /// Gets the value of an attribute of an element as a boolean. Throws a FormatException if the attribute is not a boolean.
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
24 /// </summary>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
25 /// <param name="elem">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
26 /// The <see cref="XmlElement"/> to get the attribute value of
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
27 /// </param>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
28 /// <param name="attributeName">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
29 /// The name of the attribute to get as a boolean
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
30 /// </param>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
31 /// <returns>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
32 /// The value of the attribute as an boolean
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
33 /// </returns>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
34 public static bool GetBoolValueFromAttribute(XmlElement elem, string attributeName)
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
35 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
36 try
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
37 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
38 return bool.Parse(elem.GetAttribute(attributeName));
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
39 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
40 catch(FormatException)
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
41 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
42 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id")));
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
43 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
44 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
45
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
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 }
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
105
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
106 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
107 {
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
108 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
109 {
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
110 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
111 }
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
112
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
113 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
114 }
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
115
40
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
116 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
117 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
118 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
119 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
120 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
121 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
122
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
123 return idRegex;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
124 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
125
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
126 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
127 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
128 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
129 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
130 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
131 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
132
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
133 return multiUnderscoreRegex;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
134 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
135
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
136 /// <summary>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
137 /// 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
138 /// 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
139 /// 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
140 /// </summary>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
141 /// <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
142 /// 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
143 /// </param>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
144 /// <returns>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
145 /// 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
146 /// </returns>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
147 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
148 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
149 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
150 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
151
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
152 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
153 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
154 id = "_" + id;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
155 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
156
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
157 return id;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
158 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
159
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
160 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
161 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
162 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
163
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
164 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
165 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
166 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
167 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
168 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
169
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
170 return valid;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
171 }
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
172 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
173 }