public EditorXmlNode CreateChildElement(string name) { EditorXmlNode child = new EditorXmlNode(EditorXmlNodeType.ELEMENT, name); AttachChild(child); return(child); }
// ------------------------------------------------------------------------------------------------------------- public EditorXmlNode NextChild(EditorXmlNode child) { int index = mChildren.IndexOf(child); if (index != -1 && index < mChildren.Count - 1) { return(mChildren [index + 1]); } return(null); }
public EditorXmlNode PreviousChild(EditorXmlNode child) { int index = mChildren.IndexOf(child); if (index >= 1) { return(mChildren [index - 1]); } return(null); }
private void _Create(EditorXmlNodeType type, string name, string content) { //LOG.say ("New node: " + LOG.fill (type.ToString (), 15) + " | " + LOG.fill (name.ToString (), 15) + " | [" + content.Replace ("\n", "/") + "]"); //int index; this.type = type; this.name = name; this.content = content; mParent = null; mChildren = new List <EditorXmlNode> (1); mAttributes = new List <EditorXmlNode> (0); //_ParseAttributes (content.Trim ()); }
public static EditorXmlNode ParseDocumentProfiling(string xml, EditorXmlNodeType ignore) { EditorXmlNode document = null; //DateTime start = DateTime.Now; document = EditorXmlNode.ParseDocument(xml, ignore); //DateTime end = DateTime.Now; //TimeSpan span = end - start; //say ("parsing xml string to xml document: " + span.TotalSeconds + " sec"); return(document); }
public void DetachChild(EditorXmlNode child) { if (child != null && child.parent == this && (type == EditorXmlNodeType.ELEMENT || type == EditorXmlNodeType.DOCUMENT)) { if (mChildren.Contains(child)) { mChildren.Remove(child); if (child.parent != null) { child.parent = null; } } } }
public void AttachChild(EditorXmlNode child) { if (child != null && (type == EditorXmlNodeType.ELEMENT || type == EditorXmlNodeType.DOCUMENT)) { if (!mChildren.Contains(child)) { if (child.parent != null) { child.parent = null; } mChildren.Add(child); child.parent = this; } } }
// ------------------------------------------------------------------------------------------------------------- private void Initialize(string xmlfilename, bool forcenew) { #if !UNITY_WEBPLAYER mXmlFilename = EDITOR_STATE_FOLDER + "/" + xmlfilename; if (!Directory.Exists(EDITOR_STATE_FOLDER)) { Directory.CreateDirectory(EDITOR_STATE_FOLDER); } if (forcenew) { File.CreateText(mXmlFilename).Close(); } else if (!File.Exists(mXmlFilename)) { Debug.LogWarning("File '" + xmlfilename + "' not found under " + EDITOR_STATE_FOLDER + " folder, creating a new one."); File.CreateText(mXmlFilename).Close(); } mXmlFileContent = File.ReadAllText(mXmlFilename); EditorXmlNode file = EditorXmlNode.ParseDocument(mXmlFileContent, EditorXmlNodeType.DATA); EditorXmlNode records = file.GetChildByName("records"); mRecords = new Dictionary <string, string>(); if (records != null) { foreach (EditorXmlNode record in records.GetChildrenByName("record")) { mRecords.Add(record.GetAttribute("name").content, record.GetAttribute("value").content); } } #endif }
// ------------------------------------------------------------------------------------------------------------- public void Save() { #if !UNITY_WEBPLAYER File.CreateText(mXmlFilename).Close(); EditorXmlNode document = EditorXmlNode.CreateDocument(); EditorXmlNode records = new EditorXmlNode(EditorXmlNodeType.ELEMENT, "records"); EditorXmlNode record = null; document.AttachChild(records); foreach (string key in mRecords.Keys) { record = new EditorXmlNode(EditorXmlNodeType.ELEMENT, "record"); record.SetAttribute("name", key); record.SetAttribute("value", mRecords[key]); records.AttachChild(record); } File.WriteAllText(mXmlFilename, document.ToXmlString(true)); #endif }
public static EditorXmlNode ParseDocument(string xml) { return(EditorXmlNode.ParseDocument(xml, EditorXmlNodeType.NONE)); }
/* * private void _ToXmlListIndent (List<string> list, int indent) * { * DateTime start = DateTime.Now; * DateTime end; * TimeSpan span; * * switch ( type ) * { * case XmlNodeType.DOCUMENT: * { * if ( indent > 0 ) * list.Add (new String (' ', indent * 3)); * * foreach (XmlNode node in mChildren) * { * node._ToXmlListIndent (list, indent, false); * } * * break; * } * * case XmlNodeType.DECLARATION: * { * if ( indent > 0 ) * list.Add (new String (' ', indent * 3)); * * list.Add ("<?" + name); * if ( mAttributes.Count != 0 ) * { * foreach ( XmlNode node in mAttributes ) * { * list.Add (" " + node.name + "=\"" + node.content + "\""); * } * } * list.Add ("?>\n"); * * break; * } * * case XmlNodeType.COMMENT: * { * if ( indent > 0 ) * list.Add (new String (' ', indent * 3)); * * list.Add ("<!--" + content + "-->\n"); * * break; * } * * case XmlNodeType.TEXT: * { * list.Add (content); * * break; * } * * case XmlNodeType.ELEMENT: * { * if ( mChildren.Count == 0 ) * { * if ( indent > 0 ) * list.Add (new String (' ', indent * 3)); * * list.Add ("<" + name); * if ( mAttributes.Count != 0 ) * { * foreach ( XmlNode node in mAttributes ) * { * list.Add (" " + node.name + "=\"" + node.content + "\""); * } * } * list.Add ("/>\n"); * } * else * { * bool textflag = false; * * XmlNode previous = parent.PreviousChild (this); * * if ( indent > 0 && (previous == null || previous.type != XmlNodeType.TEXT) ) * list.Add (new String (' ', indent * 3)); * * list.Add ("<" + name); * if ( mAttributes.Count != 0 ) * { * foreach ( XmlNode node in mAttributes ) * { * list.Add (" " + node.name + "=\"" + node.content + "\""); * } * } * list.Add (">"); * * if ( mChildren.Count != 0 ) * { * if ( mChildren [0].type == XmlNodeType.TEXT ) * textflag = true; * } * * if ( !textflag ) * list.Add ("\n"); * * foreach (XmlNode node in mChildren) * { * node._ToXmlListIndent (list, indent + 1, false); * } * * if ( indent > 0 && !textflag ) * list.Add (new String (' ', indent * 3)); * * list.Add ("</" + name + ">"); * * if ( !parent.inline ) * list.Add ("\n"); * } * * break; * } * * case XmlNodeType.UNDEFINED: * { * if ( indent > 0 ) * list.Add (new String (' ', indent * 3)); * * list.Add ("<!-- Undefined node: " + name + " -->"); * * break; * } * } * * if ( profiling ) * { * end = DateTime.Now; * span = end - start; * * say ("building string representation using list<string> sequence: " + span.TotalSeconds + " sec"); * } * } */ public static EditorXmlNode CreateDocument() { return(EditorXmlNode.ParseDocument(string.Empty)); }
private List <string> _ToXmlListIndent(List <string> list, int indent) { switch (type) { case EditorXmlNodeType.DOCUMENT: { if (indent > 0) { list.Add(new String(' ', indent * 3)); } foreach (EditorXmlNode node in mChildren) { list = node._ToXmlListIndent(list, indent); } break; } case EditorXmlNodeType.DECLARATION: { if (indent > 0) { list.Add(new String(' ', indent * 3)); } list.Add("<?" + name); if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { list.Add(" " + node.name + "=\"" + node.content + "\""); } } list.Add("?>\n"); break; } case EditorXmlNodeType.COMMENT: { if (indent > 0) { list.Add(new String(' ', indent * 3)); } list.Add("<!--" + content + "-->\n"); break; } case EditorXmlNodeType.TEXT: { list.Add(content); break; } case EditorXmlNodeType.ELEMENT: { if (mChildren.Count == 0) { if (indent > 0) { list.Add(new String(' ', indent * 3)); } list.Add("<" + name); if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { list.Add(" " + node.name + "=\"" + node.content + "\""); } } list.Add("/>\n"); } else { bool textflag = false; EditorXmlNode previous = parent.PreviousChild(this); if (indent > 0 && (previous == null || previous.type != EditorXmlNodeType.TEXT)) { list.Add(new String(' ', indent * 3)); } list.Add("<" + name); if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { list.Add(" " + node.name + "=\"" + node.content + "\""); } } list.Add(">"); if (mChildren.Count != 0) { if (mChildren [0].type == EditorXmlNodeType.TEXT) { textflag = true; } } if (!textflag) { list.Add("\n"); } foreach (EditorXmlNode node in mChildren) { list = node._ToXmlListIndent(list, indent + 1); } if (indent > 0 && !textflag) { list.Add(new String(' ', indent * 3)); } list.Add("</" + name + ">"); if (!parent.inline) { list.Add("\n"); } } break; } case EditorXmlNodeType.WHITESPACE: { break; } default: { if (indent > 0) { list.Add(new String(' ', indent * 3)); } list.Add("<!-- Undefined node: " + name + " -->\n"); break; } } return(list); }
private string _ToXmlStringIndent(int indent) { string str = string.Empty; switch (type) { case EditorXmlNodeType.DOCUMENT: { if (indent > 0) { str += new String(' ', indent * 3); } foreach (EditorXmlNode node in mChildren) { str += node._ToXmlStringIndent(indent); } break; } case EditorXmlNodeType.DECLARATION: { if (indent > 0) { str += new String(' ', indent * 3); } str += "<?" + name; if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { str += " " + node.name + "=\"" + node.content + "\""; } } str += "?>\n"; break; } case EditorXmlNodeType.COMMENT: { if (indent > 0) { str += new String(' ', indent * 3); } str += "<!--" + content + "-->\n"; break; } case EditorXmlNodeType.TEXT: { str += content; break; } case EditorXmlNodeType.ELEMENT: { if (mChildren.Count == 0) { if (indent > 0) { str += new String(' ', indent * 3); } str += "<" + name; if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { str += " " + node.name + "=\"" + node.content + "\""; } } str += "/>\n"; } else { bool textflag = false; EditorXmlNode previous = parent.PreviousChild(this); if (indent > 0 && (previous == null || previous.type != EditorXmlNodeType.TEXT)) { str += new String(' ', indent * 3); } str += "<" + name; if (mAttributes.Count != 0) { foreach (EditorXmlNode node in mAttributes) { str += " " + node.name + "=\"" + node.content + "\""; } } str += ">"; if (mChildren.Count != 0) { if (mChildren [0].type == EditorXmlNodeType.TEXT) { textflag = true; } } if (!textflag) { str += "\n"; } foreach (EditorXmlNode node in mChildren) { str += node._ToXmlStringIndent(indent + 1); } if (indent > 0 && !textflag) { str += new String(' ', indent * 3); } str += "</" + name + ">"; if (!parent.inline) { str += "\n"; } } break; } case EditorXmlNodeType.WHITESPACE: { break; } default: { if (indent > 0) { str += new String(' ', indent * 3); } str += "<!-- Undefined node: " + name + " -->\n"; break; } } return(str); }
public static EditorXmlNode ParseDocument(string xml, EditorXmlNodeType ignore) { EditorXmlNode emptydocument = new EditorXmlNode(EditorXmlNodeType.DOCUMENT, "XmlDocument", ""); EditorXmlNode document = new EditorXmlNode(EditorXmlNodeType.DOCUMENT, "XmlDocument", ""); XmlRawData data = new XmlRawData(xml); EditorXmlNode current = document; EditorXmlNode node; string tagname = string.Empty; string content = string.Empty; int mark; int textmark = -1; while (data.valid) { if (data.actual == '<') { // Check if text was found before entering the tag if (textmark != -1) { content = data.GetData(textmark, data.pointer - textmark); if (content.Trim() == string.Empty) { if (!((ignore & EditorXmlNodeType.WHITESPACE) == EditorXmlNodeType.WHITESPACE)) { node = new EditorXmlNode(EditorXmlNodeType.WHITESPACE, "XmlWhitespace", content); current.AttachChild(node); } } else { if (!((ignore & EditorXmlNodeType.TEXT) == EditorXmlNodeType.TEXT)) { node = new EditorXmlNode(EditorXmlNodeType.TEXT, "XmlText", content); current.AttachChild(node); } } textmark = -1; } // Xml declaration ? if (data.next == '?') { data.Advance(2); mark = data.pointer; // Get the node's name while (data.valid && data.actual != '?' && !data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("The declaration was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); if (tagname == string.Empty) { //data.say ("An Xml declaration can not have an empty name"); return(emptydocument); } node = new EditorXmlNode(EditorXmlNodeType.DECLARATION, tagname); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } while (data.valid && data.actual != '?') { if (!data.IsOn(WHITESPACES)) { mark = data.pointer; while (data.valid && data.actual != '=') { data.Advance(); } if (!data.valid) { //data.say ("All attributes must be matched with a value"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); data.Advance(); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("All attributes must have a value specified"); return(emptydocument); } while (data.valid && data.actual != '"') { data.Advance(); } data.Advance(); mark = data.pointer; while (data.valid && data.actual != '"') { data.Advance(); } if (!data.valid) { //data.say ("All attribute's values must be enclosed with double quotes"); return(emptydocument); } content = data.GetData(mark, data.pointer - mark); node.SetAttribute(tagname, content); } data.Advance(); } if (!data.valid) { //data.say ("End of declaration was not closed properly"); return(emptydocument); } current.AttachChild(node); data.Advance(2); } // Comments and CDATA else if (data.next == '!') { // Comments ? if (data.GetData(4) == "<!--") { data.Advance(4); mark = data.pointer; while (data.valid && (data.actual != '-' || data.GetData(3) != "-->")) { data.Advance(); } if (!data.valid) { //data.say ("Comment was not closed properly"); return(emptydocument); } if (!((ignore & EditorXmlNodeType.COMMENT) == EditorXmlNodeType.COMMENT)) { node = new EditorXmlNode(EditorXmlNodeType.COMMENT, "XmlComment", data.GetData(mark, data.pointer - mark)); current.AttachChild(node); } data.Advance(2); } else { //data.say ("Tag type could not be determined"); return(emptydocument); } } // Closing element else if (data.next == '/') { data.Advance(2); mark = data.pointer; while (data.valid && data.actual != '>') { data.Advance(); } if (!data.valid) { //data.say ("End of element was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark); if (string.Compare(tagname, current.name) != 0) { //data.say ("Closing a not previously opened element (" + tagname + "," + current.name + ")"); return(emptydocument); } current = current.parent; } // Everything else (expecting a new element) else { data.Advance(); mark = data.pointer; // Get the node's name while (data.valid && data.actual != '>' && data.actual != '/' && !data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("The element was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); if (tagname == string.Empty) { //data.say ("A tag can not have an empty name"); return(emptydocument); } node = new EditorXmlNode(EditorXmlNodeType.ELEMENT, tagname); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } while (data.valid && data.actual != '>') { if (data.actual == '/' && data.GetData(2) == "/>") { break; } else if (data.actual == '>') { break; } else if (!data.IsOn(WHITESPACES)) { mark = data.pointer; while (data.valid && data.actual != '=') { data.Advance(); } if (!data.valid) { //data.say ("All attributes must be matched with a value"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); data.Advance(); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("All attributes must have a value specified"); return(emptydocument); } while (data.valid && data.actual != '"') { data.Advance(); } data.Advance(); mark = data.pointer; while (data.valid && data.actual != '"') { data.Advance(); } if (!data.valid) { //data.say ("All attribute's values must be enclosed with double quotes"); return(emptydocument); } content = data.GetData(mark, data.pointer - mark); node.SetAttribute(tagname, content); } data.Advance(); } if (!data.valid) { //data.say ("End of element was not closed properly"); return(emptydocument); } if (data.actual == '>') { current.AttachChild(node); current = node; } else if (data.actual == '/') { current.AttachChild(node); data.Advance(); } } } else { if (textmark == -1) { textmark = data.pointer; } } data.Advance(); } return(document); }