@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; }
@MayBeExcluded public static String arrayToXml(Object[] a, String id) { String xml = "<a" + id + ">"; for (int i = 0; i < a.length; i++) { if (StrictWeb.jsTypeOf(a[i]) != "function") xml += objectToXml(a[i], null); } return xml + "</a>"; }
@MayBeExcluded public static String objectToXml(Object obj, String _id) { String id = (_id != null ? " id=\"" + _id + "\"" : ""); if (StrictWeb.jsTypeOf(obj) == "undefined") return "<null" + id + "/>"; if (StrictWeb.jsTypeOf(obj) == "string") return "<s" + id + ">" + StrictWeb.toHTML((String) obj) + "</s>"; if (StrictWeb.jsTypeOf(obj) == "boolean") return ((Boolean) obj) ? "<b" + id + ">1</b>" : "<b" + id + ">0</b>"; if (StrictWeb.jsTypeOf(obj) == "number") return "<n" + id + ">" + obj + "</n>"; if (StrictWeb.jsTypeOf(obj) == "object") { if (obj == null) return "<null" + id + "/>"; if (StrictWeb.isEnum(obj)) return "<e" + id + ">" + obj.toString() + "</e>"; if (StrictWeb.isInstanceOfDate(obj)) return "<d" + id + ">" + StrictWeb.dateToStringSmart((Date) obj) + "</d>"; if (StrictWeb.isInstanceOfArray(obj)) return arrayToXml((Object[]) obj, id); if (StrictWeb.isInstanceOfNode(obj)) { return "<form" + id + ">" + formToXml((Node) obj) + "</form>"; } Map<String, String> map = (Map<String, String>) obj; String xml = "<o" + id + ">"; for (String key : map.keySet()) { String val = map.get(key); if (StrictWeb.jsTypeOf(val) != "function") xml += objectToXml(val, key); } return xml + "</o>"; } return "<" + StrictWeb.jsTypeOf(obj) + "/>"; }
@MayBeExcluded public static String objectToJson(Object obj) { if (StrictWeb.jsTypeOf(obj) == "undefined") return "null"; if (StrictWeb.jsTypeOf(obj) == "string") return '"' + StrictWeb.toJSON((String) obj) + '"'; if (StrictWeb.jsTypeOf(obj) == "boolean") return ((Boolean) obj) ? "1" : "0"; if (StrictWeb.jsTypeOf(obj) == "number") return obj.toString(); if (StrictWeb.jsTypeOf(obj) == "object") { if (obj == null) return "null"; if (StrictWeb.isEnum(obj)) return obj.toString(); if (StrictWeb.isInstanceOfDate(obj)) return StrictWeb.dateToStringSmart((Date) obj); if (StrictWeb.isInstanceOfArray(obj)) return arrayToJson((Object[]) obj); if (StrictWeb.isInstanceOfNode(obj)) { String json = formToJson((Node) obj); if (json != null && json.length() > 0 && json.charAt(0) == '{') return "[" + json + "]"; return "{" + json + "}"; } Map<String, String> map = (Map<String, String>) obj; String json = ""; for (String key : map.keySet()) { String val = map.get(key); if (StrictWeb.jsTypeOf(val) == "function") continue; if (json.length() > 0) json += ","; json += objectToJson(key) + ":" + objectToJson(val); } return "{" + json + "}"; } return nativeJsThrow("Unknown object type: " + StrictWeb.jsTypeOf(obj)); }
@MayBeExcluded public static String arrayToJson(Object[] a) { if (a == null) return "null"; String json = ""; for (int i = 0; i < a.length; i++) { Object val = a[i]; if (StrictWeb.jsTypeOf(val) == "function") continue; if (json.length() > 0) json += ","; json += objectToJson(val); } return "[" + json + "]"; }
@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; }