123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- 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.DOM;
- 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() {
- final 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) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return null;
- }
- return ((JSONString) val).stringValue();
- }
-
- public Set getAttributeNames() {
- final HashSet attrs = new HashSet(((JSONObject) json.get(1)).keySet());
- attrs.remove("v");
- return attrs;
- }
-
- public int getIntAttribute(String name) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return 0;
- }
- final double num = ((JSONNumber) val).getValue();
- return (int) num;
- }
-
- public long getLongAttribute(String name) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return 0;
- }
- final double num = ((JSONNumber) val).getValue();
- return (long) num;
- }
-
- public float getFloatAttribute(String name) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return 0;
- }
- final double num = ((JSONNumber) val).getValue();
- return (float) num;
- }
-
- public double getDoubleAttribute(String name) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return 0;
- }
- final double num = ((JSONNumber) val).getValue();
- return num;
- }
-
- public boolean getBooleanAttribute(String name) {
- final JSONValue val = ((JSONObject) json.get(1)).get(name);
- if (val == null) {
- return false;
- }
- return ((JSONBoolean) val).booleanValue();
- }
-
- public String[] getStringArrayAttribute(String name) {
- final JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get(name);
- final 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[] getIntArrayAttribute(String name) {
- final JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get(name);
- final int[] s = new int[a.size()];
- for (int i = 0; i < a.size(); i++) {
- s[i] = Integer.parseInt(((JSONString) a.get(i)).stringValue());
- }
- return s;
- }
-
- public HashSet getStringArrayAttributeAsSet(String string) {
- final JSONArray a = getArrayVariable(string);
- final 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) {
-
- final 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) {
-
- final 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) {
- final 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;
- }
-
- @Override
- public String toString() {
- String s = "<" + getTag();
-
- for (final Iterator i = getAttributeNames().iterator(); i.hasNext();) {
- final String name = i.next().toString();
- s += " " + name + "=";
- final JSONValue v = ((JSONObject) json.get(1)).get(name);
- if (v.isString() != null) {
- s += v;
- } else {
- s += "\"" + v + "\"";
- }
- }
-
- s += ">\n";
-
- final Iterator i = getChildIterator();
- while (i.hasNext()) {
- final Object c = i.next();
- s += c.toString();
- }
-
- s += "</" + getTag() + ">\n";
-
- return s;
- }
-
- public String getChildrenAsXML() {
- String s = "";
- final Iterator i = getChildIterator();
- while (i.hasNext()) {
- final Object c = i.next();
- s += c.toString();
- }
- return s;
- }
-
- public IUIDLBrowser print_r() {
- return new IUIDLBrowser();
- }
-
- private class IUIDLBrowser extends Tree {
- public IUIDLBrowser() {
-
- DOM.setStyleAttribute(getElement(), "position", "");
-
- final TreeItem root = new TreeItem(getTag());
- addItem(root);
- root.addItem("");
- addTreeListener(new TreeListener() {
-
- public void onTreeItemStateChanged(TreeItem item) {
- if (item == root) {
- removeItem(root);
- IUIDLBrowser.this.removeTreeListener(this);
- addItem(dir());
- final Iterator it = treeItemIterator();
- while (it.hasNext()) {
- ((TreeItem) it.next()).setState(true);
- }
- }
- }
-
- public void onTreeItemSelected(TreeItem item) {
- }
-
- });
-
- }
-
- @Override
- protected boolean isKeyboardNavigationEnabled(TreeItem currentItem) {
- return false;
- }
-
- }
-
- public TreeItem dir() {
-
- String nodeName = getTag();
- for (final Iterator i = getAttributeNames().iterator(); i.hasNext();) {
- final String name = i.next().toString();
- final String value = getAttribute(name);
- nodeName += " " + name + "=" + value;
- }
- final TreeItem item = new TreeItem(nodeName);
-
- try {
- TreeItem tmp = null;
- for (final Iterator i = getVariableHash().keySet().iterator(); i
- .hasNext();) {
- final String name = i.next().toString();
- String value = "";
- try {
- value = getStringVariable(name);
- } catch (final Exception e) {
- try {
- final JSONArray a = getArrayVariable(name);
- value = a.toString();
- } catch (final Exception e2) {
- try {
- final int intVal = getIntVariable(name);
- value = String.valueOf(intVal);
- } catch (final Exception e3) {
- value = "unknown";
- }
- }
- }
- if (tmp == null) {
- tmp = new TreeItem("variables");
- }
- tmp.addItem(name + "=" + value);
- }
- if (tmp != null) {
- item.addItem(tmp);
- }
- } catch (final Exception e) {
- // Ignored, no variables
- }
-
- final Iterator i = getChildIterator();
- while (i.hasNext()) {
- final Object child = i.next();
- try {
- final UIDL c = (UIDL) child;
- item.addItem(c.dir());
-
- } catch (final Exception e) {
- item.addItem(child.toString());
- }
- }
- return item;
- }
-
- private JSONObject getVariableHash() {
- final 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 (final IllegalArgumentException e) {
- }
- return v != null;
- }
-
- public String getStringVariable(String name) {
- final JSONString t = (JSONString) getVariableHash().get(name);
- if (t == null) {
- throw new IllegalArgumentException("No such variable: " + name);
- }
- return t.stringValue();
- }
-
- public int getIntVariable(String name) {
- final 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) {
- final 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) {
- final JSONNumber t = (JSONNumber) getVariableHash().get(name);
- if (t == null) {
- throw new IllegalArgumentException("No such variable: " + name);
- }
- return (float) t.getValue();
- }
-
- public double getDoubleVariable(String name) {
- final JSONNumber t = (JSONNumber) getVariableHash().get(name);
- if (t == null) {
- throw new IllegalArgumentException("No such variable: " + name);
- }
- return t.getValue();
- }
-
- public boolean getBooleanVariable(String name) {
- final JSONBoolean t = (JSONBoolean) getVariableHash().get(name);
- if (t == null) {
- throw new IllegalArgumentException("No such variable: " + name);
- }
- return t.booleanValue();
- }
-
- private JSONArray getArrayVariable(String name) {
- final JSONArray t = (JSONArray) getVariableHash().get(name);
- if (t == null) {
- throw new IllegalArgumentException("No such variable: " + name);
- }
- return t;
- }
-
- public String[] getStringArrayVariable(String name) {
- final JSONArray a = getArrayVariable(name);
- final 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<String> getStringArrayVariableAsSet(String name) {
- final JSONArray a = getArrayVariable(name);
- final HashSet<String> s = new HashSet<String>();
- for (int i = 0; i < a.size(); i++) {
- s.add(((JSONString) a.get(i)).stringValue());
- }
- return s;
- }
-
- public int[] getIntArrayVariable(String name) {
- final JSONArray a = getArrayVariable(name);
- final int[] s = new int[a.size()];
- for (int i = 0; i < a.size(); i++) {
- final 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() {
- final StringBuffer sb = new StringBuffer();
- for (final Iterator it = x.keySet().iterator(); it.hasNext();) {
- final String tag = (String) it.next();
- sb.append("<");
- sb.append(tag);
- sb.append(">");
- sb.append(x.get(tag).isString().stringValue());
- sb.append("</");
- sb.append(tag);
- sb.append(">");
- }
- return sb.toString();
- }
- }
-
- public int getChildCount() {
- return json.size() - 2;
- }
-
- public UIDL getErrors() {
- final JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get("error");
- return new UIDL(a);
- }
-
- }
|