Exemple #1
0
  @MayBeExcluded
  public static String formToXml(Node start) {
    String xml = "";

    if (start.field == DOMBuilder.DISABLED) return xml;

    if (start.field != null) {
      if (StrictWeb.jsTypeOf(start.field) == "string") {
        xml = "<f id=\"" + start.field + "\">";
      } else xml = "<f>";
    }

    for (Node el : start.childNodes) {
      if (el.field == DOMBuilder.DISABLED) continue;
      if (((Boolean) (Object) el.id || (Boolean) (Object) el.name)
          && (el.tagName == "INPUT" || el.tagName == "SELECT" || el.tagName == "TEXTAREA")) {
        if (el.type == "radio" && !el.checked) continue;

        xml += "<f id=\"" + ((Boolean) (Object) el.id || (Boolean) (Object) el.name) + "\">";

        if (el.type == "checkbox") xml += el.checked ? "1" : "0";
        else xml += StrictWeb.toHTML((String) el.value);

        xml += "</f>";

      } else if (el.className != null && el.className.indexOf(FIELD_MULTISELECT) >= 0) {
        final List<Object> val = new ArrayList<Object>();
        NodeBuilder.wrap(el)
            .forEachSubchild(
                new CommonDelegate<Boolean, Node>() {
                  public Boolean delegate(Node n) {
                    if (n.field == DOMBuilder.DISABLED) return false;
                    if (n.tagName == "INPUT" || n.tagName == "SELECT") {
                      if (n.type == "checkbox") {
                        if (n.checked)
                          val.add((Boolean) (Object) n.id || (Boolean) (Object) n.name);
                      } else {
                        val.add((String) n.value);
                      }
                    }
                    return true;
                  }
                });

        xml +=
            "<ms id=\""
                + el.field
                + "\">"
                + (val.size() > 0 ? "<q>" : "")
                + StrictWeb.jsJoinList(val, "</q><q>")
                + (val.size() > 0 ? "</q>" : "")
                + "</ms>";
      } else xml += formToXml(el);
    }

    if (start.field != null) xml += "</f>";

    return xml;
  }
Exemple #2
0
  @MayBeExcluded
  public static String formToJson(Node start) {
    if (start.field == DOMBuilder.DISABLED) return "";

    String json = "";
    boolean asArray = false;

    for (Node el : start.childNodes) {
      if (el.field == DOMBuilder.DISABLED) continue;
      if (((Boolean) (Object) el.id || (Boolean) (Object) el.name)
          && (el.tagName == "INPUT" || el.tagName == "SELECT" || el.tagName == "TEXTAREA")) {
        if (el.type == "radio" && !el.checked) continue;
        if (json.length() > 0) json += ",";
        if (asArray) throw new RuntimeException("Form returned as array, not at map");
        json += objectToJson((Boolean) (Object) el.id || (Boolean) (Object) el.name) + ":";

        if (el.type == "checkbox") json += el.checked ? "1" : "0";
        else json += objectToJson(el.value);

      } else if (el.className != null && el.className.indexOf(FIELD_MULTISELECT) >= 0) {
        final List<Object> val = new ArrayList<Object>();
        NodeBuilder.wrap(el)
            .forEachSubchild(
                new CommonDelegate<Boolean, Node>() {
                  public Boolean delegate(Node n) {
                    if (n.field == DOMBuilder.DISABLED) return false;
                    if (n.tagName == "INPUT" || n.tagName == "SELECT") {
                      if (n.type == "checkbox") {
                        if (n.checked)
                          val.add((Boolean) (Object) n.id || (Boolean) (Object) n.name);
                      } else {
                        val.add((String) n.value);
                      }
                    }
                    return true;
                  }
                });

        if (json.length() > 0) json += ",";
        if (asArray) nativeJsThrow("Form returned as array, not as map 1");
        json += objectToJson(el.field) + ":" + arrayToJson((Object[]) (Object) val);
      } else {
        String childJson = formToJson(el);
        if (childJson != null && childJson.length() > 0) {
          if (childJson.charAt(0) == '{') asArray = true;
          else if (asArray) nativeJsThrow("Form returned as array, not as map 2");

          if (json.length() > 0) json += ",";
          json += childJson;
        }
      }
    }

    if (start.field != null) {
      String bra = asArray ? "[" : "{";
      String ket = asArray ? "]" : "}";

      if (StrictWeb.jsTypeOf(start.field) == "string") {
        return objectToJson(start.field) + ":" + bra + json + ket;
      } else return bra + json + ket;
    }

    return json;
  }