XmlConstructs.cs 19 KB

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