From: Manolo Carrasco Date: Wed, 14 Sep 2011 12:15:41 +0000 (+0000) Subject: Add toJsonString and toQueryString methods to Properties object. Use generics in... X-Git-Tag: release-1.3.2~204 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2539686620fb5c4bb6710840c6a991702a58539f;p=gwtquery.git Add toJsonString and toQueryString methods to Properties object. Use generics in JsCache --- 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 906b5b03..d9879488 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 @@ -16,6 +16,7 @@ package com.google.gwt.query.client; import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.query.client.js.JsCache; /** @@ -30,6 +31,7 @@ public class Properties extends JavaScriptObject { public static Properties create(String properties) { if (properties != null && !properties.isEmpty()) { String p = camelizePropertiesKeys(wrapPropertiesString(properties)); + System.out.println(p); try { return (Properties) createImpl(p); } catch (Exception e) { @@ -51,7 +53,7 @@ public class Properties extends JavaScriptObject { .replaceFirst("^[{\\(]+(|.*[^}\\)])[}\\)]+$", "$1") // Remove ({}) .replaceAll("\\('([^\\)]+)'\\)" , "($1)") // Remove quotes .replaceAll(",+([\\w-]+:+)" , ";$1") // put semicolon - .replaceAll(":\\s*[\"']?([^';]*)[\"']?\\s*(;+|$)", ":'$1',") // put quotes + .replaceAll(":\\s*[\"']?([^'\\]};]*)[\"']?\\s*(;+|$)", ":'$1',") // put quotes .replaceAll(":'(-?[\\d\\.]+|null|false|true)',", ":$1,") // numbers do not need quote .replaceFirst("[;,]$", "") // remove endings + "})"; @@ -89,7 +91,7 @@ public class Properties extends JavaScriptObject { } public final T get(Object name) { - return c().get(String.valueOf(name)); + return c().get(String.valueOf(name)); } public final boolean getBoolean(Object name) { @@ -107,7 +109,19 @@ public class Properties extends JavaScriptObject { public final String getStr(Object name) { return c().getString(String.valueOf(name)); } + + public final Object getObject(Object name) { + return c().get(String.valueOf(name)); + } + + public final T getJavaScriptObject(Object name) { + return c().getJavaScriptObject(String.valueOf(name)); + } + public final JsArrayMixed getArray(Object name) { + return c().getArray(String.valueOf(name)); + } + public final String[] keys() { return c().keys(); } @@ -121,10 +135,50 @@ public class Properties extends JavaScriptObject { } public final String tostring() { + return "(" + toJsonString() + ")"; + } + + public final String toJsonString() { String ret = ""; + for (String k : keys()){ - ret += k + ": '" + getStr(k) + "', "; + JsArrayMixed o = getArray(k); + if (o != null) { + ret += k + ":["; + for (int i = 0, l = o.length(); i < l ; i++) { + ret += "'" + o.getString(i) + "',"; + } + ret += "],"; + } else { + Properties p = getJavaScriptObject(k); + if (p != null) { + ret += k + ":" + p.toJsonString(); + } else { + ret += k + ":'" + getStr(k) + "',"; + } + } } - return "({" + ret.replaceAll("[, ]+$","") + "})"; + return "{" + ret.replaceAll(",\\s*([\\]}]|$)","").replaceAll("([:,\\[])'(-?[\\d\\.]+|null|false|true)'", "$1$2") + "}"; + } + + public final String toQueryString() { + String ret = ""; + for (String k : keys()) { + ret += ret.isEmpty() ? "" : "&"; + JsArrayMixed o = getArray(k); + if (o != null) { + for (int i = 0, l = o.length(); i < l ; i++) { + ret += (i > 0 ? "&" : "") + k + "[]=" + o.getString(i) ; + } + } else { + Properties p = getJavaScriptObject(k); + if (p != null) { + ret += p.toQueryString(); + } else { + ret += k + "=" + getStr(k); + } + } + } + return 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 dea558ee..f42f3e07 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 @@ -1,6 +1,7 @@ package com.google.gwt.query.client.js; import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.core.client.JsArrayString; /** @@ -26,87 +27,64 @@ public class JsCache extends JavaScriptObject { } } - public final native void delete(int name) /*-{ + public final native void delete(T name) /*-{ delete this[name]; }-*/; - public final native void delete(String name) /*-{ - delete this[name]; - }-*/; - - public final native boolean exists(String name) /*-{ + public final native boolean exists(T name) /*-{ return !!this[name]; }-*/; - public final native boolean exists(int id) /*-{ - return !!this[id]; - }-*/; - - public final native T get(int id) /*-{ + public final native R get(T id) /*-{ return this[id] || null; }-*/; - public final native T get(String id) /*-{ - return this[id] || null; - }-*/; - - public final JsCache getCache(int id) { + 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) /*-{ + public final native boolean getBoolean(T id) /*-{ return !!this[id]; }-*/; - public final native float getFloat(int id) /*-{ - return this[id] || 0; - }-*/; - - public final native float getFloat(String id) /*-{ + public final native float getFloat(T id) /*-{ return this[id] || 0; }-*/; - public final native double getDouble(int id) /*-{ + public final native double getDouble(T id) /*-{ return this[id] || 0; }-*/; - public final native double getDouble(String id) /*-{ - return this[id] || 0; - }-*/; - - public final native int getInt(int id) /*-{ - return this[id] || 0; - }-*/; - - public final native int getInt(String id) /*-{ + public final native int getInt(T id) /*-{ return this[id] || 0; }-*/; - public final native String getString(int id) /*-{ + public final native String getString(T id) /*-{ return this[id] == null ? null : String(this[id]); }-*/; - - public final native String getString(String id) /*-{ - return this[id] == null ? null : String(this[id]); + + public final native JsArrayMixed getArray(T id) /*-{ + var r = this[id]; + if (r != null && Object.prototype.toString.call(r) == '[object Array]') { + return r; + } + return null; }-*/; + + public final T getJavaScriptObject(Object name) { + Object o = get(name); + return (o instanceof JavaScriptObject) ? ((JavaScriptObject)o).cast() : null; + } public final native boolean isEmpty() /*-{ for (k in this) return false; return true; }-*/; - public final native void put(int id, Object obj) /*-{ + public final native void put(T id, O obj) /*-{ this[id] = obj; }-*/; - public final native void put(String id, Object obj) /*-{ - this[id] = obj; - }-*/; - public final native int length() /*-{ if (typeof(this.length) == 'number') return this.length;