/**\r
* Returns the documentElement.\r
*/\r
- Element getDocumentElement();\r
+ Element getRootElement();\r
+ \r
+ /**\r
+ * Appends a node\r
+ */\r
+ void append(XmlBuilder x);\r
+\r
+ /**\r
+ * Appends xml content\r
+ */\r
+ void append(String x);\r
}\r
//TODO empty document
protected GQuery g = $(JsUtils.parseXML("<root/>"));
- @SuppressWarnings("unchecked")
- public J parse(String xml) {
- return load(JsUtils.parseXML(xml));
+ public void append(String xml) {
+ g.append(JsUtils.parseXML(xml));
}
- @SuppressWarnings("unchecked")
- public J load(Object o) {
- assert o == null || o instanceof JavaScriptObject && JsUtils.isElement((JavaScriptObject)o) || o instanceof String;
- if (o != null && o instanceof String) {
- return parse((String)o);
- }
- if (o != null) {
- g=$((Element)o);
- }
- return (J)this;
+ public void append(XmlBuilder x) {
+ g.append(x.getRootElement());
}
- public String getText() {
- return g.text();
+ protected Boolean getBooleanBase(String n) {
+ return "true".equalsIgnoreCase(getStrBase(n));
}
- public void setText(String s) {
- g.text(s);
- }
-
- public String toString() {
- return g.toString();
- }
-
protected Element getElementBase(String n) {
return g.children(n).get(0);
}
return g.children(n).elements();
}
- public Element getDocumentElement() {
- return g.get(0);
- }
-
protected float getFloatBase(String s) {
String n = getStrBase(s).replaceAll("[^\\d\\-\\.]", "");
return n.isEmpty() ? 0 : Float.parseFloat(n);
}
- protected Boolean getBooleanBase(String n) {
- return "true".equalsIgnoreCase(getStrBase(n));
+ protected Properties getPropertiesBase(String n) {
+ // TODO:
+ return null;
+ }
+
+ public Element getRootElement() {
+ return g.get(0);
}
protected String getStrBase(String n) {
return g.attr(n);
}
- protected Properties getPropertiesBase(String n) {
- // TODO:
- return null;
+ public String getText() {
+ return g.text();
+ }
+
+ @SuppressWarnings("unchecked")
+ public J load(Object o) {
+ assert o == null || o instanceof JavaScriptObject && JsUtils.isElement((JavaScriptObject)o) || o instanceof String;
+ if (o != null && o instanceof String) {
+ return parse((String)o);
+ }
+ if (o != null) {
+ g=$((Element)o);
+ }
+ return (J)this;
+ }
+
+ @SuppressWarnings("unchecked")
+ public J parse(String xml) {
+ return load(JsUtils.parseXML(xml));
+ }
+
+ protected <T> void setArrayBase(String n, T[] r) {
+ String v = "";
+ for (T t: r) {
+ v += String.valueOf(t);
+ }
+ setBase(n, v);
+ }
+
+ protected void setBase(String n, Object v) {
+ g.attr(n, v);
+ }
+
+ public void setText(String s) {
+ g.text(s);
+ }
+
+ public String toString() {
+ return g.toString();
}
}
import com.google.gwt.core.ext.typeinfo.JClassType;\r
import com.google.gwt.core.ext.typeinfo.JMethod;\r
import com.google.gwt.core.ext.typeinfo.JParameter;\r
-import com.google.gwt.core.ext.typeinfo.JPrimitiveType;\r
import com.google.gwt.core.ext.typeinfo.JType;\r
import com.google.gwt.core.ext.typeinfo.TypeOracle;\r
import com.google.gwt.query.client.Properties;\r
sw.println("return getArrayBase(\"" + name + "\", new " + t + "[l], "\r
+ t + ".class);");\r
} else {\r
- sw.println("return p.get(\"" + name + "\");");\r
+ sw.println("return null; // Unsupported return type: " + retType);\r
}\r
sw.outdent();\r
sw.println("}");\r
} else if (params.length == 1) {\r
JType type = params[0].getType();\r
JArrayType arr = type.isArray();\r
- JPrimitiveType pri = type.isPrimitive();\r
sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)");\r
sw.println("{");\r
sw.indent();\r
if (arr != null) {\r
sw.println("setArrayBase(\"" + name + "\", a);");\r
- } else if (pri != null) {\r
} else {\r
sw.println("p.set(\"" + name + "\", a);");\r
}\r
sw.println(" l.add(c);");\r
sw.println("}");\r
sw.println("return l.toArray(new " + q + "[0]);");\r
-// } else {\r
-// sw.println("return p.get(\"" + name + "\");");\r
+ } else {\r
+ sw.println("return null; // Unsupported return type: " + retType);\r
}\r
sw.outdent();\r
sw.println("}");\r
} else if (params.length == 1) {\r
JType type = params[0].getType();\r
JArrayType arr = type.isArray();\r
- JPrimitiveType pri = type.isPrimitive();\r
- sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)");\r
+ String qname = type.getParameterizedQualifiedSourceName();\r
+ sw.print("(" + qname + " a)");\r
sw.println("{");\r
sw.indent();\r
if (arr != null) {\r
sw.println("setArrayBase(\"" + name + "\", a);");\r
- } else if (pri != null) {\r
} else {\r
- sw.println("p.set(\"" + name + "\", a);");\r
+ sw.println("setBase(\"" + name + "\", a);");\r
}\r
if (!"void".equals(retType)){\r
if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) {\r
String[] getT();
JsonExample setT(String[] strings);
JsonExample setZ(Boolean b);
+ JsonExample setD(long l);
}
public void testJsonBuilder() {
assertEquals("adios", c.getT()[1]);
assertEquals("url", c.getUrl());
c.setT(new String[]{"foo", "bar"})
- .setZ(false);
+ .setZ(false).setD(1234);
assertFalse(c.getZ());
assertEquals("foo", c.getT()[0]);
assertEquals("bar", c.getT()[1]);
+ assertEquals(1234l, c.getD());
+
}
interface XmlExample extends XmlBuilder {
XmlExample[] getX();
@Name("x")
XmlExample getFirstX();
+
+ XmlExample setA(String s);
+ @Name("c")
+ XmlExample setNumber(int i);
}
public void testXmlBuilder() {
assertEquals(-1, x.getNumber());
assertEquals("xa2", x.getX()[1].getA());
assertEquals("xa1", x.getFirstX().getA());
+ x.setA("X").setNumber(1234);
+ assertEquals("X", x.getA());
+ assertEquals(1234, x.getNumber());
}
}