Ejemplo n.º 1
0
    // This function parses a single tag and calls Parse() if it encounters subtags
    string ParseTag(string eS, XnXMLHashtable eH)
    {
        // search for "<"
        int ndx = eS.IndexOf("<");
        int end, end1, end2, end3;

        if (ndx == -1)
        {
            // It's possible that this is just a string (e.g. <someTagTheStringIsInside>string</someTagTheStringIsInside>)
            end3 = eS.IndexOf(">");             // This closes a standard tag; look for the closing tag
            if (end3 == -1)
            {
                // In that case, we just need to add an @ key/value to the hashtable
                eS = eS.Trim();                 // I think this is redundant
                //eH["@"] = eS;
                eH.text = eS;
            }
            return("");             // We're done with this tag
        }
        // Ignore this if it is just an XML header (e.g. <?xml version="1.0"?>)
        if (eS[ndx + 1] == '?')
        {
            // search for the closing tag of this header
            int    ndx2   = eS.IndexOf("?>");
            string header = eS.Substring(ndx, ndx2 - ndx + 2);
            //eH["@XML_Header"] = header;
            eH.header = header;
            return(eS.Substring(ndx2 + 2));
        }
        // Ignore this if it is an XML comment (e.g. <!-- Comment text -->)
        if (eS[ndx + 1] == '!')
        {
            // search for the closing tag of this header
            int    ndx2    = eS.IndexOf("-->");
            string comment = eS.Substring(ndx, ndx2 - ndx + 3);
            if (SHOW_COMMENTS)
            {
                Debug.Log("XMl Comment: " + comment);
            }
            //eH["@XML_Header"] = header;
            return(eS.Substring(ndx2 + 3));
        }

        // Find the end of the tag name
        // For the next few comments, this is what happens when this character is the first one found after the beginning of the tag
        end1 = eS.IndexOf(" ", ndx);            // This means that we'll have attributes
        end2 = eS.IndexOf("/", ndx);            // Immediately closes the tag,
        end3 = eS.IndexOf(">", ndx);            // This closes a standard tag; look for the closing tag
        if (end1 == -1)
        {
            end1 = int.MaxValue;
        }
        if (end2 == -1)
        {
            end2 = int.MaxValue;
        }
        if (end3 == -1)
        {
            end3 = int.MaxValue;
        }


        end = Mathf.Min(end1, end2, end3);
        string tag = eS.Substring(ndx + 1, end - ndx - 1);

        // search for this tag in eH. If it's not there, make it
        if (!eH.ContainsKey(tag))
        {
            eH[tag] = new XnXMLHashList();
        }
        // Create a hashtable to contain this tag's information
        XnXMLHashList arrL = eH[tag] as XnXMLHashList;
        //int thisHashIndex = arrL.Count;
        XnXMLHashtable thisHash = new XnXMLHashtable();

        arrL.Add(thisHash);

        // Pull the attributes string
        string atts = "";

        if (end1 < end3)
        {
            atts = eS.Substring(end1, end3 - end1);
        }
        // Parse the attributes, which are all guaranteed to be strings
        string att, val;
        int    eqNdx, spNdx;

        while (atts.Length > 0)
        {
            atts  = atts.Trim();
            eqNdx = atts.IndexOf("=");
            if (eqNdx == -1)
            {
                break;
            }
            //att = "@"+atts.Substring(0,eqNdx);
            att   = atts.Substring(0, eqNdx);
            spNdx = atts.IndexOf(" ", eqNdx);
            if (spNdx == -1)               // This is the last attribute and doesn't have a space after it
            {
                val = atts.Substring(eqNdx + 1);
                if (val[val.Length - 1] == '/')                 // If the trailing / from /> was caught, remove it
                {
                    val = val.Substring(0, val.Length - 1);
                }
                atts = "";
            }
            else                 // This attribute has a space after it
            {
                val  = atts.Substring(eqNdx + 1, spNdx - eqNdx - 2);
                atts = atts.Substring(spNdx);
            }
            val = val.Trim('\"');
            //thisHash[att] = val; // All attributes have to be unique, so this should be okay.
            thisHash.attSet(att, val);
        }


        // Pull the subs, which is everything contained by this tag but exclusing the tags on either side (e.g. <tag att="hi">.....subs.....</tag>)
        string subs           = "";
        string leftoverString = "";
        // singleLine means this doesn't have a separate closing tag (e.g. <tag att="hi" />)
        bool singleLine = (end2 == end3 - 1); // ? true : false;

        if (!singleLine)                      // This is a multiline tag (e.g. <tag> ....  </tag>)
        // find the closing tag
        {
            int close = eS.IndexOf("</" + tag + ">");
// TODO: Should this do something more if there is no closing tag?
            if (close == -1)
            {
                Debug.Log("XMLReader ERROR: XML not well formed. Closing tag </" + tag + "> missing.");
                return("");
            }
            subs           = eS.Substring(end3 + 1, close - end3 - 1);
            leftoverString = eS.Substring(eS.IndexOf(">", close) + 1);
        }
        else
        {
            leftoverString = eS.Substring(end3 + 1);
        }

        subs = subs.Trim();
        // Call Parse if this contains subs
        if (subs.Length > 0)
        {
            Parse(subs, thisHash);
        }

        // Trim and return the leftover string
        leftoverString = leftoverString.Trim();
        return(leftoverString);
    }
    string ParseTag(string eS, XnXMLHashtable eH)
    {
        int ndx = eS.IndexOf("<");
        int end, end1, end2, end3;

        if (ndx == -1)
        {
            end3 = eS.IndexOf(">");
            if (end3 == -1)
            {
                eS = eS.Trim();

                eH.text = eS;
            }
            return("");
        }

        if (eS[ndx + 1] == '?')
        {
            int    ndx2   = eS.IndexOf("?>");
            string header = eS.Substring(ndx, ndx2 - ndx + 2);

            eH.header = header;
            return(eS.Substring(ndx2 + 2));
        }

        if (eS[ndx + 1] == '!')
        {
            int    ndx2    = eS.IndexOf("-->");
            string comment = eS.Substring(ndx, ndx2 - ndx + 3);
            if (SHOW_COMMENTS)
            {
                Debug.Log("XMl Comment: " + comment);
            }

            return(eS.Substring(ndx2 + 3));
        }


        end1 = eS.IndexOf(" ", ndx);
        end2 = eS.IndexOf("/", ndx);
        end3 = eS.IndexOf(">", ndx);
        if (end1 == -1)
        {
            end1 = int.MaxValue;
        }
        if (end2 == -1)
        {
            end2 = int.MaxValue;
        }
        if (end3 == -1)
        {
            end3 = int.MaxValue;
        }


        end = Mathf.Min(end1, end2, end3);
        string tag = eS.Substring(ndx + 1, end - ndx - 1);


        if (!eH.ContainsKey(tag))
        {
            eH[tag] = new XnXMLHashList();
        }

        XnXMLHashList arrL = eH[tag] as XnXMLHashList;

        XnXMLHashtable thisHash = new XnXMLHashtable();

        arrL.Add(thisHash);


        string atts = "";

        if (end1 < end3)
        {
            atts = eS.Substring(end1, end3 - end1);
        }
        string att, val;
        int    eqNdx, spNdx;

        while (atts.Length > 0)
        {
            atts  = atts.Trim();
            eqNdx = atts.IndexOf("=");
            if (eqNdx == -1)
            {
                break;
            }

            att   = atts.Substring(0, eqNdx);
            spNdx = atts.IndexOf(" ", eqNdx);
            if (spNdx == -1)
            {
                val = atts.Substring(eqNdx + 1);
                if (val[val.Length - 1] == '/')
                {
                    val = val.Substring(0, val.Length - 1);
                }
                atts = "";
            }
            else
            {
                val  = atts.Substring(eqNdx + 1, spNdx - eqNdx - 2);
                atts = atts.Substring(spNdx);
            }
            val = val.Trim('\"');

            thisHash.attSet(att, val);
        }


        string subs           = "";
        string leftoverString = "";

        bool singleLine = (end2 == end3 - 1);      // ? true : false;

        if (!singleLine)
        {
            int close = eS.IndexOf("</" + tag + ">");

            if (close == -1)
            {
                Debug.Log("XMLReader ERROR: XML not well formed. Closing tag </" + tag + "> missing.");
                return("");
            }
            subs           = eS.Substring(end3 + 1, close - end3 - 1);
            leftoverString = eS.Substring(eS.IndexOf(">", close) + 1);
        }
        else
        {
            leftoverString = eS.Substring(end3 + 1);
        }

        subs = subs.Trim();

        if (subs.Length > 0)
        {
            Parse(subs, thisHash);
        }


        leftoverString = leftoverString.Trim();
        return(leftoverString);
    }
    // This function parses a single tag and calls Parse() if it encounters subtags
    string ParseTag(string eS, XnXMLHashtable eH)
    {
        // search for "<"
        int ndx = eS.IndexOf("<");
        int end, end1, end2, end3;
        if (ndx == -1) {
            // It's possible that this is just a string (e.g. <someTagTheStringIsInside>string</someTagTheStringIsInside>)
            end3 = eS.IndexOf(">"); // This closes a standard tag; look for the closing tag
            if (end3 == -1) {
                // In that case, we just need to add an @ key/value to the hashtable
                eS = eS.Trim(); // I think this is redundant
                //eH["@"] = eS;
                eH.text = eS;
            }
            return(""); // We're done with this tag
        }
        // Ignore this if it is just an XML header (e.g. <?xml version="1.0"?>)
        if (eS[ndx+1] == '?') {
            // search for the closing tag of this header
            int ndx2 = eS.IndexOf("?>");
            string header = eS.Substring(ndx, ndx2-ndx+2);
            //eH["@XML_Header"] = header;
            eH.header = header;
            return(eS.Substring(ndx2+2));
        }
        // Ignore this if it is an XML comment (e.g. <!-- Comment text -->)
        if (eS[ndx+1] == '!') {
            // search for the closing tag of this header
            int ndx2 = eS.IndexOf("-->");
            string comment = eS.Substring(ndx, ndx2-ndx+3);
            if (SHOW_COMMENTS) Debug.Log("XMl Comment: "+comment);
            //eH["@XML_Header"] = header;
            return(eS.Substring(ndx2+3));
        }

        // Find the end of the tag name
                                        // For the next few comments, this is what happens when this character is the first one found after the beginning of the tag
        end1 = eS.IndexOf(" ", ndx);	// This means that we'll have attributes
        end2 = eS.IndexOf("/", ndx);	// Immediately closes the tag,
        end3 = eS.IndexOf(">", ndx);	// This closes a standard tag; look for the closing tag
        if (end1 == -1) end1 = int.MaxValue;
        if (end2 == -1) end2 = int.MaxValue;
        if (end3 == -1) end3 = int.MaxValue;

        end = Mathf.Min(end1, end2, end3);
        string tag = eS.Substring(ndx+1, end-ndx-1);

        // search for this tag in eH. If it's not there, make it
        if (!eH.ContainsKey(tag)) {
            eH[tag] = new XnXMLHashList();
        }
        // Create a hashtable to contain this tag's information
        XnXMLHashList arrL = eH[tag] as XnXMLHashList;
        //int thisHashIndex = arrL.Count;
        XnXMLHashtable thisHash = new XnXMLHashtable();
        arrL.Add(thisHash);

        // Pull the attributes string
        string atts = "";
        if (end1 < end3) {
            atts = eS.Substring(end1, end3-end1);
        }
        // Parse the attributes, which are all guaranteed to be strings
        string att, val;
        int eqNdx, spNdx;
        while (atts.Length > 0) {
            atts = atts.Trim();
            eqNdx = atts.IndexOf("=");
            if (eqNdx == -1) break;
            //att = "@"+atts.Substring(0,eqNdx);
            att = atts.Substring(0,eqNdx);
            spNdx = atts.IndexOf(" ",eqNdx);
            if (spNdx == -1) { // This is the last attribute and doesn't have a space after it
                val = atts.Substring(eqNdx+1);
                if (val[val.Length-1] == '/') { // If the trailing / from /> was caught, remove it
                    val = val.Substring(0,val.Length-1);
                }
                atts = "";
            } else { // This attribute has a space after it
                val = atts.Substring(eqNdx+1, spNdx - eqNdx - 2);
                atts = atts.Substring(spNdx);
            }
            val = val.Trim('\"');
            //thisHash[att] = val; // All attributes have to be unique, so this should be okay.
            thisHash.attSet(att, val);
        }

        // Pull the subs, which is everything contained by this tag but exclusing the tags on either side (e.g. <tag att="hi">.....subs.....</tag>)
        string subs = "";
        string leftoverString = "";
        // singleLine means this doesn't have a separate closing tag (e.g. <tag att="hi" />)
        bool singleLine = (end2 == end3-1);// ? true : false;
        if (!singleLine) { // This is a multiline tag (e.g. <tag> ....  </tag>)
            // find the closing tag
            int close = eS.IndexOf("</"+tag+">");
        // TODO: Should this do something more if there is no closing tag?
            if (close == -1) {
                Debug.Log("XMLReader ERROR: XML not well formed. Closing tag </"+tag+"> missing.");
                return("");
            }
            subs = eS.Substring(end3+1, close-end3-1);
            leftoverString = eS.Substring( eS.IndexOf(">",close)+1 );
        } else {
            leftoverString = eS.Substring(end3+1);
        }

        subs = subs.Trim();
        // Call Parse if this contains subs
        if (subs.Length > 0) {
            Parse(subs, thisHash);
        }

        // Trim and return the leftover string
        leftoverString = leftoverString.Trim();
        return(leftoverString);
    }