From 96f7ece62367185a8b53cb16ce2e5c1155927eed Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 19 Oct 2011 06:34:54 +0000 Subject: [PATCH] setters in xml builders was not implemented --- .../gwt/query/client/builders/XmlBuilder.java | 12 ++- .../query/client/builders/XmlBuilderBase.java | 84 ++++++++++++------- .../query/rebind/JsonBuilderGenerator.java | 5 +- .../gwt/query/rebind/XmlBuilderGenerator.java | 11 ++- .../gwt/query/client/GQueryAjaxTest.java | 12 ++- 5 files changed, 80 insertions(+), 44 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java index bf3153ed..781341f5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java @@ -34,5 +34,15 @@ public interface XmlBuilder { /** * Returns the documentElement. */ - Element getDocumentElement(); + Element getRootElement(); + + /** + * Appends a node + */ + void append(XmlBuilder x); + + /** + * Appends xml content + */ + void append(String x); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java index 0c7fe017..0b2ba4c2 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java @@ -28,35 +28,18 @@ public abstract class XmlBuilderBase> implements Xml //TODO empty document protected GQuery g = $(JsUtils.parseXML("")); - @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); } @@ -65,25 +48,62 @@ public abstract class XmlBuilderBase> implements Xml 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 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(); } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java index 76d5a76c..5b69a292 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java @@ -26,7 +26,6 @@ import com.google.gwt.core.ext.typeinfo.JArrayType; import com.google.gwt.core.ext.typeinfo.JClassType; import com.google.gwt.core.ext.typeinfo.JMethod; import com.google.gwt.core.ext.typeinfo.JParameter; -import com.google.gwt.core.ext.typeinfo.JPrimitiveType; import com.google.gwt.core.ext.typeinfo.JType; import com.google.gwt.core.ext.typeinfo.TypeOracle; import com.google.gwt.query.client.Properties; @@ -115,20 +114,18 @@ public class JsonBuilderGenerator extends Generator { sw.println("return getArrayBase(\"" + name + "\", new " + t + "[l], " + t + ".class);"); } else { - sw.println("return p.get(\"" + name + "\");"); + sw.println("return null; // Unsupported return type: " + retType); } sw.outdent(); sw.println("}"); } else if (params.length == 1) { JType type = params[0].getType(); JArrayType arr = type.isArray(); - JPrimitiveType pri = type.isPrimitive(); sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)"); sw.println("{"); sw.indent(); if (arr != null) { sw.println("setArrayBase(\"" + name + "\", a);"); - } else if (pri != null) { } else { sw.println("p.set(\"" + name + "\", a);"); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/XmlBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/XmlBuilderGenerator.java index b0218d7f..831ec96b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/XmlBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/XmlBuilderGenerator.java @@ -111,23 +111,22 @@ public class XmlBuilderGenerator extends Generator { sw.println(" l.add(c);"); sw.println("}"); sw.println("return l.toArray(new " + q + "[0]);"); -// } else { -// sw.println("return p.get(\"" + name + "\");"); + } else { + sw.println("return null; // Unsupported return type: " + retType); } sw.outdent(); sw.println("}"); } else if (params.length == 1) { JType type = params[0].getType(); JArrayType arr = type.isArray(); - JPrimitiveType pri = type.isPrimitive(); - sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)"); + String qname = type.getParameterizedQualifiedSourceName(); + sw.print("(" + qname + " a)"); sw.println("{"); sw.indent(); if (arr != null) { sw.println("setArrayBase(\"" + name + "\", a);"); - } else if (pri != null) { } else { - sw.println("p.set(\"" + name + "\", a);"); + sw.println("setBase(\"" + name + "\", a);"); } if (!"void".equals(retType)){ if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTest.java index 841015b4..7056a9a3 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTest.java @@ -59,6 +59,7 @@ public class GQueryAjaxTest extends GWTTestCase { String[] getT(); JsonExample setT(String[] strings); JsonExample setZ(Boolean b); + JsonExample setD(long l); } public void testJsonBuilder() { @@ -75,10 +76,12 @@ public class GQueryAjaxTest extends GWTTestCase { 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 { @@ -90,6 +93,10 @@ public class GQueryAjaxTest extends GWTTestCase { XmlExample[] getX(); @Name("x") XmlExample getFirstX(); + + XmlExample setA(String s); + @Name("c") + XmlExample setNumber(int i); } public void testXmlBuilder() { @@ -101,5 +108,8 @@ public class GQueryAjaxTest extends GWTTestCase { 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()); } } -- 2.39.5