XMLIterator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace FairyGUI.Utils
  6. {
  7. public enum XMLTagType
  8. {
  9. Start,
  10. End,
  11. Void,
  12. CDATA,
  13. Comment,
  14. Instruction
  15. }
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public class XMLIterator
  20. {
  21. public static string tagName;
  22. public static XMLTagType tagType;
  23. public static string lastTagName;
  24. static string source;
  25. static int sourceLen;
  26. static int parsePos;
  27. static int tagPos;
  28. static int tagLength;
  29. static int lastTagEnd;
  30. static bool attrParsed;
  31. static bool lowerCaseName;
  32. static StringBuilder buffer = new StringBuilder();
  33. static Dictionary<string, string> attributes = new Dictionary<string, string>();
  34. const string CDATA_START = "<![CDATA[";
  35. const string CDATA_END = "]]>";
  36. const string COMMENT_START = "<!--";
  37. const string COMMENT_END = "-->";
  38. public static void Begin(string source, bool lowerCaseName = false)
  39. {
  40. XMLIterator.source = source;
  41. XMLIterator.lowerCaseName = lowerCaseName;
  42. sourceLen = source.Length;
  43. parsePos = 0;
  44. lastTagEnd = 0;
  45. tagPos = 0;
  46. tagLength = 0;
  47. tagName = null;
  48. }
  49. public static bool NextTag()
  50. {
  51. int pos;
  52. char c;
  53. tagType = XMLTagType.Start;
  54. buffer.Length = 0;
  55. lastTagEnd = parsePos;
  56. attrParsed = false;
  57. lastTagName = tagName;
  58. while ((pos = source.IndexOf('<', parsePos)) != -1)
  59. {
  60. parsePos = pos;
  61. pos++;
  62. if (pos == sourceLen)
  63. break;
  64. c = source[pos];
  65. if (c == '!')
  66. {
  67. if (sourceLen > pos + 7 && source.Substring(pos - 1, 9) == CDATA_START)
  68. {
  69. pos = source.IndexOf(CDATA_END, pos);
  70. tagType = XMLTagType.CDATA;
  71. tagName = string.Empty;
  72. tagPos = parsePos;
  73. if (pos == -1)
  74. tagLength = sourceLen - parsePos;
  75. else
  76. tagLength = pos + 3 - parsePos;
  77. parsePos += tagLength;
  78. return true;
  79. }
  80. else if (sourceLen > pos + 2 && source.Substring(pos - 1, 4) == COMMENT_START)
  81. {
  82. pos = source.IndexOf(COMMENT_END, pos);
  83. tagType = XMLTagType.Comment;
  84. tagName = string.Empty;
  85. tagPos = parsePos;
  86. if (pos == -1)
  87. tagLength = sourceLen - parsePos;
  88. else
  89. tagLength = pos + 3 - parsePos;
  90. parsePos += tagLength;
  91. return true;
  92. }
  93. else
  94. {
  95. pos++;
  96. tagType = XMLTagType.Instruction;
  97. }
  98. }
  99. else if (c == '/')
  100. {
  101. pos++;
  102. tagType = XMLTagType.End;
  103. }
  104. else if (c == '?')
  105. {
  106. pos++;
  107. tagType = XMLTagType.Instruction;
  108. }
  109. for (; pos < sourceLen; pos++)
  110. {
  111. c = source[pos];
  112. if (Char.IsWhiteSpace(c) || c == '>' || c == '/')
  113. break;
  114. }
  115. if (pos == sourceLen)
  116. break;
  117. buffer.Append(source, parsePos + 1, pos - parsePos - 1);
  118. if (buffer.Length > 0 && buffer[0] == '/')
  119. buffer.Remove(0, 1);
  120. bool singleQuoted = false, doubleQuoted = false;
  121. int possibleEnd = -1;
  122. for (; pos < sourceLen; pos++)
  123. {
  124. c = source[pos];
  125. if (c == '"')
  126. {
  127. if (!singleQuoted)
  128. doubleQuoted = !doubleQuoted;
  129. }
  130. else if (c == '\'')
  131. {
  132. if (!doubleQuoted)
  133. singleQuoted = !singleQuoted;
  134. }
  135. if (c == '>')
  136. {
  137. if (!(singleQuoted || doubleQuoted))
  138. {
  139. possibleEnd = -1;
  140. break;
  141. }
  142. possibleEnd = pos;
  143. }
  144. else if (c == '<')
  145. break;
  146. }
  147. if (possibleEnd != -1)
  148. pos = possibleEnd;
  149. if (pos == sourceLen)
  150. break;
  151. if (source[pos - 1] == '/')
  152. tagType = XMLTagType.Void;
  153. tagName = buffer.ToString();
  154. if (lowerCaseName)
  155. tagName = tagName.ToLower();
  156. tagPos = parsePos;
  157. tagLength = pos + 1 - parsePos;
  158. parsePos += tagLength;
  159. return true;
  160. }
  161. tagPos = sourceLen;
  162. tagLength = 0;
  163. tagName = null;
  164. return false;
  165. }
  166. public static string GetTagSource()
  167. {
  168. return source.Substring(tagPos, tagLength);
  169. }
  170. public static string GetRawText(bool trim = false)
  171. {
  172. if (lastTagEnd == tagPos)
  173. return string.Empty;
  174. else if (trim)
  175. {
  176. int i = lastTagEnd;
  177. for (; i < tagPos; i++)
  178. {
  179. char c = source[i];
  180. if (!char.IsWhiteSpace(c))
  181. break;
  182. }
  183. if (i == tagPos)
  184. return string.Empty;
  185. else
  186. return source.Substring(i, tagPos - i).TrimEnd();
  187. }
  188. else
  189. return source.Substring(lastTagEnd, tagPos - lastTagEnd);
  190. }
  191. public static string GetText(bool trim = false)
  192. {
  193. if (lastTagEnd == tagPos)
  194. return string.Empty;
  195. else if (trim)
  196. {
  197. int i = lastTagEnd;
  198. for (; i < tagPos; i++)
  199. {
  200. char c = source[i];
  201. if (!char.IsWhiteSpace(c))
  202. break;
  203. }
  204. if (i == tagPos)
  205. return string.Empty;
  206. else
  207. return XMLUtils.DecodeString(source.Substring(i, tagPos - i).TrimEnd());
  208. }
  209. else
  210. return XMLUtils.DecodeString(source.Substring(lastTagEnd, tagPos - lastTagEnd));
  211. }
  212. public static bool HasAttribute(string attrName)
  213. {
  214. if (!attrParsed)
  215. {
  216. attributes.Clear();
  217. ParseAttributes(attributes);
  218. attrParsed = true;
  219. }
  220. return attributes.ContainsKey(attrName);
  221. }
  222. public static string GetAttribute(string attrName)
  223. {
  224. if (!attrParsed)
  225. {
  226. attributes.Clear();
  227. ParseAttributes(attributes);
  228. attrParsed = true;
  229. }
  230. string value;
  231. if (attributes.TryGetValue(attrName, out value))
  232. return value;
  233. else
  234. return null;
  235. }
  236. public static string GetAttribute(string attrName, string defValue)
  237. {
  238. string ret = GetAttribute(attrName);
  239. if (ret != null)
  240. return ret;
  241. else
  242. return defValue;
  243. }
  244. public static int GetAttributeInt(string attrName)
  245. {
  246. return GetAttributeInt(attrName, 0);
  247. }
  248. public static int GetAttributeInt(string attrName, int defValue)
  249. {
  250. string value = GetAttribute(attrName);
  251. if (value == null || value.Length == 0)
  252. return defValue;
  253. int ret;
  254. if (int.TryParse(value, out ret))
  255. return ret;
  256. else
  257. return defValue;
  258. }
  259. public static float GetAttributeFloat(string attrName)
  260. {
  261. return GetAttributeFloat(attrName, 0);
  262. }
  263. public static float GetAttributeFloat(string attrName, float defValue)
  264. {
  265. string value = GetAttribute(attrName);
  266. if (value == null || value.Length == 0)
  267. return defValue;
  268. float ret;
  269. if (float.TryParse(value, out ret))
  270. return ret;
  271. else
  272. return defValue;
  273. }
  274. public static bool GetAttributeBool(string attrName)
  275. {
  276. return GetAttributeBool(attrName, false);
  277. }
  278. public static bool GetAttributeBool(string attrName, bool defValue)
  279. {
  280. string value = GetAttribute(attrName);
  281. if (value == null || value.Length == 0)
  282. return defValue;
  283. bool ret;
  284. if (bool.TryParse(value, out ret))
  285. return ret;
  286. else
  287. return defValue;
  288. }
  289. public static Dictionary<string, string> GetAttributes(Dictionary<string, string> result)
  290. {
  291. if (result == null)
  292. result = new Dictionary<string, string>();
  293. if (attrParsed)
  294. {
  295. foreach (KeyValuePair<string, string> kv in attributes)
  296. result[kv.Key] = kv.Value;
  297. }
  298. else //这里没有先ParseAttributes再赋值给result是为了节省复制的操作
  299. ParseAttributes(result);
  300. return result;
  301. }
  302. public static Hashtable GetAttributes(Hashtable result)
  303. {
  304. if (result == null)
  305. result = new Hashtable();
  306. if (attrParsed)
  307. {
  308. foreach (KeyValuePair<string, string> kv in attributes)
  309. result[kv.Key] = kv.Value;
  310. }
  311. else //这里没有先ParseAttributes再赋值给result是为了节省复制的操作
  312. ParseAttributes(result);
  313. return result;
  314. }
  315. static void ParseAttributes(IDictionary attrs)
  316. {
  317. string attrName;
  318. int valueStart;
  319. int valueEnd;
  320. bool waitValue = false;
  321. int quoted;
  322. buffer.Length = 0;
  323. int i = tagPos;
  324. int attrEnd = tagPos + tagLength;
  325. if (i < attrEnd && source[i] == '<')
  326. {
  327. for (; i < attrEnd; i++)
  328. {
  329. char c = source[i];
  330. if (Char.IsWhiteSpace(c) || c == '>' || c == '/')
  331. break;
  332. }
  333. }
  334. for (; i < attrEnd; i++)
  335. {
  336. char c = source[i];
  337. if (c == '=')
  338. {
  339. valueStart = -1;
  340. valueEnd = -1;
  341. quoted = 0;
  342. for (int j = i + 1; j < attrEnd; j++)
  343. {
  344. char c2 = source[j];
  345. if (Char.IsWhiteSpace(c2))
  346. {
  347. if (valueStart != -1 && quoted == 0)
  348. {
  349. valueEnd = j - 1;
  350. break;
  351. }
  352. }
  353. else if (c2 == '>')
  354. {
  355. if (quoted == 0)
  356. {
  357. valueEnd = j - 1;
  358. break;
  359. }
  360. }
  361. else if (c2 == '"')
  362. {
  363. if (valueStart != -1)
  364. {
  365. if (quoted != 1)
  366. {
  367. valueEnd = j - 1;
  368. break;
  369. }
  370. }
  371. else
  372. {
  373. quoted = 2;
  374. valueStart = j + 1;
  375. }
  376. }
  377. else if (c2 == '\'')
  378. {
  379. if (valueStart != -1)
  380. {
  381. if (quoted != 2)
  382. {
  383. valueEnd = j - 1;
  384. break;
  385. }
  386. }
  387. else
  388. {
  389. quoted = 1;
  390. valueStart = j + 1;
  391. }
  392. }
  393. else if (valueStart == -1)
  394. {
  395. valueStart = j;
  396. }
  397. }
  398. if (valueStart != -1 && valueEnd != -1)
  399. {
  400. attrName = buffer.ToString();
  401. if (lowerCaseName)
  402. attrName = attrName.ToLower();
  403. buffer.Length = 0;
  404. attrs[attrName] = XMLUtils.DecodeString(source.Substring(valueStart, valueEnd - valueStart + 1));
  405. i = valueEnd + 1;
  406. }
  407. else
  408. break;
  409. }
  410. else if (!Char.IsWhiteSpace(c))
  411. {
  412. if (waitValue || c == '/' || c == '>')
  413. {
  414. if (buffer.Length > 0)
  415. {
  416. attrName = buffer.ToString();
  417. if (lowerCaseName)
  418. attrName = attrName.ToLower();
  419. attrs[attrName] = string.Empty;
  420. buffer.Length = 0;
  421. }
  422. waitValue = false;
  423. }
  424. if (c != '/' && c != '>')
  425. buffer.Append(c);
  426. }
  427. else
  428. {
  429. if (buffer.Length > 0)
  430. waitValue = true;
  431. }
  432. }
  433. }
  434. }
  435. }