123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- namespace FairyGUI.Utils
- {
- /// <summary>
- /// A simplest and readonly XML class
- /// </summary>
- public class XML
- {
- public string name;
- public string text;
- Dictionary<string, string> _attributes;
- XMLList _children;
- public static XML Create(string tag)
- {
- XML xml = new XML();
- xml.name = tag;
- return xml;
- }
- public XML(string XmlString)
- {
- Parse(XmlString);
- }
- private XML()
- {
- }
- public Dictionary<string, string> attributes
- {
- get
- {
- if (_attributes == null)
- _attributes = new Dictionary<string, string>();
- return _attributes;
- }
- }
- public bool HasAttribute(string attrName)
- {
- if (_attributes == null)
- return false;
- return _attributes.ContainsKey(attrName);
- }
- public string GetAttribute(string attrName)
- {
- return GetAttribute(attrName, null);
- }
- public string GetAttribute(string attrName, string defValue)
- {
- if (_attributes == null)
- return defValue;
- string ret;
- if (_attributes.TryGetValue(attrName, out ret))
- return ret;
- else
- return defValue;
- }
- public int GetAttributeInt(string attrName)
- {
- return GetAttributeInt(attrName, 0);
- }
- public int GetAttributeInt(string attrName, int defValue)
- {
- string value = GetAttribute(attrName);
- if (value == null || value.Length == 0)
- return defValue;
- int ret;
- if (int.TryParse(value, out ret))
- return ret;
- else
- return defValue;
- }
- public float GetAttributeFloat(string attrName)
- {
- return GetAttributeFloat(attrName, 0);
- }
- public float GetAttributeFloat(string attrName, float defValue)
- {
- string value = GetAttribute(attrName);
- if (value == null || value.Length == 0)
- return defValue;
- float ret;
- if (float.TryParse(value, out ret))
- return ret;
- else
- return defValue;
- }
- public bool GetAttributeBool(string attrName)
- {
- return GetAttributeBool(attrName, false);
- }
- public bool GetAttributeBool(string attrName, bool defValue)
- {
- string value = GetAttribute(attrName);
- if (value == null || value.Length == 0)
- return defValue;
- bool ret;
- if (bool.TryParse(value, out ret))
- return ret;
- else
- return defValue;
- }
- public string[] GetAttributeArray(string attrName)
- {
- string value = GetAttribute(attrName);
- if (value != null)
- {
- if (value.Length == 0)
- return new string[] { };
- else
- return value.Split(',');
- }
- else
- return null;
- }
- public string[] GetAttributeArray(string attrName, char seperator)
- {
- string value = GetAttribute(attrName);
- if (value != null)
- {
- if (value.Length == 0)
- return new string[] { };
- else
- return value.Split(seperator);
- }
- else
- return null;
- }
- public Color GetAttributeColor(string attrName, Color defValue)
- {
- string value = GetAttribute(attrName);
- if (value == null || value.Length == 0)
- return defValue;
- return ToolSet.ConvertFromHtmlColor(value);
- }
- public Vector2 GetAttributeVector(string attrName)
- {
- string value = GetAttribute(attrName);
- if (value != null)
- {
- string[] arr = value.Split(',');
- return new Vector2(float.Parse(arr[0]), float.Parse(arr[1]));
- }
- else
- return Vector2.zero;
- }
- public void SetAttribute(string attrName, string attrValue)
- {
- if (_attributes == null)
- _attributes = new Dictionary<string, string>();
- _attributes[attrName] = attrValue;
- }
- public void SetAttribute(string attrName, bool attrValue)
- {
- if (_attributes == null)
- _attributes = new Dictionary<string, string>();
- _attributes[attrName] = attrValue ? "true" : "false";
- }
- public void SetAttribute(string attrName, int attrValue)
- {
- if (_attributes == null)
- _attributes = new Dictionary<string, string>();
- _attributes[attrName] = attrValue.ToString();
- }
- public void SetAttribute(string attrName, float attrValue)
- {
- if (_attributes == null)
- _attributes = new Dictionary<string, string>();
- _attributes[attrName] = string.Format("{0:#.####}", attrValue);
- }
- public void RemoveAttribute(string attrName)
- {
- if (_attributes != null)
- _attributes.Remove(attrName);
- }
- public XML GetNode(string selector)
- {
- if (_children == null)
- return null;
- else
- return _children.Find(selector);
- }
- public XMLList elements
- {
- get
- {
- if (_children == null)
- _children = new XMLList();
- return _children;
- }
- }
- public XMLList Elements()
- {
- if (_children == null)
- _children = new XMLList();
- return _children;
- }
- public XMLList Elements(string selector)
- {
- if (_children == null)
- _children = new XMLList();
- return _children.Filter(selector);
- }
- public XMLList.Enumerator GetEnumerator()
- {
- if (_children == null)
- return new XMLList.Enumerator(null, null);
- else
- return new XMLList.Enumerator(_children.rawList, null);
- }
- public XMLList.Enumerator GetEnumerator(string selector)
- {
- if (_children == null)
- return new XMLList.Enumerator(null, selector);
- else
- return new XMLList.Enumerator(_children.rawList, selector);
- }
- public void AppendChild(XML child)
- {
- this.elements.Add(child);
- }
- public void RemoveChild(XML child)
- {
- if (_children == null)
- return;
- this._children.rawList.Remove(child);
- }
- public void RemoveChildren(string selector)
- {
- if (_children == null)
- return;
- if (string.IsNullOrEmpty(selector))
- _children.Clear();
- else
- _children.RemoveAll(selector);
- }
- static Stack<XML> sNodeStack = new Stack<XML>();
- public void Parse(string aSource)
- {
- Reset();
-
- XML lastOpenNode = null;
- sNodeStack.Clear();
- XMLIterator.Begin(aSource);
- while (XMLIterator.NextTag())
- {
- if (XMLIterator.tagType == XMLTagType.Start || XMLIterator.tagType == XMLTagType.Void)
- {
- XML childNode;
- if (lastOpenNode != null)
- childNode = new XML();
- else
- {
- if (this.name != null)
- {
- Reset();
- throw new Exception("Invalid xml format - no root node.");
- }
- childNode = this;
- }
- childNode.name = XMLIterator.tagName;
- childNode._attributes = XMLIterator.GetAttributes(childNode._attributes);
- if (lastOpenNode != null)
- {
- if (XMLIterator.tagType != XMLTagType.Void)
- sNodeStack.Push(lastOpenNode);
- if (lastOpenNode._children == null)
- lastOpenNode._children = new XMLList();
- lastOpenNode._children.Add(childNode);
- }
- if (XMLIterator.tagType != XMLTagType.Void)
- lastOpenNode = childNode;
- }
- else if (XMLIterator.tagType == XMLTagType.End)
- {
- if (lastOpenNode == null || lastOpenNode.name != XMLIterator.tagName)
- {
- Reset();
- throw new Exception("Invalid xml format - <" + XMLIterator.tagName + "> dismatched.");
- }
- if (lastOpenNode._children == null || lastOpenNode._children.Count == 0)
- {
- lastOpenNode.text = XMLIterator.GetText();
- }
- if (sNodeStack.Count > 0)
- lastOpenNode = sNodeStack.Pop();
- else
- lastOpenNode = null;
- }
- }
- }
- public void Reset()
- {
- if (_attributes != null)
- _attributes.Clear();
- if (_children != null)
- _children.Clear();
- this.text = null;
- }
- public string ToXMLString(bool includeHeader)
- {
- StringBuilder sb = new StringBuilder();
- if (includeHeader)
- sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
- ToXMLString(sb, 0);
- return sb.ToString();
- }
- void ToXMLString(StringBuilder sb, int tabs)
- {
- if (tabs > 0)
- sb.Append(' ', tabs * 2);
- if (name == "!")
- {
- sb.Append("<!--");
- if (text != null)
- {
- int c = sb.Length;
- sb.Append(text);
- XMLUtils.EncodeString(sb, c);
- }
- sb.Append("-->");
- return;
- }
- sb.Append('<').Append(name);
- if (_attributes != null)
- {
- foreach (KeyValuePair<string, string> kv in _attributes)
- {
- sb.Append(' ');
- sb.Append(kv.Key).Append('=').Append('\"');
- int c = sb.Length;
- sb.Append(kv.Value);
- XMLUtils.EncodeString(sb, c, true);
- sb.Append("\"");
- }
- }
- int numChildren = _children != null ? _children.Count : 0;
- if (string.IsNullOrEmpty(text) && numChildren == 0)
- sb.Append("/>");
- else
- {
- sb.Append('>');
- if (!string.IsNullOrEmpty(text))
- {
- int c = sb.Length;
- sb.Append(text);
- XMLUtils.EncodeString(sb, c);
- }
- if (numChildren > 0)
- {
- sb.Append('\n');
- int ctabs = tabs + 1;
- for (int i = 0; i < numChildren; i++)
- {
- _children[i].ToXMLString(sb, ctabs);
- sb.Append('\n');
- }
- if (tabs > 0)
- sb.Append(' ', tabs * 2);
- }
- sb.Append("</").Append(name).Append(">");
- }
- }
- }
- }
|