123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- package com.itmill.toolkit.terminal.gwt.client;
-
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
-
- import com.google.gwt.json.client.JSONArray;
- import com.google.gwt.json.client.JSONBoolean;
- import com.google.gwt.json.client.JSONNumber;
- import com.google.gwt.json.client.JSONObject;
- import com.google.gwt.json.client.JSONString;
- import com.google.gwt.json.client.JSONValue;
- import com.google.gwt.user.client.ui.Tree;
- import com.google.gwt.user.client.ui.TreeItem;
- import com.google.gwt.user.client.ui.TreeListener;
-
- public class UIDL {
-
- JSONArray json;
-
- public UIDL(JSONArray json) {
- this.json = json;
- }
-
- public String getId() {
- JSONValue val = ((JSONObject) json.get(1)).get("id");
- if (val == null)
- return null;
- return ((JSONString) val).stringValue();
- }
-
- public String getTag() {
- return ((JSONString) json.get(0)).stringValue();
- }
-
- public String getStringAttribute(String name) {
- JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null)
- return null;
- return ((JSONString) val).stringValue();
- }
-
- 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);
- if (val == null)
- return 0;
- double num = ((JSONNumber) val).getValue();
- return (int) num;
- }
-
- public long getLongAttribute(String name) {
- JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null)
- return 0;
- double num = ((JSONNumber) val).getValue();
- return (long) num;
- }
-
- public boolean getBooleanAttribute(String name) {
- JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null)
- return false;
- return ((JSONBoolean) val).booleanValue();
- }
-
- public String[] getStringArrayAttribute(String name) {
- JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get(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 HashSet getStringArrayAttributeAsSet(String string) {
- JSONArray a = getArrayVariable(string);
- HashSet s = new HashSet();
- for (int i = 0; i < a.size(); i++)
- s.add(((JSONString) a.get(i)).stringValue());
- return s;
- }
-
- /**
- * Get attributes value as string whateever the type is
- *
- * @param name
- * @return string presentation of attribute
- */
- private String getAttribute(String name) {
- return json.get(1).isObject().get(name).toString();
- }
-
- public boolean hasAttribute(String name) {
- return ((JSONObject) json.get(1)).get(name) != null;
- }
-
- public UIDL getChildUIDL(int i) {
-
- JSONValue c = json.get(i + 2);
- if (c == null)
- return null;
- if (c.isArray() != null)
- return new UIDL(c.isArray());
- throw new IllegalStateException("Child node " + i
- + " is not of type UIDL");
- }
-
- public String getChildString(int i) {
-
- JSONValue c = json.get(i + 2);
- if (c.isString() != null)
- return ((JSONString) c).stringValue();
- throw new IllegalStateException("Child node " + i
- + " is not of type String");
- }
-
- public Iterator getChildIterator() {
-
- return new Iterator() {
-
- 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 " + c
- + " in tag " + getTag() + " at index " + index);
- }
- return null;
- }
-
- public boolean hasNext() {
- return json.size() > index;
- }
-
- };
- }
-
- public int getNumberOfChildren() {
- return json.size() - 2;
- }
-
- public String toString() {
- String s = "<" + getTag();
-
- for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
- String name = i.next().toString();
- s += " " + name + "=";
- JSONValue v = ((JSONObject) json.get(1)).get(name);
- if (v.isString() != null) s += v;
- else s += "\"" + v + "\"";
- }
-
- s += ">\n";
-
- Iterator i = getChildIterator();
- while (i.hasNext()) {
- Object c = i.next();
- s += c.toString();
- }
-
- s += "</" + getTag() + ">\n";
-
- return s;
- }
-
- public String getChildrenAsXML() {
- String s="";
- Iterator i = getChildIterator();
- while (i.hasNext()) {
- Object c = i.next();
- s += c.toString();
- }
- return s;
- }
-
- public UIDLBrowser print_r() {
- return new UIDLBrowser();
- }
-
- private class UIDLBrowser extends Tree {
- public UIDLBrowser() {
- final TreeItem root = new TreeItem(getTag());
- addItem(root);
- root.addItem("");
- addTreeListener(new TreeListener() {
-
- public void onTreeItemStateChanged(TreeItem item) {
- if (item == root) {
- removeItem(root);
- UIDLBrowser.this.removeTreeListener(this);
- addItem(dir());
- Iterator it = treeItemIterator();
- while (it.hasNext())
- ((TreeItem) it.next()).setState(true);
- }
- }
-
- public void onTreeItemSelected(TreeItem item) {
- }
-
- });
- }
- }
-
- public TreeItem dir() {
-
- String nodeName = getTag();
- for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
- String name = i.next().toString();
- String value = getAttribute(name);
- nodeName += " " + name + "=" + value;
- }
- TreeItem item = new TreeItem(nodeName);
-
- try {
- TreeItem tmp = null;
- for (Iterator i = getVariableHash().keySet().iterator(); i
- .hasNext();) {
- String name = i.next().toString();
- String value = "";
- try {
- value = getStringVariable(name);
- } catch (Exception e) {
- try {
- JSONArray a = getArrayVariable(name);
- value = a.toString();
- } catch (Exception e2) {
- try {
- int intVal = getIntVariable(name);
- value = String.valueOf(intVal);
- } catch (Exception e3) {
- value = "unknown";
- }
- }
- }
- if (tmp == null)
- tmp = new TreeItem("variables");
- tmp.addItem(name + "=" + value);
- }
- if (tmp != null)
- item.addItem(tmp);
- } catch (Exception e) {
- // Ingonered, no variables
- }
-
- Iterator i = getChildIterator();
- while (i.hasNext()) {
- Object child = i.next();
- try {
- UIDL c = (UIDL) child;
- item.addItem(c.dir());
-
- } catch (Exception e) {
- item.addItem(child.toString());
- }
- }
- return item;
- }
-
- private JSONObject getVariableHash() {
- JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
- if (v == null)
- throw new IllegalArgumentException("No variables defined in tag.");
- return v;
- }
-
- public boolean hasVariable(String name) {
- Object v = null;
- try {
- v = getVariableHash().get(name);
- } catch(IllegalArgumentException e) {}
- return v != null;
- }
-
- public String getStringVariable(String name) {
- JSONString t = (JSONString) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return t.stringValue();
- }
-
- public int getIntVariable(String name) {
- JSONNumber t = (JSONNumber) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return (int) t.getValue();
- }
-
- public long getLongVariable(String name) {
- JSONNumber t = (JSONNumber) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return (long) t.getValue();
- }
-
- public float getFloatVariable(String name) {
- JSONNumber t = (JSONNumber) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return (float) t.getValue();
- }
-
- public boolean getBooleanVariable(String name) {
- JSONBoolean t = (JSONBoolean) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return t.booleanValue();
- }
-
- private JSONArray getArrayVariable(String name) {
- JSONArray t = (JSONArray) getVariableHash().get(name);
- if (t == null)
- throw new IllegalArgumentException("No such variable: " + name);
- return t;
- }
-
- public String[] getStringArrayVariable(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 Set getStringArrayVariableAsSet(String name) {
- JSONArray a = getArrayVariable(name);
- HashSet s = new HashSet();
- for (int i = 0; i < a.size(); i++)
- s.add(((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();
- }
- }
-
- public int getChidlCount() {
- return json.size()-2;
- }
- }
|