XML.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace FairyGUI.Utils
  6. {
  7. /// <summary>
  8. /// A simplest and readonly XML class
  9. /// </summary>
  10. public class XML
  11. {
  12. public string name;
  13. public string text;
  14. Dictionary<string, string> _attributes;
  15. XMLList _children;
  16. public static XML Create(string tag)
  17. {
  18. XML xml = new XML();
  19. xml.name = tag;
  20. return xml;
  21. }
  22. public XML(string XmlString)
  23. {
  24. Parse(XmlString);
  25. }
  26. private XML()
  27. {
  28. }
  29. public Dictionary<string, string> attributes
  30. {
  31. get
  32. {
  33. if (_attributes == null)
  34. _attributes = new Dictionary<string, string>();
  35. return _attributes;
  36. }
  37. }
  38. public bool HasAttribute(string attrName)
  39. {
  40. if (_attributes == null)
  41. return false;
  42. return _attributes.ContainsKey(attrName);
  43. }
  44. public string GetAttribute(string attrName)
  45. {
  46. return GetAttribute(attrName, null);
  47. }
  48. public string GetAttribute(string attrName, string defValue)
  49. {
  50. if (_attributes == null)
  51. return defValue;
  52. string ret;
  53. if (_attributes.TryGetValue(attrName, out ret))
  54. return ret;
  55. else
  56. return defValue;
  57. }
  58. public int GetAttributeInt(string attrName)
  59. {
  60. return GetAttributeInt(attrName, 0);
  61. }
  62. public int GetAttributeInt(string attrName, int defValue)
  63. {
  64. string value = GetAttribute(attrName);
  65. if (value == null || value.Length == 0)
  66. return defValue;
  67. int ret;
  68. if (int.TryParse(value, out ret))
  69. return ret;
  70. else
  71. return defValue;
  72. }
  73. public float GetAttributeFloat(string attrName)
  74. {
  75. return GetAttributeFloat(attrName, 0);
  76. }
  77. public float GetAttributeFloat(string attrName, float defValue)
  78. {
  79. string value = GetAttribute(attrName);
  80. if (value == null || value.Length == 0)
  81. return defValue;
  82. float ret;
  83. if (float.TryParse(value, out ret))
  84. return ret;
  85. else
  86. return defValue;
  87. }
  88. public bool GetAttributeBool(string attrName)
  89. {
  90. return GetAttributeBool(attrName, false);
  91. }
  92. public bool GetAttributeBool(string attrName, bool defValue)
  93. {
  94. string value = GetAttribute(attrName);
  95. if (value == null || value.Length == 0)
  96. return defValue;
  97. bool ret;
  98. if (bool.TryParse(value, out ret))
  99. return ret;
  100. else
  101. return defValue;
  102. }
  103. public string[] GetAttributeArray(string attrName)
  104. {
  105. string value = GetAttribute(attrName);
  106. if (value != null)
  107. {
  108. if (value.Length == 0)
  109. return new string[] { };
  110. else
  111. return value.Split(',');
  112. }
  113. else
  114. return null;
  115. }
  116. public string[] GetAttributeArray(string attrName, char seperator)
  117. {
  118. string value = GetAttribute(attrName);
  119. if (value != null)
  120. {
  121. if (value.Length == 0)
  122. return new string[] { };
  123. else
  124. return value.Split(seperator);
  125. }
  126. else
  127. return null;
  128. }
  129. public Color GetAttributeColor(string attrName, Color defValue)
  130. {
  131. string value = GetAttribute(attrName);
  132. if (value == null || value.Length == 0)
  133. return defValue;
  134. return ToolSet.ConvertFromHtmlColor(value);
  135. }
  136. public Vector2 GetAttributeVector(string attrName)
  137. {
  138. string value = GetAttribute(attrName);
  139. if (value != null)
  140. {
  141. string[] arr = value.Split(',');
  142. return new Vector2(float.Parse(arr[0]), float.Parse(arr[1]));
  143. }
  144. else
  145. return Vector2.zero;
  146. }
  147. public void SetAttribute(string attrName, string attrValue)
  148. {
  149. if (_attributes == null)
  150. _attributes = new Dictionary<string, string>();
  151. _attributes[attrName] = attrValue;
  152. }
  153. public void SetAttribute(string attrName, bool attrValue)
  154. {
  155. if (_attributes == null)
  156. _attributes = new Dictionary<string, string>();
  157. _attributes[attrName] = attrValue ? "true" : "false";
  158. }
  159. public void SetAttribute(string attrName, int attrValue)
  160. {
  161. if (_attributes == null)
  162. _attributes = new Dictionary<string, string>();
  163. _attributes[attrName] = attrValue.ToString();
  164. }
  165. public void SetAttribute(string attrName, float attrValue)
  166. {
  167. if (_attributes == null)
  168. _attributes = new Dictionary<string, string>();
  169. _attributes[attrName] = string.Format("{0:#.####}", attrValue);
  170. }
  171. public void RemoveAttribute(string attrName)
  172. {
  173. if (_attributes != null)
  174. _attributes.Remove(attrName);
  175. }
  176. public XML GetNode(string selector)
  177. {
  178. if (_children == null)
  179. return null;
  180. else
  181. return _children.Find(selector);
  182. }
  183. public XMLList elements
  184. {
  185. get
  186. {
  187. if (_children == null)
  188. _children = new XMLList();
  189. return _children;
  190. }
  191. }
  192. public XMLList Elements()
  193. {
  194. if (_children == null)
  195. _children = new XMLList();
  196. return _children;
  197. }
  198. public XMLList Elements(string selector)
  199. {
  200. if (_children == null)
  201. _children = new XMLList();
  202. return _children.Filter(selector);
  203. }
  204. public XMLList.Enumerator GetEnumerator()
  205. {
  206. if (_children == null)
  207. return new XMLList.Enumerator(null, null);
  208. else
  209. return new XMLList.Enumerator(_children.rawList, null);
  210. }
  211. public XMLList.Enumerator GetEnumerator(string selector)
  212. {
  213. if (_children == null)
  214. return new XMLList.Enumerator(null, selector);
  215. else
  216. return new XMLList.Enumerator(_children.rawList, selector);
  217. }
  218. public void AppendChild(XML child)
  219. {
  220. this.elements.Add(child);
  221. }
  222. public void RemoveChild(XML child)
  223. {
  224. if (_children == null)
  225. return;
  226. this._children.rawList.Remove(child);
  227. }
  228. public void RemoveChildren(string selector)
  229. {
  230. if (_children == null)
  231. return;
  232. if (string.IsNullOrEmpty(selector))
  233. _children.Clear();
  234. else
  235. _children.RemoveAll(selector);
  236. }
  237. static Stack<XML> sNodeStack = new Stack<XML>();
  238. public void Parse(string aSource)
  239. {
  240. Reset();
  241. XML lastOpenNode = null;
  242. sNodeStack.Clear();
  243. XMLIterator.Begin(aSource);
  244. while (XMLIterator.NextTag())
  245. {
  246. if (XMLIterator.tagType == XMLTagType.Start || XMLIterator.tagType == XMLTagType.Void)
  247. {
  248. XML childNode;
  249. if (lastOpenNode != null)
  250. childNode = new XML();
  251. else
  252. {
  253. if (this.name != null)
  254. {
  255. Reset();
  256. throw new Exception("Invalid xml format - no root node.");
  257. }
  258. childNode = this;
  259. }
  260. childNode.name = XMLIterator.tagName;
  261. childNode._attributes = XMLIterator.GetAttributes(childNode._attributes);
  262. if (lastOpenNode != null)
  263. {
  264. if (XMLIterator.tagType != XMLTagType.Void)
  265. sNodeStack.Push(lastOpenNode);
  266. if (lastOpenNode._children == null)
  267. lastOpenNode._children = new XMLList();
  268. lastOpenNode._children.Add(childNode);
  269. }
  270. if (XMLIterator.tagType != XMLTagType.Void)
  271. lastOpenNode = childNode;
  272. }
  273. else if (XMLIterator.tagType == XMLTagType.End)
  274. {
  275. if (lastOpenNode == null || lastOpenNode.name != XMLIterator.tagName)
  276. {
  277. Reset();
  278. throw new Exception("Invalid xml format - <" + XMLIterator.tagName + "> dismatched.");
  279. }
  280. if (lastOpenNode._children == null || lastOpenNode._children.Count == 0)
  281. {
  282. lastOpenNode.text = XMLIterator.GetText();
  283. }
  284. if (sNodeStack.Count > 0)
  285. lastOpenNode = sNodeStack.Pop();
  286. else
  287. lastOpenNode = null;
  288. }
  289. }
  290. }
  291. public void Reset()
  292. {
  293. if (_attributes != null)
  294. _attributes.Clear();
  295. if (_children != null)
  296. _children.Clear();
  297. this.text = null;
  298. }
  299. public string ToXMLString(bool includeHeader)
  300. {
  301. StringBuilder sb = new StringBuilder();
  302. if (includeHeader)
  303. sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  304. ToXMLString(sb, 0);
  305. return sb.ToString();
  306. }
  307. void ToXMLString(StringBuilder sb, int tabs)
  308. {
  309. if (tabs > 0)
  310. sb.Append(' ', tabs * 2);
  311. if (name == "!")
  312. {
  313. sb.Append("<!--");
  314. if (text != null)
  315. {
  316. int c = sb.Length;
  317. sb.Append(text);
  318. XMLUtils.EncodeString(sb, c);
  319. }
  320. sb.Append("-->");
  321. return;
  322. }
  323. sb.Append('<').Append(name);
  324. if (_attributes != null)
  325. {
  326. foreach (KeyValuePair<string, string> kv in _attributes)
  327. {
  328. sb.Append(' ');
  329. sb.Append(kv.Key).Append('=').Append('\"');
  330. int c = sb.Length;
  331. sb.Append(kv.Value);
  332. XMLUtils.EncodeString(sb, c, true);
  333. sb.Append("\"");
  334. }
  335. }
  336. int numChildren = _children != null ? _children.Count : 0;
  337. if (string.IsNullOrEmpty(text) && numChildren == 0)
  338. sb.Append("/>");
  339. else
  340. {
  341. sb.Append('>');
  342. if (!string.IsNullOrEmpty(text))
  343. {
  344. int c = sb.Length;
  345. sb.Append(text);
  346. XMLUtils.EncodeString(sb, c);
  347. }
  348. if (numChildren > 0)
  349. {
  350. sb.Append('\n');
  351. int ctabs = tabs + 1;
  352. for (int i = 0; i < numChildren; i++)
  353. {
  354. _children[i].ToXMLString(sb, ctabs);
  355. sb.Append('\n');
  356. }
  357. if (tabs > 0)
  358. sb.Append(' ', tabs * 2);
  359. }
  360. sb.Append("</").Append(name).Append(">");
  361. }
  362. }
  363. }
  364. }