private void addTagsToProperties(MethodInfo mi, JSONObject propJ) throws JSONException {
    // create description object. description tag enables the visual tools to display description of
    // keys/values
    // of a map property, items of a list, properties within a complex type.
    JSONObject descriptionObj = new JSONObject();
    if (mi.comment != null) {
      descriptionObj.put("$", mi.comment);
    }
    for (Map.Entry<String, String> descEntry : mi.descriptions.entrySet()) {
      descriptionObj.put(descEntry.getKey(), descEntry.getValue());
    }
    if (descriptionObj.length() > 0) {
      propJ.put("descriptions", descriptionObj);
    }

    // create useSchema object. useSchema tag is added to enable visual tools to be able to render a
    // text field
    // as a dropdown with choices populated from the schema attached to the port.
    JSONObject useSchemaObj = new JSONObject();
    for (Map.Entry<String, String> useSchemaEntry : mi.useSchemas.entrySet()) {
      useSchemaObj.put(useSchemaEntry.getKey(), useSchemaEntry.getValue());
    }
    if (useSchemaObj.length() > 0) {
      propJ.put("useSchema", useSchemaObj);
    }
  }
 /** Creates a CookieList from an empty string. */
 @Test
 public void emptyStringCookieList() {
   String cookieStr = "";
   String expectedCookieStr = "";
   JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
   assertTrue(jsonObject.length() == 0);
 }
 /** Writes an object. */
 public void writeObject(JSONObject object) throws IOException {
   if (object.isUndefined()) writeString("undefined");
   else if (object.isNull()) writeString("null");
   else if (object.isArray()) {
     writeString("[");
     for (int i = 0; i < object.length(); i++) {
       writeObject(object.get(i));
       if (i < object.length() - 1) writeString(",");
     }
     writeString("]");
   } else if (object.isObject()) {
     writeString("{");
     int i = 0;
     int len = object.getMemberCount();
     for (String member : object.getMemberNames()) {
       writeString("\"");
       writeString(Common.withEscChars(member));
       writeString("\":");
       writeObject(object.get(member));
       if (i < len - 1) writeString(",");
       i++;
     }
     writeString("}");
   } else {
     Object value = object.getValue();
     if (value instanceof Boolean) writeString(object.getBoolean());
     else if (value instanceof Byte) writeString(object.getByte());
     else if (value instanceof Short) writeString(object.getShort());
     else if (value instanceof Integer) writeString(object.getInt());
     else if (value instanceof Float) writeString(object.getFloat());
     else if (value instanceof Long) writeString(object.getLong());
     else if (value instanceof Double) writeString(object.getDouble());
     else {
       writeString("\"");
       writeString(Common.withEscChars(object.getString()));
       writeString("\"");
     }
   }
 }
Example #4
0
 /**
  * Get an array of field names from a JSONObject.
  *
  * @return An array of field names, or null if there are no names.
  */
 public static String[] getNames(JSONObject jo) {
   int length = jo.length();
   if (length == 0) {
     return null;
   }
   Iterator iterator = jo.keys();
   String[] names = new String[length];
   int i = 0;
   while (iterator.hasNext()) {
     names[i] = (String) iterator.next();
     i += 1;
   }
   return names;
 }
Example #5
0
  /**
   * Scan the content following the named tag, attaching it to the context.
   *
   * @param x The XMLTokener containing the source string.
   * @param context The JSONObject that will include the new material.
   * @param name The tag name.
   * @return true if the close tag is processed.
   * @throws JSONException
   */
  private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException {
    char c;
    int i;
    JSONObject jsonobject = null;
    String string;
    String tagName;
    Object token;

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

    token = x.nextToken();

    // <!

    if (token == BANG) {
      c = x.next();
      if (c == '-') {
        if (x.next() == '-') {
          x.skipPast("-->");
          return false;
        }
        x.back();
      } else if (c == '[') {
        token = x.nextToken();
        if ("CDATA".equals(token)) {
          if (x.next() == '[') {
            string = x.nextCDATA();
            if (string.length() > 0) {
              context.accumulate("content", string);
            }
            return false;
          }
        }
        throw x.syntaxError("Expected 'CDATA['");
      }
      i = 1;
      do {
        token = x.nextMeta();
        if (token == null) {
          throw x.syntaxError("Missing '>' after '<!'.");
        } else if (token == LT) {
          i += 1;
        } else if (token == GT) {
          i -= 1;
        }
      } while (i > 0);
      return false;
    } else if (token == QUEST) {

      // <?

      x.skipPast("?>");
      return false;
    } else if (token == SLASH) {

      // Close tag </

      token = x.nextToken();
      if (name == null) {
        throw x.syntaxError("Mismatched close tag " + token);
      }
      if (!token.equals(name)) {
        throw x.syntaxError("Mismatched " + name + " and " + token);
      }
      if (x.nextToken() != GT) {
        throw x.syntaxError("Misshaped close tag");
      }
      return true;

    } else if (token instanceof Character) {
      throw x.syntaxError("Misshaped tag");

      // Open tag <

    } else {
      tagName = (String) token;
      token = null;
      jsonobject = new JSONObject();
      for (; ; ) {
        if (token == null) {
          token = x.nextToken();
        }

        // attribute = value

        if (token instanceof String) {
          string = (String) token;
          token = x.nextToken();
          if (token == EQ) {
            token = x.nextToken();
            if (!(token instanceof String)) {
              throw x.syntaxError("Missing value");
            }
            jsonobject.accumulate(string, XML.stringToValue((String) token));
            token = null;
          } else {
            jsonobject.accumulate(string, "");
          }

          // Empty tag <.../>

        } else if (token == SLASH) {
          if (x.nextToken() != GT) {
            throw x.syntaxError("Misshaped tag");
          }
          if (jsonobject.length() > 0) {
            context.accumulate(tagName, jsonobject);
          } else {
            context.accumulate(tagName, "");
          }
          return false;

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

        } else if (token == GT) {
          for (; ; ) {
            token = x.nextContent();
            if (token == null) {
              if (tagName != null) {
                throw x.syntaxError("Unclosed tag " + tagName);
              }
              return false;
            } else if (token instanceof String) {
              string = (String) token;
              if (string.length() > 0) {
                jsonobject.accumulate("content", XML.stringToValue(string));
              }

              // Nested element

            } else if (token == LT) {
              if (parse(x, jsonobject, tagName)) {
                if (jsonobject.length() == 0) {
                  context.accumulate(tagName, "");
                } else if (jsonobject.length() == 1 && jsonobject.opt("content") != null) {
                  context.accumulate(tagName, jsonobject.opt("content"));
                } else {
                  context.accumulate(tagName, jsonobject);
                }
                return false;
              }
            }
          }
        } else {
          throw x.syntaxError("Misshaped tag");
        }
      }
    }
  }
Example #6
0
  /**
   * Parse XML values and store them in a JSONArray.
   *
   * @param x The XMLTokener containing the source string.
   * @param arrayForm true if array form, false if object form.
   * @param ja The JSONArray that is containing the current tag or null if we are at the outermost
   *     level.
   * @return A JSONArray if the value is the outermost tag, otherwise null.
   * @throws JSONException
   */
  private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja) throws JSONException {
    String attribute;
    char c;
    String closeTag = null;
    int i;
    JSONArray newja = null;
    JSONObject newjo = null;
    Object token;
    String tagName = null;

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

    while (true) {
      if (!x.more()) {
        throw x.syntaxError("Bad XML");
      }
      token = x.nextContent();
      if (token == XML.LT) {
        token = x.nextToken();
        if (token instanceof Character) {
          if (token == XML.SLASH) {

            // Close tag </

            token = x.nextToken();
            if (!(token instanceof String)) {
              throw new JSONException("Expected a closing name instead of '" + token + "'.");
            }
            if (x.nextToken() != XML.GT) {
              throw x.syntaxError("Misshaped close tag");
            }
            return token;
          } else if (token == XML.BANG) {

            // <!

            c = x.next();
            if (c == '-') {
              if (x.next() == '-') {
                x.skipPast("-->");
              } else {
                x.back();
              }
            } else if (c == '[') {
              token = x.nextToken();
              if (token.equals("CDATA") && x.next() == '[') {
                if (ja != null) {
                  ja.put(x.nextCDATA());
                }
              } else {
                throw x.syntaxError("Expected 'CDATA['");
              }
            } else {
              i = 1;
              do {
                token = x.nextMeta();
                if (token == null) {
                  throw x.syntaxError("Missing '>' after '<!'.");
                } else if (token == XML.LT) {
                  i += 1;
                } else if (token == XML.GT) {
                  i -= 1;
                }
              } while (i > 0);
            }
          } else if (token == XML.QUEST) {

            // <?

            x.skipPast("?>");
          } else {
            throw x.syntaxError("Misshaped tag");
          }

          // Open tag <

        } else {
          if (!(token instanceof String)) {
            throw x.syntaxError("Bad tagName '" + token + "'.");
          }
          tagName = (String) token;
          newja = new JSONArray();
          newjo = new JSONObject();
          if (arrayForm) {
            newja.put(tagName);
            if (ja != null) {
              ja.put(newja);
            }
          } else {
            newjo.put("tagName", tagName);
            if (ja != null) {
              ja.put(newjo);
            }
          }
          token = null;
          for (; ; ) {
            if (token == null) {
              token = x.nextToken();
            }
            if (token == null) {
              throw x.syntaxError("Misshaped tag");
            }
            if (!(token instanceof String)) {
              break;
            }

            // attribute = value

            attribute = (String) token;
            if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
              throw x.syntaxError("Reserved attribute.");
            }
            token = x.nextToken();
            if (token == XML.EQ) {
              token = x.nextToken();
              if (!(token instanceof String)) {
                throw x.syntaxError("Missing value");
              }
              newjo.accumulate(attribute, XML.stringToValue((String) token));
              token = null;
            } else {
              newjo.accumulate(attribute, "");
            }
          }
          if (arrayForm && newjo.length() > 0) {
            newja.put(newjo);
          }

          // Empty tag <.../>

          if (token == XML.SLASH) {
            if (x.nextToken() != XML.GT) {
              throw x.syntaxError("Misshaped tag");
            }
            if (ja == null) {
              if (arrayForm) {
                return newja;
              } else {
                return newjo;
              }
            }

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

          } else {
            if (token != XML.GT) {
              throw x.syntaxError("Misshaped tag");
            }
            closeTag = (String) parse(x, arrayForm, newja);
            if (closeTag != null) {
              if (!closeTag.equals(tagName)) {
                throw x.syntaxError("Mismatched '" + tagName + "' and '" + closeTag + "'");
              }
              tagName = null;
              if (!arrayForm && newja.length() > 0) {
                newjo.put("childNodes", newja);
              }
              if (ja == null) {
                if (arrayForm) {
                  return newja;
                } else {
                  return newjo;
                }
              }
            }
          }
        }
      } else {
        if (ja != null) {
          ja.put(token instanceof String ? XML.stringToValue((String) token) : token);
        }
      }
    }
  }
Example #7
0
  /**
   * Scan the content following the named tag, attaching it to the context.
   *
   * @param x The XMLTokener containing the source string.
   * @param context The JSONObject that will include the new material.
   * @param name The tag name.
   * @return true if the close tag is processed.
   * @throws JSONException
   */
  private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException {
    char c;
    int i;
    String n;
    JSONObject o = null;
    String s;
    Object t;

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

    t = x.nextToken();

    // <!

    if (t == BANG) {
      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['");
      }
      i = 1;
      do {
        t = x.nextMeta();
        if (t == null) {
          throw x.syntaxError("Missing '>' after '<!'.");
        } else if (t == LT) {
          i += 1;
        } else if (t == GT) {
          i -= 1;
        }
      } while (i > 0);
      return false;
    } else if (t == QUEST) {

      // <?

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

      // Close tag </

      t = x.nextToken();
      if (name == null) {
        throw x.syntaxError("Mismatched close tag" + t);
      }
      if (!t.equals(name)) {
        throw x.syntaxError("Mismatched " + name + " and " + t);
      }
      if (x.nextToken() != GT) {
        throw x.syntaxError("Misshaped close tag");
      }
      return true;

    } else if (t instanceof Character) {
      throw x.syntaxError("Misshaped tag");

      // Open tag <

    } else {
      n = (String) t;
      t = null;
      o = new JSONObject();
      for (; ; ) {
        if (t == null) {
          t = x.nextToken();
        }

        // attribute = value

        if (t instanceof String) {
          s = (String) t;
          t = x.nextToken();
          if (t == EQ) {
            t = x.nextToken();
            if (!(t instanceof String)) {
              throw x.syntaxError("Missing value");
            }
            o.accumulate(s, JSONObject.stringToValue((String) t));
            t = null;
          } else {
            o.accumulate(s, "");
          }

          // Empty tag <.../>

        } else if (t == SLASH) {
          if (x.nextToken() != GT) {
            throw x.syntaxError("Misshaped tag");
          }
          if (o.length() > 0) {
            context.accumulate(n, o);
          } else {
            context.accumulate(n, "");
          }
          return false;

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

        } else if (t == GT) {
          for (; ; ) {
            t = x.nextContent();
            if (t == null) {
              if (n != null) {
                throw x.syntaxError("Unclosed tag " + n);
              }
              return false;
            } else if (t instanceof String) {
              s = (String) t;
              if (s.length() > 0) {
                o.accumulate("content", JSONObject.stringToValue(s));
              }

              // Nested element

            } else if (t == 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");
        }
      }
    }
  }