Exemplo n.º 1
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");
                }
            }
        }
Exemplo n.º 2
0
 ////////////////////////////////////////////////////////////////////////////
 //--------------------------------- REVISIONS ------------------------------
 // Date       Name                 Tracking #         Description
 // ---------  -------------------  -------------      ----------------------
 // 07JUN2009  James Shen                 	          Initial Creation
 ////////////////////////////////////////////////////////////////////////////
 /**
  * Construct a JSONObject from a subset of another JSONObject.
  * An array of strings is used to identify the keys that should be copied.
  * Missing keys are ignored.
  * @param jo A JSONObject.
  * @param sa An array of strings.
  * @exception JSONException If a value is a non-finite number.
  */
 public JSONObject(JSONObject jo, string[] sa)
     : this()
 {
     for (var i = 0; i < sa.Length; i += 1)
     {
         PutOpt(sa[i], jo.Opt(sa[i]));
     }
 }