annotate Xml/XmlTools.cs @ 121:9131bc46903e default tip

* Add NamedStream wrapper class to support warfoundry:#419
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Nov 2012 20:21:40 +0000
parents 07660ac09a5f
children
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 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
6 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
7 using System.Text.RegularExpressions;
25
148edabc9c73 Re #18 - Migrate XML methods to core utils
IBBoard <dev@ibboard.co.uk>
parents: 24
diff changeset
8 using System.Xml;
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
9 using System.Xml.Schema;
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
10 using System.Reflection;
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
11 using System.IO;
24
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 namespace IBBoard.Xml
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
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 /// 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
17 /// </summary>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 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
19 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
20 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
21 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
22 private static NumberFormatInfo doubleFormat;
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
23
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 /// <summary>
27
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
25 /// 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
26 /// </summary>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
27 /// <param name="elem">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
28 /// 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
29 /// </param>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
30 /// <param name="attributeName">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
31 /// 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
32 /// </param>
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 /// The value of the attribute as an boolean
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
35 /// </returns>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
36 public static bool GetBoolValueFromAttribute(XmlElement elem, string attributeName)
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 try
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 return bool.Parse(elem.GetAttribute(attributeName));
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
41 }
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
42 catch (FormatException)
27
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 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
45 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
46 }
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
47
27
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
48 /// <summary>
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 /// 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
50 /// </summary>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 /// <param name="elem">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 /// 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
53 /// </param>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 /// <param name="attributeName">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 /// 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
56 /// </param>
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 /// 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
59 /// </returns>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 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
61 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 try
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 return int.Parse(elem.GetAttribute(attributeName));
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 }
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
66 catch (FormatException)
24
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 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
69 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71
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 /// 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
74 /// </summary>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 /// <param name="elem">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 /// 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
77 /// </param>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 /// <param name="attributeName">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 /// 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
80 /// </param>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 /// <returns>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 /// 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
83 /// </returns>
26
14f3daf48ba5 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents: 25
diff changeset
84 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
85 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 double doubleVal = double.NaN;
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 string attribValue = elem.GetAttribute(attributeName);
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 if (attribValue == "INF")
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 doubleVal = double.PositiveInfinity;
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 else
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 try
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 {
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
97 return double.Parse(attribValue, GetNumberFormatInfo());
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
98 }
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
99 catch (FormatException)
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
100 {
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
102 }
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105 return doubleVal;
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106 }
40
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
107
62
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
108 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
109 {
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
110 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
111 }
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
112
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
113 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
114 {
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
115 string attribValue = elem.GetAttribute(attributeName);
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
116
62
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
117 try
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
118 {
63
8fe11cd7d3bf Re #25: Enum parsing
IBBoard <dev@ibboard.co.uk>
parents: 62
diff changeset
119 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
120 }
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
121 catch (ArgumentException)
62
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
122 {
63
8fe11cd7d3bf Re #25: Enum parsing
IBBoard <dev@ibboard.co.uk>
parents: 62
diff changeset
123 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
124 }
6e46a62ad9b8 Re #25: Add method for parsing enums from XML elements
IBBoard <dev@ibboard.co.uk>
parents: 43
diff changeset
125 }
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
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 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
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 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
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 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
132 }
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 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
135 }
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
136
40
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
137 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
138 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
139 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
140 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
141 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
142 }
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 return idRegex;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
145 }
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 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
148 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
149 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
150 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
151 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
152 }
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 return multiUnderscoreRegex;
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 /// <summary>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
158 /// 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
159 /// 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
160 /// 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
161 /// </summary>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
162 /// <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
163 /// 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
164 /// </param>
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 /// 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
167 /// </returns>
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
168 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
169 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
170 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
171 id = GetMultiUnderscoreRegex().Replace(id, "_");
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
172
40
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
173 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
174 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
175 id = "_" + id;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
176 }
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 return id;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
179 }
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 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
182 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
183 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
184
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
185 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
186 {
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
187 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
188 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
189 }
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
190
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
191 return valid;
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
192 }
114
7ca4acc659bb * Add resource resolver for use with DTDs as resources
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
193
115
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
194 public static void AddSchemaToSetFromResource(XmlSchemaSet schemaSet, string targetNamespace, Assembly assm, string id)
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
195 {
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
196 Stream resStream = assm.GetManifestResourceStream(id);
de0ed24eb961 * Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents: 63
diff changeset
197 schemaSet.Add(targetNamespace, new XmlTextReader(resStream));
40
c71855e241fc * Add method to clean up a string as a valid XML ID
IBBoard <dev@ibboard.co.uk>
parents: 27
diff changeset
198 }
24
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
199 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
200 }