*/\r
public class Properties extends JavaScriptObject {\r
\r
+ public static Properties create() {\r
+ return (Properties) createImpl("({})");\r
+ }\r
+ \r
public static Properties create(String properties) {\r
String p = wrapPropertiesString(properties);\r
try {\r
return (Properties) createImpl(p);\r
} catch (Exception e) {\r
- System.err.println("Error creating Properties: \n" + properties + "\n" + p + "\n" + e.getMessage());\r
- return (Properties) createImpl("({})");\r
+ System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n" + e.getMessage());\r
+ return create();\r
}\r
}\r
- \r
- public static Properties create() {\r
- return (Properties) createImpl("({})");\r
- }\r
\r
public static final native JavaScriptObject createImpl(String properties) /*-{\r
- return eval(properties);\r
+ return eval(properties);\r
}-*/;\r
\r
public static String wrapPropertiesString(String s) {\r
String ret = "({" + s //\r
- .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") //\r
- .replaceAll("([:\\)\\(,;}{'\"])\\s+" , "$1") //\r
- .replaceAll("\\s+([:\\)\\(,;}{'\"])" , "$1") //\r
- .replaceFirst("^[{\\(]+(.+[^}\\)])[}\\)]+$", "$1") //\r
- .replaceAll("\\('([^\\)]+)'\\)" , "($1)") //\r
- .replaceAll(",([\\w-]+:+)" , ";$1") //\r
- .replaceAll(":\\s*[\"']?([^;]+)([;]|$)[\"']?\\s*", ":'$1',") //\r
- .replaceFirst("[;,]$", "") //\r
- .replaceAll("\\s*[']+\\s*", "'") //\r
+ .replaceAll("\\s*/\\*[\\s\\S]*?\\*/\\s*", "") // Remove comments\r
+ .replaceAll("([:\\)\\(,;}{'\"])\\s+" , "$1") // Remove spaces\r
+ .replaceAll("\\s+([:\\)\\(,;}{'\"])" , "$1") // Remove spaces\r
+ .replaceFirst("^[{\\(]+(|.*[^}\\)])[}\\)]+$", "$1") // Remove ({})\r
+ .replaceAll("\\('([^\\)]+)'\\)" , "($1)") // Remove quotes\r
+ .replaceAll(",+([\\w-]+:+)" , ";$1") // put semicolon\r
+ .replaceAll(":\\s*[\"']?([^;]+)([;]+|$)[\"']?\\s*", ":'$1',") // put quotes\r
+ .replaceAll(":'(-?[\\d\\.]+|null|false|true)',", ":$1,") // numbers do not need quote\r
+ .replaceFirst("[;,]$", "") // remove endings \r
+ .replaceAll("\\s*[']+\\s*", "'") // remove duplicates\r
+ "})";\r
return ret;\r
}\r
return this;\r
}\r
\r
+ private JsCache c() {\r
+ return this.<JsCache>cast();\r
+ }\r
+\r
public final native Properties cloneProps() /*-{\r
var props = {};\r
for(p in this) {\r
return props;\r
}-*/;\r
\r
- public final native boolean defined(String name) /*-{\r
- return this[name] != undefined; \r
- }-*/;\r
+ public final boolean defined(Object name) {\r
+ return c().exists(String.valueOf(name));\r
+ }\r
\r
- public final native <T> T get(String name) /*-{\r
- return this[name];\r
- }-*/;\r
+ public final <T> T get(Object name) {\r
+ return c().get(String.valueOf(name));\r
+ }\r
\r
- public final native String getStr(String name) /*-{\r
- return String(this[name]);\r
- }-*/;\r
+ public final boolean getBoolean(Object name) {\r
+ return c().getBoolean(String.valueOf(name));\r
+ }\r
\r
- public final native float getFloat(String name) /*-{\r
- return this[name];\r
- }-*/;\r
+ public final float getFloat(Object name) {\r
+ return c().getFloat(String.valueOf(name));\r
+ }\r
\r
- public final native int getInt(String name) /*-{\r
- return this[name];\r
- }-*/;\r
+ public final int getInt(Object name) {\r
+ return c().getInt(String.valueOf(name));\r
+ }\r
+ \r
+ public final String getStr(Object name) {\r
+ return c().getString(String.valueOf(name));\r
+ }\r
\r
public final String[] keys() {\r
- return this.<JsCache>cast().keys();\r
+ return c().keys();\r
+ }\r
+ \r
+ public final <T> void remove(T name) {\r
+ c().delete(String.valueOf(name));\r
+ }\r
+ \r
+ public final <T> void set(T name, Object val) {\r
+ c().put(String.valueOf(name), val);\r
}\r
-\r
- public final native void set(String key, Object val) /*-{\r
- this[key]=val;\r
- }-*/;\r
\r
public final String tostring() {\r
String ret = "";\r
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"));
}
}
}
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
.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"));
}