You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Properties.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright 2011, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client;
  17. import com.google.gwt.core.client.GWT;
  18. import com.google.gwt.core.client.JavaScriptObject;
  19. import com.google.gwt.core.client.JsArrayMixed;
  20. import com.google.gwt.query.client.builders.JsonBuilder;
  21. import com.google.gwt.query.client.js.JsCache;
  22. import com.google.gwt.query.client.js.JsUtils;
  23. /**
  24. * JSO for accessing Javascript objective literals used by GwtQuery functions.
  25. */
  26. public class Properties extends JavaScriptObject implements IsProperties {
  27. public static Properties create() {
  28. return JsCache.create().cast();
  29. }
  30. public static Properties create(String properties) {
  31. if (properties != null && !properties.isEmpty()) {
  32. String p = wrapPropertiesString(properties);
  33. try {
  34. return JsUtils.parseJSON(p);
  35. } catch (Exception e) {
  36. if (!GWT.isProdMode()) {
  37. System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n"
  38. + e.getMessage());
  39. }
  40. }
  41. }
  42. return create();
  43. }
  44. /**
  45. * Allows using a more relaxed syntax for creating json objects from strings.
  46. *
  47. * It is very useful in java, since we dont have to use escaped double quotes,
  48. * and we can pass directly css strings.
  49. *
  50. * Example:
  51. * $$("a: b; c: 'n'; d: null") is the same than $$("\"a\": \"b\", "\b\":\"n\"n, \"d\":null)")
  52. */
  53. public static String wrapPropertiesString(String s) {
  54. String ret = s //
  55. .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") // Remove comments
  56. .replaceAll("([:\\)\\(,;}{'\"])\\s+", "$1") // Remove spaces
  57. .replaceAll("\\s+([:\\)\\(,;}{'\"])", "$1") // Remove spaces
  58. .replaceFirst("^[\\(]+(.*)[\\)]+$", "$1") // Remove ()
  59. .replaceAll("\\([\"']([^\\)]+)[\"']\\)", "($1)") // Remove quotes
  60. .replaceAll("[;,]+([\\w-\\$]+:|$)", ";$1") // Change comma by semicolon
  61. .replaceAll("([^,;])([\\]}])", "$1;$2") // Put control semicolon used below
  62. .replaceAll(":\\s*[\"']?([^;\\{\\}\\[\\]\"']*)[\"']?\\s*([;,]+|$)", ":\"$1\";") // put quotes to all values (even empty)
  63. .replaceAll("[;,]+([\\w-]+):", ";$1:") // Change semicolon by comma
  64. .replaceAll("(^|[^\\w-\\$'])([\\w-\\$]+):(['\"\\[{])", "$1\"$2\":$3") // quote keys
  65. .replaceAll("(^|[^\\w-\\$'])([\\w-\\$]+):(['\"\\[{])", "$1\"$2\":$3") // quote keys second pass
  66. .replaceAll("(|[\\[\\]\\{\\},\\(])'([^']*)'", "$1\"$2\"") // Replace single-quote by double-quote
  67. .replaceAll(";([^:]+):", ",$1:") // change semicolon
  68. .replaceAll(";([^:]+):", ",$1:") // change semicolon second pass
  69. .replaceAll(":\"(-?\\d[\\d\\.]*|null|false|true)\"[;,]", ":$1,") // numbers do not need quote
  70. .replaceAll("[;,]+([\\]\\}]|$)", "$1"); // remove endings
  71. ret = ret.matches("(^[\\[\\{].*[\\]\\}]$)") ? ret : "{" + ret + "}";
  72. return ret;
  73. }
  74. protected Properties() {
  75. }
  76. public final Properties $$(String key, String value) {
  77. set(key, value);
  78. return this;
  79. }
  80. private JsCache c() {
  81. return this.<JsCache> cast();
  82. }
  83. public final native Properties cloneProps() /*-{
  84. var props = {};
  85. for(p in this) {
  86. props[p] = this[p];
  87. }
  88. return props;
  89. }-*/;
  90. public final boolean defined(Object name) {
  91. return c().exists(String.valueOf(name));
  92. }
  93. @SuppressWarnings("unchecked")
  94. public final <T> T get(Object name) {
  95. // Casting because of issue_135
  96. return (T) c().get(String.valueOf(name));
  97. }
  98. public final boolean getBoolean(Object name) {
  99. return c().getBoolean(String.valueOf(name));
  100. }
  101. public final float getFloat(Object name) {
  102. return c().getFloat(String.valueOf(name));
  103. }
  104. public final Function getFunction(Object name) {
  105. final Object o = c().get(String.valueOf(name));
  106. if (o != null) {
  107. if (o instanceof Function) {
  108. return (Function) o;
  109. } else if (o instanceof JavaScriptObject) {
  110. Object f = ((JavaScriptObject) o).<Properties> cast().getObject("__f");
  111. if (f != null && f instanceof Function) {
  112. return (Function) f;
  113. }
  114. return new JsUtils.JsFunction((JavaScriptObject) o);
  115. }
  116. }
  117. return null;
  118. }
  119. public final int getInt(Object name) {
  120. return c().getInt(String.valueOf(name));
  121. }
  122. public final String getStr(Object name) {
  123. return c().getString(String.valueOf(name));
  124. }
  125. public final Object getObject(Object name) {
  126. return c().get(String.valueOf(name));
  127. }
  128. public final Properties getProperties(Object name) {
  129. return getJavaScriptObject(name);
  130. }
  131. @SuppressWarnings("unchecked")
  132. public final <T extends JavaScriptObject> T getJavaScriptObject(Object name) {
  133. // Casting because of issue_135
  134. return (T) c().getJavaScriptObject(String.valueOf(name));
  135. }
  136. public final JsArrayMixed getArray(Object name) {
  137. return c().getArray(String.valueOf(name));
  138. }
  139. public final String[] keys() {
  140. return c().keys();
  141. }
  142. public final <T> void remove(T name) {
  143. c().delete(String.valueOf(name));
  144. }
  145. public final <T> void setNumber(T name, double val) {
  146. c().putNumber(name, val);
  147. }
  148. public final <T> void setBoolean(T name, boolean val) {
  149. c().putBoolean(name, val);
  150. }
  151. /**
  152. * Adds a new native js function to the properties object.
  153. * This native function will wrap the passed java Function.
  154. *
  155. * Its useful for exporting or importing to javascript.
  156. *
  157. */
  158. public final native <T> void setFunction(T name, Function f) /*-{
  159. if (!f) return;
  160. this[name] = function() {
  161. f.@com.google.gwt.query.client.Function::fe(Ljava/lang/Object;)(arguments);
  162. }
  163. // We store the original function reference
  164. this[name].__f = f;
  165. }-*/;
  166. @SuppressWarnings("unchecked")
  167. public final Properties set(Object name, Object val) {
  168. c().put(String.valueOf(name), val);
  169. return this;
  170. }
  171. public final String tostring() {
  172. return toJsonString();
  173. }
  174. public final String toJsonString() {
  175. return JsUtils.JSON2String(JsCache.checkNull(this));
  176. }
  177. public final String toQueryString() {
  178. return JsUtils.param(JsCache.checkNull(this));
  179. }
  180. public final boolean isEmpty() {
  181. return c().length() == 0;
  182. }
  183. public final <J extends IsProperties> J load(Object prp) {
  184. c().clear();
  185. if (prp instanceof JsCache) {
  186. c().copy((JsCache) prp);
  187. }
  188. return getDataImpl();
  189. }
  190. @SuppressWarnings("unchecked")
  191. public final <J extends IsProperties> J strip() {
  192. return (J)this;
  193. }
  194. public final <J extends IsProperties> J parse(String json) {
  195. return load(JsUtils.parseJSON(json));
  196. }
  197. public final String[] getFieldNames() {
  198. return c().keys();
  199. }
  200. public final String toJson() {
  201. return toJsonString();
  202. }
  203. public final String toJsonWithName() {
  204. return toJsonWithName(getJsonName());
  205. }
  206. public final String toJsonWithName(String name) {
  207. return "{\"" + name + "\":{" + toJson() + "}";
  208. }
  209. @SuppressWarnings("unchecked")
  210. public final <J> J getDataImpl() {
  211. return (J) this;
  212. }
  213. public final String getJsonName() {
  214. return "jso";
  215. }
  216. public final <T extends JsonBuilder> T as(Class<T> clz) {
  217. T ret = GQ.create(clz);
  218. ret.load(this);
  219. return ret;
  220. }
  221. }