Ejemplo n.º 1
0
    public EditorXmlNode CreateChildElement(string name)
    {
        EditorXmlNode child = new EditorXmlNode(EditorXmlNodeType.ELEMENT, name);

        AttachChild(child);

        return(child);
    }
Ejemplo n.º 2
0
    // -------------------------------------------------------------------------------------------------------------
    public EditorXmlNode NextChild(EditorXmlNode child)
    {
        int index = mChildren.IndexOf(child);

        if (index != -1 && index < mChildren.Count - 1)
        {
            return(mChildren [index + 1]);
        }

        return(null);
    }
Ejemplo n.º 3
0
    public EditorXmlNode PreviousChild(EditorXmlNode child)
    {
        int index = mChildren.IndexOf(child);

        if (index >= 1)
        {
            return(mChildren [index - 1]);
        }

        return(null);
    }
Ejemplo n.º 4
0
    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 ());
    }
Ejemplo n.º 5
0
    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);
    }
Ejemplo n.º 6
0
    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;
                }
            }
        }
    }
Ejemplo n.º 7
0
    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;
            }
        }
    }
Ejemplo n.º 8
0
    // -------------------------------------------------------------------------------------------------------------
    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
    }
Ejemplo n.º 9
0
    // -------------------------------------------------------------------------------------------------------------
    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
    }
Ejemplo n.º 10
0
 public static EditorXmlNode ParseDocument(string xml)
 {
     return(EditorXmlNode.ParseDocument(xml, EditorXmlNodeType.NONE));
 }
Ejemplo n.º 11
0
    /*
     * 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));
    }
Ejemplo n.º 12
0
    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);
    }
Ejemplo n.º 13
0
    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);
    }
Ejemplo n.º 14
0
    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);
    }