aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-05-02 08:41:50 +0000
committerManolo Carrasco <manolo@apache.org>2011-05-02 08:41:50 +0000
commit6aafaac36916a96ca8a72e16666bf0437af5be8a (patch)
treed990e69133cb969a5535899cb44251d6b6887e84
parent4cbbbc02064c333fed8cdf0213d44f5bf600b2f2 (diff)
downloadgwtquery-6aafaac36916a96ca8a72e16666bf0437af5be8a.tar.gz
gwtquery-6aafaac36916a96ca8a72e16666bf0437af5be8a.zip
Fixing a bug in Properties parser when the input string was an empty json. Re-factoring of properties class so as it use native methods already present in jscache
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java85
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java22
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java15
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java8
4 files changed, 89 insertions, 41 deletions
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 5d576ed5..25d62c8f 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
@@ -23,35 +23,36 @@ import com.google.gwt.query.client.js.JsCache;
*/
public class Properties extends JavaScriptObject {
+ public static Properties create() {
+ return (Properties) createImpl("({})");
+ }
+
public static Properties create(String properties) {
String p = wrapPropertiesString(properties);
try {
return (Properties) createImpl(p);
} catch (Exception e) {
- System.err.println("Error creating Properties: \n" + properties + "\n" + p + "\n" + e.getMessage());
- return (Properties) createImpl("({})");
+ System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n" + e.getMessage());
+ return create();
}
}
-
- public static Properties create() {
- return (Properties) createImpl("({})");
- }
public static final native JavaScriptObject createImpl(String properties) /*-{
- return eval(properties);
+ return eval(properties);
}-*/;
public static String wrapPropertiesString(String s) {
String ret = "({" + s //
- .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") //
- .replaceAll("([:\\)\\(,;}{'\"])\\s+" , "$1") //
- .replaceAll("\\s+([:\\)\\(,;}{'\"])" , "$1") //
- .replaceFirst("^[{\\(]+(.+[^}\\)])[}\\)]+$", "$1") //
- .replaceAll("\\('([^\\)]+)'\\)" , "($1)") //
- .replaceAll(",([\\w-]+:+)" , ";$1") //
- .replaceAll(":\\s*[\"']?([^;]+)([;]|$)[\"']?\\s*", ":'$1',") //
- .replaceFirst("[;,]$", "") //
- .replaceAll("\\s*[']+\\s*", "'") //
+ .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") // Remove comments
+ .replaceAll("([:\\)\\(,;}{'\"])\\s+" , "$1") // Remove spaces
+ .replaceAll("\\s+([:\\)\\(,;}{'\"])" , "$1") // Remove spaces
+ .replaceFirst("^[{\\(]+(|.*[^}\\)])[}\\)]+$", "$1") // Remove ({})
+ .replaceAll("\\('([^\\)]+)'\\)" , "($1)") // Remove quotes
+ .replaceAll(",+([\\w-]+:+)" , ";$1") // put semicolon
+ .replaceAll(":\\s*[\"']?([^;]+)([;]+|$)[\"']?\\s*", ":'$1',") // put quotes
+ .replaceAll(":'(-?[\\d\\.]+|null|false|true)',", ":$1,") // numbers do not need quote
+ .replaceFirst("[;,]$", "") // remove endings
+ .replaceAll("\\s*[']+\\s*", "'") // remove duplicates
+ "})";
return ret;
}
@@ -64,6 +65,10 @@ public class Properties extends JavaScriptObject {
return this;
}
+ private JsCache c() {
+ return this.<JsCache>cast();
+ }
+
public final native Properties cloneProps() /*-{
var props = {};
for(p in this) {
@@ -72,33 +77,41 @@ public class Properties extends JavaScriptObject {
return props;
}-*/;
- public final native boolean defined(String name) /*-{
- return this[name] != undefined;
- }-*/;
+ public final boolean defined(Object name) {
+ return c().exists(String.valueOf(name));
+ }
- public final native <T> T get(String name) /*-{
- return this[name];
- }-*/;
+ public final <T> T get(Object name) {
+ return c().get(String.valueOf(name));
+ }
- public final native String getStr(String name) /*-{
- return String(this[name]);
- }-*/;
+ public final boolean getBoolean(Object name) {
+ return c().getBoolean(String.valueOf(name));
+ }
- public final native float getFloat(String name) /*-{
- return this[name];
- }-*/;
+ public final float getFloat(Object name) {
+ return c().getFloat(String.valueOf(name));
+ }
- public final native int getInt(String name) /*-{
- return this[name];
- }-*/;
+ public final int getInt(Object name) {
+ return c().getInt(String.valueOf(name));
+ }
+
+ public final String getStr(Object name) {
+ return c().getString(String.valueOf(name));
+ }
public final String[] keys() {
- return this.<JsCache>cast().keys();
+ return c().keys();
+ }
+
+ public final <T> void remove(T name) {
+ c().delete(String.valueOf(name));
+ }
+
+ public final <T> void set(T name, Object val) {
+ c().put(String.valueOf(name), val);
}
-
- public final native void set(String key, Object val) /*-{
- this[key]=val;
- }-*/;
public final String tostring() {
String ret = "";
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
index 022ee6bc..5a85be04 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
@@ -53,6 +53,22 @@ public class JsCache extends JavaScriptObject {
public final JsCache getCache(int id) {
return (JsCache)get(id);
}
+
+ public final native boolean getBoolean(int id) /*-{
+ return !!this[id];
+ }-*/;
+
+ public final native boolean getBoolean(String id) /*-{
+ return !!this[id];
+ }-*/;
+
+ public final native float getFloat(int id) /*-{
+ return this[id] || 0;
+ }-*/;
+
+ public final native float getFloat(String id) /*-{
+ return this[id] || 0;
+ }-*/;
public final native double getDouble(int id) /*-{
return this[id] || 0;
@@ -61,7 +77,7 @@ public class JsCache extends JavaScriptObject {
public final native double getDouble(String id) /*-{
return this[id] || 0;
}-*/;
-
+
public final native int getInt(int id) /*-{
return this[id] || 0;
}-*/;
@@ -71,11 +87,11 @@ public class JsCache extends JavaScriptObject {
}-*/;
public final native String getString(int id) /*-{
- return this[id];
+ return this[id] == null ? null : String(this[id]);
}-*/;
public final native String getString(String id) /*-{
- return this[id] || null;
+ return this[id] == null ? null : String(this[id]);
}-*/;
public final native boolean isEmpty() /*-{
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java
index c497bde6..0a3bb9d9 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java
@@ -117,5 +117,20 @@ public class GQueryJsTest extends GWTTestCase {
p = $$("color: 'rgb(0, 0,139)', background: red");
assertEquals(2, p.keys().length);
assertEquals("rgb(0,0,139)", p.getStr("color"));
+
+ p = $$("a: 1, b: 0.5, c: null, d: whatever, e: true, f: false");
+ System.out.println(p.tostring());
+ assertEquals(1, p.getInt("a"));
+ assertEquals(0.5f, p.getFloat("b"));
+ assertEquals("whatever", p.getStr("d"));
+ assertNull(p.getStr("c"));
+ assertNull(p.getStr("ccc"));
+ assertTrue(p.getBoolean("e"));
+ assertTrue(p.getBoolean("d"));
+ assertFalse(p.getBoolean("f"));
+ assertFalse(p.getBoolean("c"));
+ assertTrue(p.defined("d"));
+ p.remove("d");
+ assertFalse(p.defined("d"));
}
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java
index a6df7a39..48c9dda0 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java
@@ -34,6 +34,10 @@ public class JreQueryCoreTest extends GWTTestCase {
}
public void testWrapPropertiesString() {
+ assertEquals("({})", Properties
+ .wrapPropertiesString(""));
+ assertEquals("({})", Properties
+ .wrapPropertiesString("({})"));
assertEquals("({border:'1px solid black'})", Properties
.wrapPropertiesString("border:'1px solid black'"));
assertEquals("({border:'1px solid black'})", Properties
@@ -48,8 +52,8 @@ public class JreQueryCoreTest extends GWTTestCase {
.wrapPropertiesString("{(border:'1px solid black')}"));
assertEquals("({border:'1px solid black'})", Properties
.wrapPropertiesString("({border:'1px solid black'})"));
- assertEquals("({b:'a',c:'1',d:'url(https://test.com)'})", Properties
- .wrapPropertiesString("b: 'a'; c: 1, /*gg: aadf*/d: url('https://test.com');"));
+ assertEquals("({b:'a',c:1,d:'url(https://test.com)',e:null,f:false})", Properties
+ .wrapPropertiesString("b: 'a'; c: 1, /*gg: aadf*/d: url('https://test.com');,e:null,f:false"));
assertEquals("({color:'rgb(0,0,139)',background:'red'})", Properties
.wrapPropertiesString("color: 'rgb(0, 0,139)', background: red"));
}