summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>2007-06-06 11:24:52 +0000
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>2007-06-06 11:24:52 +0000
commit7ca4a2cb81c58ed70758c31d8d89e4b56bf2e96f (patch)
tree17d97c2990cf5f835a97cdaae38de1ab36548413 /src
parentbbe97c9d7df9f8dc10ce1c6068981107e8c13de6 (diff)
downloadvaadin-framework-7ca4a2cb81c58ed70758c31d8d89e4b56bf2e96f.tar.gz
vaadin-framework-7ca4a2cb81c58ed70758c31d8d89e4b56bf2e96f.zip
Feature complete?
svn changeset:1619/svn branch:trunk
Diffstat (limited to 'src')
-rw-r--r--src/com/itmill/toolkit/terminal/gwt/client/UIDL.java161
1 files changed, 125 insertions, 36 deletions
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java b/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java
index 54a6eeda08..d0235dfc4b 100644
--- a/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java
+++ b/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java
@@ -1,5 +1,6 @@
package com.itmill.toolkit.terminal.gwt.client;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
@@ -13,74 +14,98 @@ import com.google.gwt.json.client.JSONValue;
public class UIDL {
JSONArray json;
-
+
public UIDL(JSONArray json) {
this.json = json;
}
-
+
public String getId() {
return getStringAttribute("id");
}
-
+
public String getTag() {
return json.get(0).toString();
}
-
+
public String getStringAttribute(String name) {
- JSONValue val = ((JSONObject)json.get(1)).get(name);
- return ((JSONString)val).stringValue();
+ JSONValue val = ((JSONObject) json.get(1)).get(name);
+ return ((JSONString) val).stringValue();
+ }
+
+ public String getAttribute(String name) {
+ JSONValue val = ((JSONObject) json.get(1)).get(name);
+ if (val == null)
+ return null;
+ return val.toString();
}
-
+
+ public Set getAttributeNames() {
+ HashSet attrs = new HashSet(((JSONObject) json.get(1)).keySet());
+ attrs.remove("v");
+ return attrs;
+ }
+
public int getIntAttribute(String name) {
- JSONValue val = ((JSONObject)json.get(1)).get(name);
- double num = ((JSONNumber)val).getValue();
+ JSONValue val = ((JSONObject) json.get(1)).get(name);
+ double num = ((JSONNumber) val).getValue();
return (int) num;
}
-
+
public long getLongAttribute(String name) {
- JSONValue val = ((JSONObject)json.get(1)).get(name);
- double num = ((JSONNumber)val).getValue();
+ JSONValue val = ((JSONObject) json.get(1)).get(name);
+ double num = ((JSONNumber) val).getValue();
return (long) num;
}
-
+
public boolean getBooleanAttribute(String name) {
- JSONValue val = ((JSONObject)json.get(1)).get(name);
- return ((JSONBoolean)val).booleanValue();
+ JSONValue val = ((JSONObject) json.get(1)).get(name);
+ return ((JSONBoolean) val).booleanValue();
}
-
+
public Iterator getChildIterator() {
-
+
return new Iterator() {
-
- int index=2;
-
+
+ int index = 2;
+
public void remove() {
throw new UnsupportedOperationException();
}
-
+
public Object next() {
-
+
if (json.size() > index) {
JSONValue c = json.get(index++);
- if (c.isString() != null) return c.isString().stringValue();
- else if (c.isArray() != null) return new UIDL(c.isArray());
- else if (c.isObject() != null) return new XML(c.isObject());
- else throw new IllegalStateException("Illegal child of type "+c.getClass().toString()+" in tag " + getTag() + " at index " + index);
+ if (c.isString() != null)
+ return c.isString().stringValue();
+ else if (c.isArray() != null)
+ return new UIDL(c.isArray());
+ else if (c.isObject() != null)
+ return new XML(c.isObject());
+ else
+ throw new IllegalStateException(
+ "Illegal child of type "
+ + c.getClass().toString() + " in tag "
+ + getTag() + " at index " + index);
}
return null;
}
-
+
public boolean hasNext() {
return json.size() > index;
}
-
+
};
}
-
+
public String toString() {
- String s = "<"+getTag()+ " ";
-
- s +="id="+getId();
+ String s = "<" + getTag();
+
+ for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
+ String name = i.next().toString();
+ s += " " + name + "=\"" + getAttribute(name) + "\"";
+ }
+
s += ">\n";
Iterator i = getChildIterator();
@@ -89,17 +114,81 @@ public class UIDL {
s += c.toString();
}
- s += "</"+getTag()+">\n";
-
+ s += "</" + getTag() + ">\n";
+
return s;
}
-
+
+ public String getStringVariable(String name) {
+ JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
+ if (v == null)
+ throw new IllegalArgumentException(
+ "No variables defined in tag. (requested: " + name + ")");
+ JSONString t = (JSONString) v.get(name);
+ if (t == null)
+ throw new IllegalArgumentException("No such variable: " + name);
+ return t.stringValue();
+ }
+
+ public int getIntVariable(String name) {
+ JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
+ if (v == null)
+ throw new IllegalArgumentException(
+ "No variables defined in tag. (requested: " + name + ")");
+ JSONNumber t = (JSONNumber) v.get(name);
+ if (t == null)
+ throw new IllegalArgumentException("No such variable: " + name);
+ return (int) t.getValue();
+ }
+
+ public boolean getBooleanVariable(String name) {
+ JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
+ if (v == null)
+ throw new IllegalArgumentException(
+ "No variables defined in tag. (requested: " + name + ")");
+ JSONBoolean t = (JSONBoolean) v.get(name);
+ if (t == null)
+ throw new IllegalArgumentException("No such variable: " + name);
+ return t.booleanValue();
+ }
+
+ public JSONArray getArrayVariable(String name) {
+ JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
+ if (v == null)
+ throw new IllegalArgumentException(
+ "No variables defined in tag. (requested: " + name + ")");
+ JSONArray t = (JSONArray) v.get(name);
+ if (t == null)
+ throw new IllegalArgumentException("No such variable: " + name);
+ return t;
+ }
+
+ public String[] getStingArrayVariable(String name) {
+ JSONArray a = getArrayVariable(name);
+ String[] s = new String[a.size()];
+ for (int i = 0; i < a.size(); i++)
+ s[i] = ((JSONString) a.get(i)).stringValue();
+ return s;
+ }
+
+ public int[] getIntArrayVariable(String name) {
+ JSONArray a = getArrayVariable(name);
+ int[] s = new int[a.size()];
+ for (int i = 0; i < a.size(); i++) {
+ JSONValue v = a.get(i);
+ s[i] = v.isNumber() != null ? (int) ((JSONNumber) v).getValue()
+ : Integer.parseInt(v.toString());
+ }
+ return s;
+ }
+
public class XML {
JSONObject x;
+
private XML(JSONObject x) {
this.x = x;
}
-
+
public String getXMLAsString() {
return x.get("x").toString();
}