示例#1
0
 /*     */ private static void stringify(JSONArray ja, StringBuffer b)
     /*     */ throws JSONException
       /*     */ {
   /* 243 */ b.append('<');
   /* 244 */ b.append(ja.get(0));
   /* 245 */ Object o = ja.opt(1);
   /*     */ int i;
   /*     */ int i;
   /* 246 */ if ((o instanceof JSONObject))
   /*     */ {
     /* 250 */ JSONObject jo = (JSONObject) o;
     /* 251 */ Iterator keys = jo.keys();
     /* 252 */ while (keys.hasNext()) {
       /* 253 */ String k = keys.next().toString();
       /* 254 */ Object v = jo.get(k).toString();
       /* 255 */ b.append(' ');
       /* 256 */ b.append(k);
       /* 257 */ b.append("=\"");
       /* 258 */ b.append(XML.escape((String) v));
       /* 259 */ b.append('"');
       /*     */ }
     /* 261 */ i = 2;
     /*     */ } else {
     /* 263 */ i = 1;
     /*     */ }
   /* 265 */ int len = ja.length();
   /*     */
   /* 267 */ if (i >= len) {
     /* 268 */ b.append("/>");
     /*     */ } else {
     /* 270 */ b.append('>');
     /* 271 */ while (i < len) {
       /* 272 */ Object v = ja.get(i);
       /* 273 */ if ((v instanceof JSONArray)) /* 274 */ stringify((JSONArray) v, b);
       /*     */ else {
         /* 276 */ b.append(XML.escape(v.toString()));
         /*     */ }
       /* 278 */ i++;
       /*     */ }
     /* 280 */ b.append("</");
     /* 281 */ b.append(ja.get(0));
     /* 282 */ b.append('>');
     /*     */ }
   /*     */ }
示例#2
0
  /**
   * Reverse the JSONML transformation, making an XML text from a JSONObject. The JSONObject must
   * contain a "tagName" property. If it has children, then it must have a "childNodes" property
   * containing an array of objects. The other properties are attributes with string values.
   *
   * @param jo A JSONObject.
   * @return An XML string.
   * @throws JSONException
   */
  public static String toString(JSONObject jo) throws JSONException {
    StringBuilder sb = new StringBuilder();
    int i;
    JSONArray ja;
    String key;
    Iterator<String> keys;
    int length;
    Object object;
    String tagName;
    String value;

    // Emit <tagName

    tagName = jo.optString("tagName");
    if (tagName == null) {
      return XML.escape(jo.toString());
    }
    XML.noSpace(tagName);
    tagName = XML.escape(tagName);
    sb.append('<');
    sb.append(tagName);

    // Emit the attributes

    keys = jo.keys();
    while (keys.hasNext()) {
      key = keys.next();
      if (!"tagName".equals(key) && !"childNodes".equals(key)) {
        XML.noSpace(key);
        value = jo.optString(key);
        if (value != null) {
          sb.append(' ');
          sb.append(XML.escape(key));
          sb.append('=');
          sb.append('"');
          sb.append(XML.escape(value));
          sb.append('"');
        }
      }
    }

    // Emit content in body

    ja = jo.optJSONArray("childNodes");
    if (ja == null) {
      sb.append('/');
      sb.append('>');
    } else {
      sb.append('>');
      length = ja.length();
      for (i = 0; i < length; i += 1) {
        object = ja.get(i);
        if (object != null) {
          if (object instanceof String) {
            sb.append(XML.escape(object.toString()));
          } else if (object instanceof JSONObject) {
            sb.append(toString((JSONObject) object));
          } else if (object instanceof JSONArray) {
            sb.append(toString((JSONArray) object));
          } else {
            sb.append(object.toString());
          }
        }
      }
      sb.append('<');
      sb.append('/');
      sb.append(tagName);
      sb.append('>');
    }
    return sb.toString();
  }
示例#3
0
  /**
   * Reverse the JSONML transformation, making an XML text from a JSONArray.
   *
   * @param ja A JSONArray.
   * @return An XML string.
   * @throws JSONException
   */
  public static String toString(JSONArray ja) throws JSONException {
    int i;
    JSONObject jo;
    String key;
    Iterator<String> keys;
    int length;
    Object object;
    StringBuilder sb = new StringBuilder();
    String tagName;
    String value;

    // Emit <tagName

    tagName = ja.getString(0);
    XML.noSpace(tagName);
    tagName = XML.escape(tagName);
    sb.append('<');
    sb.append(tagName);

    object = ja.opt(1);
    if (object instanceof JSONObject) {
      i = 2;
      jo = (JSONObject) object;

      // Emit the attributes

      keys = jo.keys();
      while (keys.hasNext()) {
        key = keys.next();
        XML.noSpace(key);
        value = jo.optString(key);
        if (value != null) {
          sb.append(' ');
          sb.append(XML.escape(key));
          sb.append('=');
          sb.append('"');
          sb.append(XML.escape(value));
          sb.append('"');
        }
      }
    } else {
      i = 1;
    }

    // Emit content in body

    length = ja.length();
    if (i >= length) {
      sb.append('/');
      sb.append('>');
    } else {
      sb.append('>');
      do {
        object = ja.get(i);
        i += 1;
        if (object != null) {
          if (object instanceof String) {
            sb.append(XML.escape(object.toString()));
          } else if (object instanceof JSONObject) {
            sb.append(toString((JSONObject) object));
          } else if (object instanceof JSONArray) {
            sb.append(toString((JSONArray) object));
          }
        }
      } while (i < length);
      sb.append('<');
      sb.append('/');
      sb.append(tagName);
      sb.append('>');
    }
    return sb.toString();
  }