XmlConstructs.cs 20 KB

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