diff options
8 files changed, 74 insertions, 18 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java index 292321b8..a780602c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java @@ -64,13 +64,25 @@ public abstract class GQ { } /** - * Create an instance of IsProperties. Normally a Properties javascript - * object in client side, or a proxy object in the JVM + * Create an instance of IsProperties, a Properties JavaScriptObject in the client + * side and a proxy object in the JVM. */ public static IsProperties create(String s) { return getFactory().create(s); } - + + /** + * Create an instance of IsProperties, a Properties JavaScriptObject in the client + * side and a proxy object in the JVM. + * + * If fixJson is set, we correct certain errors in the Json string. It is useful + * for generating Properties using java strings, so as we can use a more relaxed + * syntax. + */ + public static IsProperties create(String s, boolean fixJson) { + return getFactory().create(fixJson ? Properties.wrapPropertiesString(s) : s); + } + /** * Return the appropriate transport implementation depending on the runtime * environment: browser or JVM diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index ed39d4eb..db35f5d6 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -45,6 +45,15 @@ public class Properties extends JavaScriptObject implements IsProperties { return create(); } + /** + * Allows using a more relaxed syntax for creating json objects from strings. + * + * It is very useful in java, since we dont have to use escaped double quotes, + * and we can pass directly css strings. + * + * Example: + * $$("a: b; c: 'n'; d: null") is the same than $$("\"a\": \"b\", "\b\":\"n\"n, \"d\":null)") + */ public static String wrapPropertiesString(String s) { String ret = s // .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") // Remove comments diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java index 66ed45a4..4231fd2e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java @@ -15,7 +15,6 @@ */ package com.google.gwt.query.rebind; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; @@ -94,7 +93,7 @@ public class JsniBundleGenerator extends Generator { } try { // Read the javascript content - String content = getContent(logger, packageName.replace(".", File.separator) , value); + String content = getContent(logger, packageName.replace(".", "/") , value); // Adjust javascript so as we can introduce it in a JSNI comment block without // breaking java syntax. @@ -129,8 +128,9 @@ public class JsniBundleGenerator extends Generator { InputStream in = null; try { if (!src.matches("(?i)https?://.*")) { - String file = path + File.separator + src; + String file = path + "/" + src; logger.log(TreeLogger.INFO, getClass().getSimpleName() + " - importing external javascript: " + file); + in = this.getClass().getClassLoader().getResourceAsStream(file); if (in == null) { logger.log(TreeLogger.ERROR, "Unable to read javascript file: " + file); 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 270db704..f85177de 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 @@ -43,6 +43,7 @@ import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.JsonBuilderBase; import com.google.gwt.query.client.builders.JsonFactory; import com.google.gwt.query.client.builders.Name; +import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; import com.google.gwt.user.rebind.SourceWriter; @@ -324,7 +325,7 @@ public class JsonBuilderGenerator extends Generator { sw.println("}"); sw.println("public " + IsProperties.class.getName() + " create(String s) {"); sw.indent(); - sw.println("return " + Properties.class.getName() + ".create(s);"); + sw.println("return (" + IsProperties.class.getName() + ")" + JsUtils.class.getName() + ".parseJSON(s);"); sw.outdent(); sw.println("}"); sw.println("public " + IsProperties.class.getName() + " create() {"); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index 76deabb2..775a789f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -66,8 +66,6 @@ public class JsonFactoryJre implements JsonFactory { ret = jsonArrayToList(obj.getJSONArray(attr), ctype, clz.isArray()); } else if (clz.equals(Date.class)) { ret = new Date(obj != null ? obj.getLong(attr): arr.getLong(idx)); - } else if (clz.equals(String.class)) { - ret = String.valueOf(obj != null ? obj.get(attr) : arr.get(idx)); } else if (clz.equals(Boolean.class) || clz.isPrimitive() && clz == Boolean.TYPE) { try { ret = obj != null ? obj.getBoolean(attr): arr.getBoolean(idx); @@ -97,15 +95,19 @@ public class JsonFactoryJre implements JsonFactory { } } else { ret = obj != null ? obj.get(attr): arr.get(idx); - if (ret instanceof JSONObject) { + if (ret == JSONObject.NULL ) { + // org.json returns an Null object instead of null when parsing. + ret = null; + } else if (clz.equals(String.class)) { + ret = String.valueOf(ret); + } else if (ret instanceof JSONObject) { if (clz == Object.class) { ret = jsonFactory.createBinder((JSONObject)ret); } else if (IsProperties.class.isAssignableFrom(clz) && !clz.isAssignableFrom(ret.getClass())) { ret = jsonFactory.create(clz, (JSONObject)ret); } - } - // Javascript always returns a double - if (ret instanceof Number) { + } else if (ret instanceof Number) { + // Javascript always returns a double ret = Double.valueOf(((Number) ret).doubleValue()); } } @@ -177,7 +179,11 @@ public class JsonFactoryJre implements JsonFactory { } else if (mname.matches("getProperties|getDataImpl")) { return jsonObject; } else if (largs > 0 && ("parse".equals(mname) || "load".equals(mname))) { - jsonObject = new JSONObject(String.valueOf(args[0])); + String json = String.valueOf(args[0]); + if (largs > 1 && Boolean.TRUE.equals(args[0])) { + json = Properties.wrapPropertiesString(json); + } + jsonObject = new JSONObject(json); } else if (mname.matches("toString")) { return jsonObject.toString(); } else if (mname.matches("toJsonWithName")) { @@ -294,7 +300,7 @@ public class JsonFactoryJre implements JsonFactory { @Override public IsProperties create(String s) { IsProperties ret = createBinder(); - ret.parse(Properties.wrapPropertiesString(s)); + ret.parse(s); return ret; } diff --git a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java index 7f8fb42e..3647538b 100644 --- a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java +++ b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java @@ -17,6 +17,7 @@ package com.google.gwt.query.client; import com.google.gwt.core.shared.GWT; import com.google.gwt.query.client.IsProperties; +import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.JsonFactory; import com.google.gwt.query.client.plugins.ajax.AjaxTransportJs; @@ -44,6 +45,10 @@ public class GQ { return getFactory().create(s); } + public static IsProperties create(String s, boolean fix) { + return getFactory().create(fix ? Properties.wrapPropertiesString(s) : s); + } + public static IsProperties create() { return getFactory().create(); } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java index f5d0cbf5..1920afc3 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java @@ -50,8 +50,8 @@ public abstract class AjaxTests extends GWTTestCase { }; public AjaxTests() { - jsonGET = GQ.create("data: {a: abc, d: def}"); - json = GQ.create("a: abc, d: def"); + jsonGET = GQ.create("data: {a: abc, d: def}", true); + json = GQ.create("a: abc, d: def", true); } private Promise performAjaxJsonTest(Settings s) { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java index ac648fc1..965bf161 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java @@ -22,6 +22,7 @@ import java.util.List; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQ; +import com.google.gwt.query.client.IsProperties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.Name; @@ -34,6 +35,26 @@ public class DataBindingTestJre extends GWTTestCase { return null; } + public void testPropertiesCreate() { + IsProperties p1 = GQ.create(); + p1.set("a", "1"); + p1.set("b", 1); + p1.set("c", "null"); + p1.set("d", null); + + assertEquals("1", p1.get("a")); + assertEquals(Double.valueOf(1), p1.get("b")); + assertEquals("null", p1.get("c")); + assertNull(p1.get("d")); + + p1 = GQ.create(p1.toJson()); + + assertEquals("1", p1.get("a")); + assertEquals(Double.valueOf(1), p1.get("b")); + assertEquals("null", p1.get("c")); + assertNull(p1.get("d")); + } + public interface Item extends JsonBuilder { public static enum Type {BIG, SMALL} @@ -62,16 +83,18 @@ public class DataBindingTestJre extends GWTTestCase { void y(String s); Function getF(); void setF(Function f); + String getN(); } boolean functionRun = false; public void testJsonBuilder() { - String json = "{M:0, a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true, 'items':[{'date':100}]}"; + String json = "{n: null, M:0, a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true, 'items':[{'date':100}]}"; JsonExample c = GQ.create(JsonExample.class); assertEquals(0, c.getA()); c.parse(json, true); + assertNull(c.getN()); assertEquals(0, c.getM()); assertEquals(1, c.getA()); assertNotNull(c.getB()); |