Ejemplo n.º 1
0
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------- REVISIONS ------------------------------
        // Date       Name                 Tracking #         Description
        // ---------  -------------------  -------------      ----------------------
        // 07JUN2009  James Shen                              Initial Creation
        ////////////////////////////////////////////////////////////////////////////

        /**
         * Convert a well-formed (but not necessarily valid) JSONXML str into a
         * JSONObject. Some information may be lost in this transformation
         * because JSON is a data format and JSONXML is a document format. JSONXML uses
         * elements, attributes, and content text, while JSON uses unordered
         * collections of name/value pairs and arrays of values. JSON does not
         * does not like to distinguish between elements and attributes.
         * Sequences of similar elements are represented as JSONArrays. Content
         * text may be placed in a "content" member. Comments, prologs, DTDs, and
         * <code>&lt;[ [ ]]></code> are ignored.
         * @param str The source str.
         * @return A JSONObject containing the structured data from the JSONXML str.
         * @
         */
        public static JSONObject ToJSONObject(string str)
        {
            var o = new JSONObject();
            var x = new JSONXMLTokener(str);

            while (x.More())
            {
                x.SkipPast("<");
                Parse(x, o, null);
            }
            return(o);
        }
Ejemplo n.º 2
0
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------- REVISIONS ------------------------------
        // Date       Name                 Tracking #         Description
        // ---------  -------------------  -------------      ----------------------
        // 07JUN2009  James Shen                              Initial Creation
        ////////////////////////////////////////////////////////////////////////////

        /**
         * Scan the content following the named tag, attaching it to the context.
         * @param x       The JSONXMLTokener containing the source str.
         * @param context The JSONObject that will include the new material.
         * @param name    The tag name.
         * @return true if the close tag is processed.
         * @
         */
        private static bool Parse(JSONXMLTokener x, JSONObject context,
                                  string name)
        {
            JSONObject o;
            string     s;

            // Test for and skip past these forms:
            //      <!-- ... -->
            //      <!   ...   >
            //      <![  ... ]]>
            //      <?   ...  ?>
            // Report errors for these forms:
            //      <>
            //      <=
            //      <<

            var t = x.NextToken();

            // <!

            if (t == (object)BANG)
            {
                var c = x.Next();
                if (c == '-')
                {
                    if (x.Next() == '-')
                    {
                        x.SkipPast("-->");
                        return(false);
                    }
                    x.Back();
                }
                else if (c == '[')
                {
                    t = x.NextToken();
                    if (t.Equals("CDATA"))
                    {
                        if (x.Next() == '[')
                        {
                            s = x.NextCDATA();
                            if (s.Length > 0)
                            {
                                context.Accumulate("content", s);
                            }
                            return(false);
                        }
                    }
                    throw x.SyntaxError("Expected 'CDATA['");
                }
                var i = 1;
                do
                {
                    t = x.NextMeta();
                    if (t == null)
                    {
                        throw x.SyntaxError("Missing '>' after '<!'.");
                    }
                    if (t == (object)LT)
                    {
                        i += 1;
                    }
                    else if (t == (object)GT)
                    {
                        i -= 1;
                    }
                } while (i > 0);
                return(false);
            }
            if (t == (object)QUEST)
            {
                // <?

                x.SkipPast("?>");
                return(false);
            }
            if (t == (object)SLASH)
            {
                // Close tag </

                if (name == null || !x.NextToken().Equals(name))
                {
                    throw x.SyntaxError("Mismatched close tag");
                }
                if (x.NextToken() != (object)GT)
                {
                    throw x.SyntaxError("Misshaped close tag");
                }
                return(true);
            }
            if (t is char)
            {
                throw x.SyntaxError("Misshaped tag");

                // Open tag <
            }
            string n = (string)t;

            t = null;
            o = new JSONObject();
            for (; ;)
            {
                if (t == null)
                {
                    t = x.NextToken();
                }

                // attribute = value

                if (t is string)
                {
                    s = (string)t;
                    t = x.NextToken();
                    if (t == (object)EQ)
                    {
                        t = x.NextToken();
                        if (!(t is string))
                        {
                            throw x.SyntaxError("Missing value");
                        }
                        o.Accumulate(s, t);
                        t = null;
                    }
                    else
                    {
                        o.Accumulate(s, "");
                    }

                    // Empty tag <.../>
                }
                else if (t == (object)SLASH)
                {
                    if (x.NextToken() != (object)GT)
                    {
                        throw x.SyntaxError("Misshaped tag");
                    }
                    context.Accumulate(n, o);
                    return(false);

                    // Content, between <...> and </...>
                }
                else if (t == (object)GT)
                {
                    for (; ;)
                    {
                        t = x.NextContent();
                        if (t == null)
                        {
                            if (name != null)
                            {
                                throw x.SyntaxError("Unclosed tag " + name);
                            }
                            return(false);
                        }
                        if (t is string)
                        {
                            s = (string)t;
                            if (s.Length > 0)
                            {
                                o.Accumulate("content", s);
                            }

                            // Nested element
                        }
                        else if (t == (object)LT)
                        {
                            if (Parse(x, o, n))
                            {
                                if (o.Length() == 0)
                                {
                                    context.Accumulate(n, "");
                                }
                                else if (o.Length() == 1 &&
                                         o.Opt("content") != null)
                                {
                                    context.Accumulate(n, o.Opt("content"));
                                }
                                else
                                {
                                    context.Accumulate(n, o);
                                }
                                return(false);
                            }
                        }
                    }
                }
                else
                {
                    throw x.SyntaxError("Misshaped tag");
                }
            }
        }
Ejemplo n.º 3
0
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------- REVISIONS ------------------------------
        // Date       Name                 Tracking #         Description
        // ---------  -------------------  -------------      ----------------------
        // 07JUN2009  James Shen                 	          Initial Creation
        ////////////////////////////////////////////////////////////////////////////
        /**
         * Scan the content following the named tag, attaching it to the context.
         * @param x       The JSONXMLTokener containing the source str.
         * @param context The JSONObject that will include the new material.
         * @param name    The tag name.
         * @return true if the close tag is processed.
         * @
         */
        private static bool Parse(JSONXMLTokener x, JSONObject context,
                                     string name)
        {
            JSONObject o;
            string s;

            // Test for and skip past these forms:
            //      <!-- ... -->
            //      <!   ...   >
            //      <![  ... ]]>
            //      <?   ...  ?>
            // Report errors for these forms:
            //      <>
            //      <=
            //      <<

            var t = x.NextToken();

            // <!

            if (t == (object)BANG)
            {
                var c = x.Next();
                if (c == '-')
                {
                    if (x.Next() == '-')
                    {
                        x.SkipPast("-->");
                        return false;
                    }
                    x.Back();
                }
                else if (c == '[')
                {
                    t = x.NextToken();
                    if (t.Equals("CDATA"))
                    {
                        if (x.Next() == '[')
                        {
                            s = x.NextCDATA();
                            if (s.Length > 0)
                            {
                                context.Accumulate("content", s);
                            }
                            return false;
                        }
                    }
                    throw x.SyntaxError("Expected 'CDATA['");
                }
                var i = 1;
                do
                {
                    t = x.NextMeta();
                    if (t == null)
                    {
                        throw x.SyntaxError("Missing '>' after '<!'.");
                    }
                    if (t == (object)LT)
                    {
                        i += 1;
                    }
                    else if (t == (object)GT)
                    {
                        i -= 1;
                    }
                } while (i > 0);
                return false;
            }
            if (t == (object)QUEST)
            {

                // <?

                x.SkipPast("?>");
                return false;
            }
            if (t == (object)SLASH)
            {

                // Close tag </

                if (name == null || !x.NextToken().Equals(name))
                {
                    throw x.SyntaxError("Mismatched close tag");
                }
                if (x.NextToken() != (object)GT)
                {
                    throw x.SyntaxError("Misshaped close tag");
                }
                return true;

            }
            if (t is char)
            {
                throw x.SyntaxError("Misshaped tag");

                // Open tag <

            }
            string n = (string)t;
            t = null;
            o = new JSONObject();
            for (; ; )
            {
                if (t == null)
                {
                    t = x.NextToken();
                }

                // attribute = value

                if (t is string)
                {
                    s = (string)t;
                    t = x.NextToken();
                    if (t == (object)EQ)
                    {
                        t = x.NextToken();
                        if (!(t is string))
                        {
                            throw x.SyntaxError("Missing value");
                        }
                        o.Accumulate(s, t);
                        t = null;
                    }
                    else
                    {
                        o.Accumulate(s, "");
                    }

                    // Empty tag <.../>

                }
                else if (t == (object)SLASH)
                {
                    if (x.NextToken() != (object)GT)
                    {
                        throw x.SyntaxError("Misshaped tag");
                    }
                    context.Accumulate(n, o);
                    return false;

                    // Content, between <...> and </...>

                }
                else if (t == (object)GT)
                {
                    for (; ; )
                    {
                        t = x.NextContent();
                        if (t == null)
                        {
                            if (name != null)
                            {
                                throw x.SyntaxError("Unclosed tag " + name);
                            }
                            return false;
                        }
                        if (t is string)
                        {
                            s = (string)t;
                            if (s.Length > 0)
                            {
                                o.Accumulate("content", s);
                            }

                            // Nested element

                        }
                        else if (t == (object)LT)
                        {
                            if (Parse(x, o, n))
                            {
                                if (o.Length() == 0)
                                {
                                    context.Accumulate(n, "");
                                }
                                else if (o.Length() == 1 &&
                                         o.Opt("content") != null)
                                {
                                    context.Accumulate(n, o.Opt("content"));
                                }
                                else
                                {
                                    context.Accumulate(n, o);
                                }
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    throw x.SyntaxError("Misshaped tag");
                }
            }
        }
Ejemplo n.º 4
0
 ////////////////////////////////////////////////////////////////////////////
 //--------------------------------- REVISIONS ------------------------------
 // Date       Name                 Tracking #         Description
 // ---------  -------------------  -------------      ----------------------
 // 07JUN2009  James Shen                 	          Initial Creation
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Convert a well-formed (but not necessarily valid) JSONXML str into a
  * JSONObject. Some information may be lost in this transformation
  * because JSON is a data format and JSONXML is a document format. JSONXML uses
  * elements, attributes, and content text, while JSON uses unordered
  * collections of name/value pairs and arrays of values. JSON does not
  * does not like to distinguish between elements and attributes.
  * Sequences of similar elements are represented as JSONArrays. Content
  * text may be placed in a "content" member. Comments, prologs, DTDs, and
  * <code>&lt;[ [ ]]></code> are ignored.
  * @param str The source str.
  * @return A JSONObject containing the structured data from the JSONXML str.
  * @
  */
 public static JSONObject ToJSONObject(string str)
 {
     var o = new JSONObject();
     var x = new JSONXMLTokener(str);
     while (x.More())
     {
         x.SkipPast("<");
         Parse(x, o, null);
     }
     return o;
 }