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.

UIDL.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import com.google.gwt.json.client.JSONArray;
  6. import com.google.gwt.json.client.JSONBoolean;
  7. import com.google.gwt.json.client.JSONNumber;
  8. import com.google.gwt.json.client.JSONObject;
  9. import com.google.gwt.json.client.JSONString;
  10. import com.google.gwt.json.client.JSONValue;
  11. import com.google.gwt.user.client.ui.Tree;
  12. import com.google.gwt.user.client.ui.TreeItem;
  13. import com.google.gwt.user.client.ui.TreeListener;
  14. public class UIDL {
  15. JSONArray json;
  16. public UIDL(JSONArray json) {
  17. this.json = json;
  18. }
  19. public String getId() {
  20. JSONValue val = ((JSONObject) json.get(1)).get("id");
  21. if (val == null)
  22. return null;
  23. return ((JSONString) val).stringValue();
  24. }
  25. public String getTag() {
  26. return ((JSONString) json.get(0)).stringValue();
  27. }
  28. public String getStringAttribute(String name) {
  29. JSONValue val = ((JSONObject) json.get(1)).get(name);
  30. if (val == null)
  31. return null;
  32. return ((JSONString) val).stringValue();
  33. }
  34. public Set getAttributeNames() {
  35. HashSet attrs = new HashSet(((JSONObject) json.get(1)).keySet());
  36. attrs.remove("v");
  37. return attrs;
  38. }
  39. public int getIntAttribute(String name) {
  40. JSONValue val = ((JSONObject) json.get(1)).get(name);
  41. if (val == null)
  42. return 0;
  43. double num = ((JSONNumber) val).getValue();
  44. return (int) num;
  45. }
  46. public long getLongAttribute(String name) {
  47. JSONValue val = ((JSONObject) json.get(1)).get(name);
  48. if (val == null)
  49. return 0;
  50. double num = ((JSONNumber) val).getValue();
  51. return (long) num;
  52. }
  53. public float getFloatAttribute(String name) {
  54. JSONValue val = ((JSONObject) json.get(1)).get(name);
  55. if (val == null)
  56. return 0;
  57. double num = ((JSONNumber) val).getValue();
  58. return (float) num;
  59. }
  60. public double getDoubleAttribute(String name) {
  61. JSONValue val = ((JSONObject) json.get(1)).get(name);
  62. if (val == null)
  63. return 0;
  64. double num = ((JSONNumber) val).getValue();
  65. return (double) num;
  66. }
  67. public boolean getBooleanAttribute(String name) {
  68. JSONValue val = ((JSONObject) json.get(1)).get(name);
  69. if (val == null)
  70. return false;
  71. return ((JSONBoolean) val).booleanValue();
  72. }
  73. public String[] getStringArrayAttribute(String name) {
  74. JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get(name);
  75. String[] s = new String[a.size()];
  76. for (int i = 0; i < a.size(); i++)
  77. s[i] = ((JSONString) a.get(i)).stringValue();
  78. return s;
  79. }
  80. public HashSet getStringArrayAttributeAsSet(String string) {
  81. JSONArray a = getArrayVariable(string);
  82. HashSet s = new HashSet();
  83. for (int i = 0; i < a.size(); i++)
  84. s.add(((JSONString) a.get(i)).stringValue());
  85. return s;
  86. }
  87. /**
  88. * Get attributes value as string whateever the type is
  89. *
  90. * @param name
  91. * @return string presentation of attribute
  92. */
  93. private String getAttribute(String name) {
  94. return json.get(1).isObject().get(name).toString();
  95. }
  96. public boolean hasAttribute(String name) {
  97. return ((JSONObject) json.get(1)).get(name) != null;
  98. }
  99. public UIDL getChildUIDL(int i) {
  100. JSONValue c = json.get(i + 2);
  101. if (c == null)
  102. return null;
  103. if (c.isArray() != null)
  104. return new UIDL(c.isArray());
  105. throw new IllegalStateException("Child node " + i
  106. + " is not of type UIDL");
  107. }
  108. public String getChildString(int i) {
  109. JSONValue c = json.get(i + 2);
  110. if (c.isString() != null)
  111. return ((JSONString) c).stringValue();
  112. throw new IllegalStateException("Child node " + i
  113. + " is not of type String");
  114. }
  115. public Iterator getChildIterator() {
  116. return new Iterator() {
  117. int index = 2;
  118. public void remove() {
  119. throw new UnsupportedOperationException();
  120. }
  121. public Object next() {
  122. if (json.size() > index) {
  123. JSONValue c = json.get(index++);
  124. if (c.isString() != null)
  125. return c.isString().stringValue();
  126. else if (c.isArray() != null)
  127. return new UIDL(c.isArray());
  128. else if (c.isObject() != null)
  129. return new XML(c.isObject());
  130. else
  131. throw new IllegalStateException("Illegal child " + c
  132. + " in tag " + getTag() + " at index " + index);
  133. }
  134. return null;
  135. }
  136. public boolean hasNext() {
  137. return json.size() > index;
  138. }
  139. };
  140. }
  141. public int getNumberOfChildren() {
  142. return json.size() - 2;
  143. }
  144. public String toString() {
  145. String s = "<" + getTag();
  146. for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
  147. String name = i.next().toString();
  148. s += " " + name + "=";
  149. JSONValue v = ((JSONObject) json.get(1)).get(name);
  150. if (v.isString() != null) s += v;
  151. else s += "\"" + v + "\"";
  152. }
  153. s += ">\n";
  154. Iterator i = getChildIterator();
  155. while (i.hasNext()) {
  156. Object c = i.next();
  157. s += c.toString();
  158. }
  159. s += "</" + getTag() + ">\n";
  160. return s;
  161. }
  162. public String getChildrenAsXML() {
  163. String s="";
  164. Iterator i = getChildIterator();
  165. while (i.hasNext()) {
  166. Object c = i.next();
  167. s += c.toString();
  168. }
  169. return s;
  170. }
  171. public UIDLBrowser print_r() {
  172. return new UIDLBrowser();
  173. }
  174. private class UIDLBrowser extends Tree {
  175. public UIDLBrowser() {
  176. final TreeItem root = new TreeItem(getTag());
  177. addItem(root);
  178. root.addItem("");
  179. addTreeListener(new TreeListener() {
  180. public void onTreeItemStateChanged(TreeItem item) {
  181. if (item == root) {
  182. removeItem(root);
  183. UIDLBrowser.this.removeTreeListener(this);
  184. addItem(dir());
  185. Iterator it = treeItemIterator();
  186. while (it.hasNext())
  187. ((TreeItem) it.next()).setState(true);
  188. }
  189. }
  190. public void onTreeItemSelected(TreeItem item) {
  191. }
  192. });
  193. }
  194. }
  195. public TreeItem dir() {
  196. String nodeName = getTag();
  197. for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
  198. String name = i.next().toString();
  199. String value = getAttribute(name);
  200. nodeName += " " + name + "=" + value;
  201. }
  202. TreeItem item = new TreeItem(nodeName);
  203. try {
  204. TreeItem tmp = null;
  205. for (Iterator i = getVariableHash().keySet().iterator(); i
  206. .hasNext();) {
  207. String name = i.next().toString();
  208. String value = "";
  209. try {
  210. value = getStringVariable(name);
  211. } catch (Exception e) {
  212. try {
  213. JSONArray a = getArrayVariable(name);
  214. value = a.toString();
  215. } catch (Exception e2) {
  216. try {
  217. int intVal = getIntVariable(name);
  218. value = String.valueOf(intVal);
  219. } catch (Exception e3) {
  220. value = "unknown";
  221. }
  222. }
  223. }
  224. if (tmp == null)
  225. tmp = new TreeItem("variables");
  226. tmp.addItem(name + "=" + value);
  227. }
  228. if (tmp != null)
  229. item.addItem(tmp);
  230. } catch (Exception e) {
  231. // Ingonered, no variables
  232. }
  233. Iterator i = getChildIterator();
  234. while (i.hasNext()) {
  235. Object child = i.next();
  236. try {
  237. UIDL c = (UIDL) child;
  238. item.addItem(c.dir());
  239. } catch (Exception e) {
  240. item.addItem(child.toString());
  241. }
  242. }
  243. return item;
  244. }
  245. private JSONObject getVariableHash() {
  246. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  247. if (v == null)
  248. throw new IllegalArgumentException("No variables defined in tag.");
  249. return v;
  250. }
  251. public boolean hasVariable(String name) {
  252. Object v = null;
  253. try {
  254. v = getVariableHash().get(name);
  255. } catch(IllegalArgumentException e) {}
  256. return v != null;
  257. }
  258. public String getStringVariable(String name) {
  259. JSONString t = (JSONString) getVariableHash().get(name);
  260. if (t == null)
  261. throw new IllegalArgumentException("No such variable: " + name);
  262. return t.stringValue();
  263. }
  264. public int getIntVariable(String name) {
  265. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  266. if (t == null)
  267. throw new IllegalArgumentException("No such variable: " + name);
  268. return (int) t.getValue();
  269. }
  270. public long getLongVariable(String name) {
  271. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  272. if (t == null)
  273. throw new IllegalArgumentException("No such variable: " + name);
  274. return (long) t.getValue();
  275. }
  276. public float getFloatVariable(String name) {
  277. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  278. if (t == null)
  279. throw new IllegalArgumentException("No such variable: " + name);
  280. return (float) t.getValue();
  281. }
  282. public double getDoubleVariable(String name) {
  283. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  284. if (t == null)
  285. throw new IllegalArgumentException("No such variable: " + name);
  286. return (double) t.getValue();
  287. }
  288. public boolean getBooleanVariable(String name) {
  289. JSONBoolean t = (JSONBoolean) getVariableHash().get(name);
  290. if (t == null)
  291. throw new IllegalArgumentException("No such variable: " + name);
  292. return t.booleanValue();
  293. }
  294. private JSONArray getArrayVariable(String name) {
  295. JSONArray t = (JSONArray) getVariableHash().get(name);
  296. if (t == null)
  297. throw new IllegalArgumentException("No such variable: " + name);
  298. return t;
  299. }
  300. public String[] getStringArrayVariable(String name) {
  301. JSONArray a = getArrayVariable(name);
  302. String[] s = new String[a.size()];
  303. for (int i = 0; i < a.size(); i++)
  304. s[i] = ((JSONString) a.get(i)).stringValue();
  305. return s;
  306. }
  307. public Set getStringArrayVariableAsSet(String name) {
  308. JSONArray a = getArrayVariable(name);
  309. HashSet s = new HashSet();
  310. for (int i = 0; i < a.size(); i++)
  311. s.add(((JSONString) a.get(i)).stringValue());
  312. return s;
  313. }
  314. public int[] getIntArrayVariable(String name) {
  315. JSONArray a = getArrayVariable(name);
  316. int[] s = new int[a.size()];
  317. for (int i = 0; i < a.size(); i++) {
  318. JSONValue v = a.get(i);
  319. s[i] = v.isNumber() != null ? (int) ((JSONNumber) v).getValue()
  320. : Integer.parseInt(v.toString());
  321. }
  322. return s;
  323. }
  324. public class XML {
  325. JSONObject x;
  326. private XML(JSONObject x) {
  327. this.x = x;
  328. }
  329. public String getXMLAsString() {
  330. return x.get("x").toString();
  331. }
  332. }
  333. public int getChidlCount() {
  334. return json.size()-2;
  335. }
  336. }