XmlConstructs.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. using System;
  22. namespace System.Xml
  23. {
  24. /// <summary>
  25. /// http://www.w3.org/TR/REC-xml
  26. /// </summary>
  27. /// <remarks>
  28. /// Includes code and Ideas from org.apache.xerces.util.XMLChar class of Xerces 2.0.1
  29. /// However, No surrogate support is included in this class.
  30. /// This class is currently public. Make it internal after testing completes
  31. /// </remarks>
  32. internal class XmlChar//XmlConstructs
  33. {
  34. internal static readonly char [] WhitespaceChars = {' ', '\n', '\t', '\r'};
  35. /** Character flags. */
  36. internal static readonly byte [] CHARS = new byte [1 << 16];
  37. /** Valid character mask. */
  38. internal const int VALID = 0x01;
  39. /** Space character mask. */
  40. internal const int SPACE = 0x02;
  41. /** Name start character mask. */
  42. internal const int NAME_START = 0x04;
  43. /** Name character mask. */
  44. internal const int NAME = 0x08;
  45. /** Pubid character mask. */
  46. internal const int PUBID = 0x10;
  47. /**
  48. * Content character mask. Special characters are those that can
  49. * be considered the start of markup, such as '&lt;' and '&amp;'.
  50. * The various newline characters are considered special as well.
  51. * All other valid XML characters can be considered content.
  52. * <p>
  53. * This is an optimization for the inner loop of character scanning.
  54. */
  55. internal const int CONTENT = 0x20;
  56. /** NCName start character mask. */
  57. internal const int NCNAME_START = 0x40;
  58. /** NCName character mask. */
  59. internal const int NCNAME = 0x80;
  60. static XmlChar()
  61. {
  62. //
  63. // [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] |
  64. // [#xE000-#xFFFD] | [#x10000-#x10FFFF]
  65. //
  66. int[] charRange = {
  67. 0x0009, 0x000A, 0x000D, 0x000D, 0x0020, 0xD7FF, 0xE000, 0xFFFD,
  68. };
  69. //
  70. // [3] S ::= (#x20 | #x9 | #xD | #xA)+
  71. //
  72. int[] spaceChar = {
  73. 0x0020, 0x0009, 0x000D, 0x000A,
  74. };
  75. //
  76. // [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
  77. // CombiningChar | Extender
  78. //
  79. int[] nameChar = {
  80. 0x002D, 0x002E, // '-' and '.'
  81. };
  82. //
  83. // [5] Name ::= (Letter | '_' | ':') (NameChar)*
  84. //
  85. int[] nameStartChar = {
  86. 0x003A, 0x005F, // ':' and '_'
  87. };
  88. //
  89. // [13] PubidChar ::= #x20 | 0xD | 0xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
  90. //
  91. int[] pubidChar = {
  92. 0x000A, 0x000D, 0x0020, 0x0021, 0x0023, 0x0024, 0x0025, 0x003D,
  93. 0x005F
  94. };
  95. int[] pubidRange = {
  96. 0x0027, 0x003B, 0x003F, 0x005A, 0x0061, 0x007A
  97. };
  98. //
  99. // [84] Letter ::= BaseChar | Ideographic
  100. //
  101. int[] letterRange = {
  102. // BaseChar
  103. 0x0041, 0x005A, 0x0061, 0x007A, 0x00C0, 0x00D6, 0x00D8, 0x00F6,
  104. 0x00F8, 0x0131, 0x0134, 0x013E, 0x0141, 0x0148, 0x014A, 0x017E,
  105. 0x0180, 0x01C3, 0x01CD, 0x01F0, 0x01F4, 0x01F5, 0x01FA, 0x0217,
  106. 0x0250, 0x02A8, 0x02BB, 0x02C1, 0x0388, 0x038A, 0x038E, 0x03A1,
  107. 0x03A3, 0x03CE, 0x03D0, 0x03D6, 0x03E2, 0x03F3, 0x0401, 0x040C,
  108. 0x040E, 0x044F, 0x0451, 0x045C, 0x045E, 0x0481, 0x0490, 0x04C4,
  109. 0x04C7, 0x04C8, 0x04CB, 0x04CC, 0x04D0, 0x04EB, 0x04EE, 0x04F5,
  110. 0x04F8, 0x04F9, 0x0531, 0x0556, 0x0561, 0x0586, 0x05D0, 0x05EA,
  111. 0x05F0, 0x05F2, 0x0621, 0x063A, 0x0641, 0x064A, 0x0671, 0x06B7,
  112. 0x06BA, 0x06BE, 0x06C0, 0x06CE, 0x06D0, 0x06D3, 0x06E5, 0x06E6,
  113. 0x0905, 0x0939, 0x0958, 0x0961, 0x0985, 0x098C, 0x098F, 0x0990,
  114. 0x0993, 0x09A8, 0x09AA, 0x09B0, 0x09B6, 0x09B9, 0x09DC, 0x09DD,
  115. 0x09DF, 0x09E1, 0x09F0, 0x09F1, 0x0A05, 0x0A0A, 0x0A0F, 0x0A10,
  116. 0x0A13, 0x0A28, 0x0A2A, 0x0A30, 0x0A32, 0x0A33, 0x0A35, 0x0A36,
  117. 0x0A38, 0x0A39, 0x0A59, 0x0A5C, 0x0A72, 0x0A74, 0x0A85, 0x0A8B,
  118. 0x0A8F, 0x0A91, 0x0A93, 0x0AA8, 0x0AAA, 0x0AB0, 0x0AB2, 0x0AB3,
  119. 0x0AB5, 0x0AB9, 0x0B05, 0x0B0C, 0x0B0F, 0x0B10, 0x0B13, 0x0B28,
  120. 0x0B2A, 0x0B30, 0x0B32, 0x0B33, 0x0B36, 0x0B39, 0x0B5C, 0x0B5D,
  121. 0x0B5F, 0x0B61, 0x0B85, 0x0B8A, 0x0B8E, 0x0B90, 0x0B92, 0x0B95,
  122. 0x0B99, 0x0B9A, 0x0B9E, 0x0B9F, 0x0BA3, 0x0BA4, 0x0BA8, 0x0BAA,
  123. 0x0BAE, 0x0BB5, 0x0BB7, 0x0BB9, 0x0C05, 0x0C0C, 0x0C0E, 0x0C10,
  124. 0x0C12, 0x0C28, 0x0C2A, 0x0C33, 0x0C35, 0x0C39, 0x0C60, 0x0C61,
  125. 0x0C85, 0x0C8C, 0x0C8E, 0x0C90, 0x0C92, 0x0CA8, 0x0CAA, 0x0CB3,
  126. 0x0CB5, 0x0CB9, 0x0CE0, 0x0CE1, 0x0D05, 0x0D0C, 0x0D0E, 0x0D10,
  127. 0x0D12, 0x0D28, 0x0D2A, 0x0D39, 0x0D60, 0x0D61, 0x0E01, 0x0E2E,
  128. 0x0E32, 0x0E33, 0x0E40, 0x0E45, 0x0E81, 0x0E82, 0x0E87, 0x0E88,
  129. 0x0E94, 0x0E97, 0x0E99, 0x0E9F, 0x0EA1, 0x0EA3, 0x0EAA, 0x0EAB,
  130. 0x0EAD, 0x0EAE, 0x0EB2, 0x0EB3, 0x0EC0, 0x0EC4, 0x0F40, 0x0F47,
  131. 0x0F49, 0x0F69, 0x10A0, 0x10C5, 0x10D0, 0x10F6, 0x1102, 0x1103,
  132. 0x1105, 0x1107, 0x110B, 0x110C, 0x110E, 0x1112, 0x1154, 0x1155,
  133. 0x115F, 0x1161, 0x116D, 0x116E, 0x1172, 0x1173, 0x11AE, 0x11AF,
  134. 0x11B7, 0x11B8, 0x11BC, 0x11C2, 0x1E00, 0x1E9B, 0x1EA0, 0x1EF9,
  135. 0x1F00, 0x1F15, 0x1F18, 0x1F1D, 0x1F20, 0x1F45, 0x1F48, 0x1F4D,
  136. 0x1F50, 0x1F57, 0x1F5F, 0x1F7D, 0x1F80, 0x1FB4, 0x1FB6, 0x1FBC,
  137. 0x1FC2, 0x1FC4, 0x1FC6, 0x1FCC, 0x1FD0, 0x1FD3, 0x1FD6, 0x1FDB,
  138. 0x1FE0, 0x1FEC, 0x1FF2, 0x1FF4, 0x1FF6, 0x1FFC, 0x212A, 0x212B,
  139. 0x2180, 0x2182, 0x3041, 0x3094, 0x30A1, 0x30FA, 0x3105, 0x312C,
  140. 0xAC00, 0xD7A3,
  141. // Ideographic
  142. 0x3021, 0x3029, 0x4E00, 0x9FA5,
  143. };
  144. int[] letterChar = {
  145. // BaseChar
  146. 0x0386, 0x038C, 0x03DA, 0x03DC, 0x03DE, 0x03E0, 0x0559, 0x06D5,
  147. 0x093D, 0x09B2, 0x0A5E, 0x0A8D, 0x0ABD, 0x0AE0, 0x0B3D, 0x0B9C,
  148. 0x0CDE, 0x0E30, 0x0E84, 0x0E8A, 0x0E8D, 0x0EA5, 0x0EA7, 0x0EB0,
  149. 0x0EBD, 0x1100, 0x1109, 0x113C, 0x113E, 0x1140, 0x114C, 0x114E,
  150. 0x1150, 0x1159, 0x1163, 0x1165, 0x1167, 0x1169, 0x1175, 0x119E,
  151. 0x11A8, 0x11AB, 0x11BA, 0x11EB, 0x11F0, 0x11F9, 0x1F59, 0x1F5B,
  152. 0x1F5D, 0x1FBE, 0x2126, 0x212E,
  153. // Ideographic
  154. 0x3007,
  155. };
  156. //
  157. // [87] CombiningChar ::= ...
  158. //
  159. int[] combiningCharRange = {
  160. 0x0300, 0x0345, 0x0360, 0x0361, 0x0483, 0x0486, 0x0591, 0x05A1,
  161. 0x05A3, 0x05B9, 0x05BB, 0x05BD, 0x05C1, 0x05C2, 0x064B, 0x0652,
  162. 0x06D6, 0x06DC, 0x06DD, 0x06DF, 0x06E0, 0x06E4, 0x06E7, 0x06E8,
  163. 0x06EA, 0x06ED, 0x0901, 0x0903, 0x093E, 0x094C, 0x0951, 0x0954,
  164. 0x0962, 0x0963, 0x0981, 0x0983, 0x09C0, 0x09C4, 0x09C7, 0x09C8,
  165. 0x09CB, 0x09CD, 0x09E2, 0x09E3, 0x0A40, 0x0A42, 0x0A47, 0x0A48,
  166. 0x0A4B, 0x0A4D, 0x0A70, 0x0A71, 0x0A81, 0x0A83, 0x0ABE, 0x0AC5,
  167. 0x0AC7, 0x0AC9, 0x0ACB, 0x0ACD, 0x0B01, 0x0B03, 0x0B3E, 0x0B43,
  168. 0x0B47, 0x0B48, 0x0B4B, 0x0B4D, 0x0B56, 0x0B57, 0x0B82, 0x0B83,
  169. 0x0BBE, 0x0BC2, 0x0BC6, 0x0BC8, 0x0BCA, 0x0BCD, 0x0C01, 0x0C03,
  170. 0x0C3E, 0x0C44, 0x0C46, 0x0C48, 0x0C4A, 0x0C4D, 0x0C55, 0x0C56,
  171. 0x0C82, 0x0C83, 0x0CBE, 0x0CC4, 0x0CC6, 0x0CC8, 0x0CCA, 0x0CCD,
  172. 0x0CD5, 0x0CD6, 0x0D02, 0x0D03, 0x0D3E, 0x0D43, 0x0D46, 0x0D48,
  173. 0x0D4A, 0x0D4D, 0x0E34, 0x0E3A, 0x0E47, 0x0E4E, 0x0EB4, 0x0EB9,
  174. 0x0EBB, 0x0EBC, 0x0EC8, 0x0ECD, 0x0F18, 0x0F19, 0x0F71, 0x0F84,
  175. 0x0F86, 0x0F8B, 0x0F90, 0x0F95, 0x0F99, 0x0FAD, 0x0FB1, 0x0FB7,
  176. 0x20D0, 0x20DC, 0x302A, 0x302F,
  177. };
  178. int[] combiningCharChar = {
  179. 0x05BF, 0x05C4, 0x0670, 0x093C, 0x094D, 0x09BC, 0x09BE, 0x09BF,
  180. 0x09D7, 0x0A02, 0x0A3C, 0x0A3E, 0x0A3F, 0x0ABC, 0x0B3C, 0x0BD7,
  181. 0x0D57, 0x0E31, 0x0EB1, 0x0F35, 0x0F37, 0x0F39, 0x0F3E, 0x0F3F,
  182. 0x0F97, 0x0FB9, 0x20E1, 0x3099, 0x309A,
  183. };
  184. //
  185. // [88] Digit ::= ...
  186. //
  187. int[] digitRange = {
  188. 0x0030, 0x0039, 0x0660, 0x0669, 0x06F0, 0x06F9, 0x0966, 0x096F,
  189. 0x09E6, 0x09EF, 0x0A66, 0x0A6F, 0x0AE6, 0x0AEF, 0x0B66, 0x0B6F,
  190. 0x0BE7, 0x0BEF, 0x0C66, 0x0C6F, 0x0CE6, 0x0CEF, 0x0D66, 0x0D6F,
  191. 0x0E50, 0x0E59, 0x0ED0, 0x0ED9, 0x0F20, 0x0F29,
  192. };
  193. //
  194. // [89] Extender ::= ...
  195. //
  196. int[] extenderRange = {
  197. 0x3031, 0x3035, 0x309D, 0x309E, 0x30FC, 0x30FE,
  198. };
  199. int[] extenderChar = {
  200. 0x00B7, 0x02D0, 0x02D1, 0x0387, 0x0640, 0x0E46, 0x0EC6, 0x3005,
  201. };
  202. //
  203. // SpecialChar ::= '<', '&', '\n', '\r', ']'
  204. //
  205. int[] specialChar = {
  206. '<', '&', '\n', '\r', ']',
  207. };
  208. //
  209. // Initialize
  210. //
  211. // set valid characters
  212. for (int i = 0; i < charRange.Length; i += 2)
  213. {
  214. for (int j = charRange[i]; j <= charRange[i + 1]; j++)
  215. {
  216. CHARS[j] = (byte)(CHARS[j] | VALID | CONTENT);
  217. }
  218. }
  219. // remove special characters
  220. for (int i = 0; i < specialChar.Length; i++)
  221. {
  222. CHARS[specialChar[i]] = (byte)(CHARS[specialChar[i]] & ~CONTENT);
  223. }
  224. // set space characters
  225. for (int i = 0; i < spaceChar.Length; i++)
  226. {
  227. CHARS[spaceChar[i]] = (byte)(CHARS[spaceChar[i]] | SPACE);
  228. }
  229. // set name start characters
  230. for (int i = 0; i < nameStartChar.Length; i++)
  231. {
  232. CHARS[nameStartChar[i]] = (byte)(CHARS[nameStartChar[i]] | NAME_START | NAME |
  233. NCNAME_START | NCNAME);
  234. }
  235. for (int i = 0; i < letterRange.Length; i += 2)
  236. {
  237. for (int j = letterRange[i]; j <= letterRange[i + 1]; j++)
  238. {
  239. CHARS[j] = (byte)(CHARS[j] | NAME_START | NAME | NCNAME_START | NCNAME);
  240. }
  241. }
  242. for (int i = 0; i < letterChar.Length; i++)
  243. {
  244. CHARS[letterChar[i]] = (byte)(CHARS[letterChar[i]] | NAME_START | NAME |
  245. NCNAME_START | NCNAME);
  246. }
  247. // set name characters
  248. for (int i = 0; i < nameChar.Length; i++)
  249. {
  250. CHARS[nameChar[i]] = (byte)(CHARS[nameChar[i]] | NAME | NCNAME);
  251. }
  252. for (int i = 0; i < digitRange.Length; i += 2)
  253. {
  254. for (int j = digitRange[i]; j <= digitRange[i + 1]; j++)
  255. {
  256. CHARS[j] = (byte)(CHARS[j] | NAME | NCNAME);
  257. }
  258. }
  259. for (int i = 0; i < combiningCharRange.Length; i += 2)
  260. {
  261. for (int j = combiningCharRange[i]; j <= combiningCharRange[i + 1]; j++)
  262. {
  263. CHARS[j] = (byte)(CHARS[j] | NAME | NCNAME);
  264. }
  265. }
  266. for (int i = 0; i < combiningCharChar.Length; i++)
  267. {
  268. CHARS[combiningCharChar[i]] = (byte)(CHARS[combiningCharChar[i]] | NAME | NCNAME);
  269. }
  270. for (int i = 0; i < extenderRange.Length; i += 2)
  271. {
  272. for (int j = extenderRange[i]; j <= extenderRange[i + 1]; j++)
  273. {
  274. CHARS[j] = (byte)(CHARS[j] | NAME | NCNAME);
  275. }
  276. }
  277. for (int i = 0; i < extenderChar.Length; i++)
  278. {
  279. CHARS[extenderChar[i]] = (byte)(CHARS[extenderChar[i]] | NAME | NCNAME);
  280. }
  281. // remove ':' from allowable NCNAME_START and NCNAME chars
  282. CHARS[':'] = (byte)(CHARS[':'] & ~(NCNAME_START | NCNAME));
  283. // set Pubid characters
  284. for (int i = 0; i < pubidChar.Length; i++)
  285. {
  286. CHARS[pubidChar[i]] = (byte)(CHARS[pubidChar[i]] | PUBID);
  287. }
  288. for (int i = 0; i < pubidRange.Length; i += 2)
  289. {
  290. for (int j = pubidRange[i]; j <= pubidRange[i + 1]; j++)
  291. {
  292. CHARS[j] = (byte)(CHARS[j] | PUBID);
  293. }
  294. }
  295. }
  296. private XmlChar()
  297. {
  298. }
  299. //Static Methods
  300. /// <summary>
  301. /// Returns true if the specified character is valid.
  302. /// </summary>
  303. /// <param name="c">The character to check.</param>
  304. public static bool IsValid(char c)
  305. {
  306. return c > 0 && ((CHARS[c] & VALID) != 0);
  307. }
  308. public static bool IsValid (int c)
  309. {
  310. if (c > 0xffff)
  311. return c < 0x110000;
  312. return c > 0 && ((CHARS[c] & VALID) != 0);
  313. }
  314. /// <summary>
  315. /// Returns true if the specified character is invalid.
  316. /// </summary>
  317. /// <param name="c">The character to check.</param>
  318. public static bool IsInvalid(char c)
  319. {
  320. return !IsValid(c);
  321. }
  322. public static bool IsInvalid(int c)
  323. {
  324. return !IsValid(c);
  325. }
  326. /// <summary>
  327. /// Returns true if the specified character can be considered content.
  328. /// </summary>
  329. /// <param name="c">The character to check.</param>
  330. public static bool IsContent(char c)
  331. {
  332. return (CHARS[c] & CONTENT) != 0;
  333. }
  334. public static bool IsContent(int c)
  335. {
  336. return c > 0 && c < CHARS.Length && (CHARS[c] & CONTENT) != 0;
  337. }
  338. /// <summary>
  339. /// Returns true if the specified character can be considered markup.
  340. /// Markup characters include '&lt;', '&amp;', and '%'.
  341. /// </summary>
  342. /// <param name="c">The character to check.</param>
  343. public static bool IsMarkup(char c)
  344. {
  345. return c == '<' || c == '&' || c == '%';
  346. }
  347. public static bool IsMarkup(int c)
  348. {
  349. return c > 0 && c < CHARS.Length && (c == '<' || c == '&' || c == '%');
  350. }
  351. /// <summary>
  352. /// Returns true if the specified character is a space character
  353. /// as defined by production [3] in the XML 1.0 specification.
  354. /// </summary>
  355. /// <param name="c">The character to check.</param>
  356. /// <returns></returns>
  357. public static bool IsWhitespace (char c)
  358. {
  359. return (CHARS[c] & SPACE) != 0;
  360. }
  361. public static bool IsWhitespace (int c)
  362. {
  363. return c > 0 && c < CHARS.Length && (CHARS[c] & SPACE) != 0;
  364. }
  365. /// <summary>
  366. /// Returns true if the specified character is a valid name start
  367. /// character as defined by production [5] in the XML 1.0 specification.
  368. /// </summary>
  369. /// <param name="c">The character to check.</param>
  370. public static bool IsFirstNameChar (char c)
  371. {
  372. return (CHARS[c] & NAME_START) != 0;
  373. }
  374. public static bool IsFirstNameChar (int c)
  375. {
  376. return c > 0 && c < CHARS.Length && (CHARS[c] & NAME_START) != 0;
  377. }
  378. /// <summary>
  379. /// Returns true if the specified character is a valid name
  380. /// character as defined by production [4] in the XML 1.0 specification.
  381. /// </summary>
  382. /// <param name="c">The character to check.</param>
  383. public static bool IsNameChar(char c)
  384. {
  385. return (CHARS[c] & NAME) != 0;
  386. }
  387. public static bool IsNameChar(int c)
  388. {
  389. return c > 0 && c < CHARS.Length && (CHARS[c] & NAME) != 0;
  390. }
  391. /// <summary>
  392. /// Returns true if the specified character is a valid NCName start
  393. /// character as defined by production [4] in Namespaces in XML
  394. /// recommendation.
  395. /// </summary>
  396. /// <param name="c">The character to check.</param>
  397. /// <returns></returns>
  398. public static bool IsNCNameStart(char c)
  399. {
  400. return (CHARS[c] & NCNAME_START) != 0;
  401. }
  402. public static bool IsNCNameStart(int c)
  403. {
  404. return c > 0 && c < CHARS.Length && (CHARS[c] & NCNAME_START) != 0;
  405. }
  406. /// <summary>
  407. /// Returns true if the specified character is a valid NCName
  408. /// character as defined by production [5] in Namespaces in XML
  409. /// recommendation.
  410. /// </summary>
  411. /// <param name="c"></param>
  412. /// <returns></returns>
  413. public static bool IsNCNameChar(char c)
  414. {
  415. return (CHARS[c] & NCNAME) != 0;
  416. }
  417. public static bool IsNCNameChar(int c)
  418. {
  419. return c > 0 && c < CHARS.Length && (CHARS[c] & NCNAME) != 0;
  420. }
  421. /// <summary>
  422. /// Returns true if the specified character is a valid Pubid
  423. /// character as defined by production [13] in the XML 1.0 specification.
  424. /// </summary>
  425. /// <param name="c">The character to check</param>
  426. public static bool IsPubidChar (char c)
  427. {
  428. return (CHARS[c] & PUBID) != 0;
  429. }
  430. public static bool IsPubidChar (int c)
  431. {
  432. return c > 0 && c < CHARS.Length && (CHARS[c] & PUBID) != 0;
  433. }
  434. /// <summary>
  435. /// Check to see if a string is a valid Name according to [5]
  436. /// in the XML 1.0 Recommendation
  437. /// </summary>
  438. /// <param name="name">The string to check</param>
  439. public static bool IsValidName(String name, out Exception err)
  440. {
  441. err = null;
  442. if (name.Length == 0)
  443. {
  444. err = new XmlException("Name can not be an empty string",null);
  445. return false;
  446. }
  447. char ch = name[0];
  448. if( IsFirstNameChar (ch) == false)
  449. {
  450. err = new XmlException("The character '"+ch+"' cannot start a Name",null);
  451. return false;
  452. }
  453. for (int i = 1; i < name.Length; i++ )
  454. {
  455. ch = name[i];
  456. if( IsNameChar (ch) == false )
  457. {
  458. err = new XmlException("The character '"+ch+"' is not allowed in a Name",null);
  459. return false;
  460. }
  461. }
  462. return true;
  463. }
  464. public static int IsValidName (string name)
  465. {
  466. if (name.Length == 0)
  467. return 0;
  468. if (!IsFirstNameChar (name [0]))
  469. return 0;
  470. for (int i=1; i<name.Length; i++)
  471. if (!IsNameChar (name [i]))
  472. return i;
  473. return -1;
  474. }
  475. /// <summary>
  476. /// Check to see if a string is a valid NCName according to [4]
  477. /// from the XML Namespaces 1.0 Recommendation
  478. /// </summary>
  479. /// <param name="ncName">The string to check</param>
  480. public static bool IsValidNCName(String ncName, out Exception err)
  481. {
  482. err = null;
  483. if (ncName.Length == 0)
  484. {
  485. err = new XmlException("NCName can not be an empty string",null);
  486. return false;
  487. }
  488. char ch = ncName[0];
  489. if( IsNCNameStart(ch) == false)
  490. {
  491. err = new XmlException("The character '"+ch+"' cannot start a NCName",null);
  492. return false;
  493. }
  494. for (int i = 1; i < ncName.Length; i++ )
  495. {
  496. ch = ncName[i];
  497. if( IsNCNameChar (ch) == false )
  498. {
  499. err = new XmlException("The character '"+ch+"' is not allowed in a NCName",null);
  500. return false;
  501. }
  502. }
  503. return true;
  504. }
  505. /// <summary>
  506. /// Check to see if a string is a valid Nmtoken according to [7]
  507. /// in the XML 1.0 Recommendation
  508. /// </summary>
  509. /// <param name="nmtoken">The string to check.</param>
  510. public static bool IsValidNmtoken(String nmtoken, out Exception err)
  511. {
  512. err = null;
  513. if (nmtoken.Length == 0)
  514. {
  515. err = new XmlException("NMTOKEN can not be an empty string", null);
  516. return false;
  517. }
  518. for (int i = 0; i < nmtoken.Length; i++ )
  519. {
  520. char ch = nmtoken[i];
  521. if( ! IsNameChar (ch) )
  522. {
  523. err = new XmlException("The character '"+ch+"' is not allowed in a NMTOKEN",null);
  524. return false;
  525. }
  526. }
  527. return true;
  528. }
  529. // encodings
  530. /// <summary>
  531. /// Returns true if the encoding name is a valid IANA encoding.
  532. /// This method does not verify that there is a decoder available
  533. /// for this encoding, only that the characters are valid for an
  534. /// IANA encoding name.
  535. /// </summary>
  536. /// <param name="ianaEncoding">The encoding to check.</param>
  537. /// <returns></returns>
  538. public static bool IsValidIANAEncoding(String ianaEncoding)
  539. {
  540. if (ianaEncoding != null)
  541. {
  542. int length = ianaEncoding.Length;
  543. if (length > 0)
  544. {
  545. char c = ianaEncoding[0];
  546. if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  547. {
  548. for (int i = 1; i < length; i++)
  549. {
  550. c = ianaEncoding[i];
  551. if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') &&
  552. (c < '0' || c > '9') && c != '.' && c != '_' &&
  553. c != '-')
  554. {
  555. return false;
  556. }
  557. }
  558. return true;
  559. }
  560. }
  561. }
  562. return false;
  563. }
  564. public static bool IsName (string str)
  565. {
  566. if (str.Length == 0)
  567. return false;
  568. if (!IsFirstNameChar (str [0]))
  569. return false;
  570. for (int i = 1; i < str.Length; i++)
  571. if (!IsNameChar (str [i]))
  572. return false;
  573. return true;
  574. }
  575. public static bool IsNCName (string str)
  576. {
  577. if (str.Length == 0)
  578. return false;
  579. if (!IsFirstNameChar (str [0]))
  580. return false;
  581. for (int i = 0; i < str.Length; i++)
  582. if (!IsNCNameChar (str [i]))
  583. return false;
  584. return true;
  585. }
  586. public static bool IsNmToken (string str)
  587. {
  588. if (str.Length == 0)
  589. return false;
  590. for (int i = 0; i < str.Length; i++)
  591. if (!IsNameChar (str [i]))
  592. return false;
  593. return true;
  594. }
  595. public static bool IsWhitespace (string str)
  596. {
  597. for (int i = 0; i < str.Length; i++)
  598. if (!IsWhitespace (str [i])) return false;
  599. return true;
  600. }
  601. public static int GetPredefinedEntity (string name)
  602. {
  603. switch (name) {
  604. case "amp":
  605. return '&';
  606. case "lt":
  607. return '<';
  608. case "gt":
  609. return '>';
  610. case "quot":
  611. return '"';
  612. case "apos":
  613. return '\'';
  614. default:
  615. return -1;
  616. }
  617. }
  618. }
  619. }