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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 int[] getIntArrayAttribute(String name) {
  81. JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get(name);
  82. int[] s = new int[a.size()];
  83. for (int i = 0; i < a.size(); i++)
  84. s[i] = Integer.parseInt(((JSONString) a.get(i)).stringValue());
  85. return s;
  86. }
  87. public HashSet getStringArrayAttributeAsSet(String string) {
  88. JSONArray a = getArrayVariable(string);
  89. HashSet s = new HashSet();
  90. for (int i = 0; i < a.size(); i++)
  91. s.add(((JSONString) a.get(i)).stringValue());
  92. return s;
  93. }
  94. /**
  95. * Get attributes value as string whateever the type is
  96. *
  97. * @param name
  98. * @return string presentation of attribute
  99. */
  100. private String getAttribute(String name) {
  101. return json.get(1).isObject().get(name).toString();
  102. }
  103. public boolean hasAttribute(String name) {
  104. return ((JSONObject) json.get(1)).get(name) != null;
  105. }
  106. public UIDL getChildUIDL(int i) {
  107. JSONValue c = json.get(i + 2);
  108. if (c == null)
  109. return null;
  110. if (c.isArray() != null)
  111. return new UIDL(c.isArray());
  112. throw new IllegalStateException("Child node " + i
  113. + " is not of type UIDL");
  114. }
  115. public String getChildString(int i) {
  116. JSONValue c = json.get(i + 2);
  117. if (c.isString() != null)
  118. return ((JSONString) c).stringValue();
  119. throw new IllegalStateException("Child node " + i
  120. + " is not of type String");
  121. }
  122. public Iterator getChildIterator() {
  123. return new Iterator() {
  124. int index = 2;
  125. public void remove() {
  126. throw new UnsupportedOperationException();
  127. }
  128. public Object next() {
  129. if (json.size() > index) {
  130. JSONValue c = json.get(index++);
  131. if (c.isString() != null)
  132. return c.isString().stringValue();
  133. else if (c.isArray() != null)
  134. return new UIDL(c.isArray());
  135. else if (c.isObject() != null)
  136. return new XML(c.isObject());
  137. else
  138. throw new IllegalStateException("Illegal child " + c
  139. + " in tag " + getTag() + " at index " + index);
  140. }
  141. return null;
  142. }
  143. public boolean hasNext() {
  144. return json.size() > index;
  145. }
  146. };
  147. }
  148. public int getNumberOfChildren() {
  149. return json.size() - 2;
  150. }
  151. public String toString() {
  152. String s = "<" + getTag();
  153. for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
  154. String name = i.next().toString();
  155. s += " " + name + "=";
  156. JSONValue v = ((JSONObject) json.get(1)).get(name);
  157. if (v.isString() != null)
  158. s += v;
  159. else
  160. s += "\"" + v + "\"";
  161. }
  162. s += ">\n";
  163. Iterator i = getChildIterator();
  164. while (i.hasNext()) {
  165. Object c = i.next();
  166. s += c.toString();
  167. }
  168. s += "</" + getTag() + ">\n";
  169. return s;
  170. }
  171. public String getChildrenAsXML() {
  172. String s = "";
  173. Iterator i = getChildIterator();
  174. while (i.hasNext()) {
  175. Object c = i.next();
  176. s += c.toString();
  177. }
  178. return s;
  179. }
  180. public UIDLBrowser print_r() {
  181. return new UIDLBrowser();
  182. }
  183. private class UIDLBrowser extends Tree {
  184. public UIDLBrowser() {
  185. final TreeItem root = new TreeItem(getTag());
  186. addItem(root);
  187. root.addItem("");
  188. addTreeListener(new TreeListener() {
  189. public void onTreeItemStateChanged(TreeItem item) {
  190. if (item == root) {
  191. removeItem(root);
  192. UIDLBrowser.this.removeTreeListener(this);
  193. addItem(dir());
  194. Iterator it = treeItemIterator();
  195. while (it.hasNext())
  196. ((TreeItem) it.next()).setState(true);
  197. }
  198. }
  199. public void onTreeItemSelected(TreeItem item) {
  200. }
  201. });
  202. }
  203. }
  204. public TreeItem dir() {
  205. String nodeName = getTag();
  206. for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
  207. String name = i.next().toString();
  208. String value = getAttribute(name);
  209. nodeName += " " + name + "=" + value;
  210. }
  211. TreeItem item = new TreeItem(nodeName);
  212. try {
  213. TreeItem tmp = null;
  214. for (Iterator i = getVariableHash().keySet().iterator(); i
  215. .hasNext();) {
  216. String name = i.next().toString();
  217. String value = "";
  218. try {
  219. value = getStringVariable(name);
  220. } catch (Exception e) {
  221. try {
  222. JSONArray a = getArrayVariable(name);
  223. value = a.toString();
  224. } catch (Exception e2) {
  225. try {
  226. int intVal = getIntVariable(name);
  227. value = String.valueOf(intVal);
  228. } catch (Exception e3) {
  229. value = "unknown";
  230. }
  231. }
  232. }
  233. if (tmp == null)
  234. tmp = new TreeItem("variables");
  235. tmp.addItem(name + "=" + value);
  236. }
  237. if (tmp != null)
  238. item.addItem(tmp);
  239. } catch (Exception e) {
  240. // Ingonered, no variables
  241. }
  242. Iterator i = getChildIterator();
  243. while (i.hasNext()) {
  244. Object child = i.next();
  245. try {
  246. UIDL c = (UIDL) child;
  247. item.addItem(c.dir());
  248. } catch (Exception e) {
  249. item.addItem(child.toString());
  250. }
  251. }
  252. return item;
  253. }
  254. private JSONObject getVariableHash() {
  255. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  256. if (v == null)
  257. throw new IllegalArgumentException("No variables defined in tag.");
  258. return v;
  259. }
  260. public boolean hasVariable(String name) {
  261. Object v = null;
  262. try {
  263. v = getVariableHash().get(name);
  264. } catch (IllegalArgumentException e) {
  265. }
  266. return v != null;
  267. }
  268. public String getStringVariable(String name) {
  269. JSONString t = (JSONString) getVariableHash().get(name);
  270. if (t == null)
  271. throw new IllegalArgumentException("No such variable: " + name);
  272. return t.stringValue();
  273. }
  274. public int getIntVariable(String name) {
  275. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  276. if (t == null)
  277. throw new IllegalArgumentException("No such variable: " + name);
  278. return (int) t.getValue();
  279. }
  280. public long getLongVariable(String name) {
  281. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  282. if (t == null)
  283. throw new IllegalArgumentException("No such variable: " + name);
  284. return (long) t.getValue();
  285. }
  286. public float getFloatVariable(String name) {
  287. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  288. if (t == null)
  289. throw new IllegalArgumentException("No such variable: " + name);
  290. return (float) t.getValue();
  291. }
  292. public double getDoubleVariable(String name) {
  293. JSONNumber t = (JSONNumber) getVariableHash().get(name);
  294. if (t == null)
  295. throw new IllegalArgumentException("No such variable: " + name);
  296. return (double) t.getValue();
  297. }
  298. public boolean getBooleanVariable(String name) {
  299. JSONBoolean t = (JSONBoolean) getVariableHash().get(name);
  300. if (t == null)
  301. throw new IllegalArgumentException("No such variable: " + name);
  302. return t.booleanValue();
  303. }
  304. private JSONArray getArrayVariable(String name) {
  305. JSONArray t = (JSONArray) getVariableHash().get(name);
  306. if (t == null)
  307. throw new IllegalArgumentException("No such variable: " + name);
  308. return t;
  309. }
  310. public String[] getStringArrayVariable(String name) {
  311. JSONArray a = getArrayVariable(name);
  312. String[] s = new String[a.size()];
  313. for (int i = 0; i < a.size(); i++)
  314. s[i] = ((JSONString) a.get(i)).stringValue();
  315. return s;
  316. }
  317. public Set getStringArrayVariableAsSet(String name) {
  318. JSONArray a = getArrayVariable(name);
  319. HashSet s = new HashSet();
  320. for (int i = 0; i < a.size(); i++)
  321. s.add(((JSONString) a.get(i)).stringValue());
  322. return s;
  323. }
  324. public int[] getIntArrayVariable(String name) {
  325. JSONArray a = getArrayVariable(name);
  326. int[] s = new int[a.size()];
  327. for (int i = 0; i < a.size(); i++) {
  328. JSONValue v = a.get(i);
  329. s[i] = v.isNumber() != null ? (int) ((JSONNumber) v).getValue()
  330. : Integer.parseInt(v.toString());
  331. }
  332. return s;
  333. }
  334. public class XML {
  335. JSONObject x;
  336. private XML(JSONObject x) {
  337. this.x = x;
  338. }
  339. public String getXMLAsString() {
  340. StringBuffer sb = new StringBuffer();
  341. for (Iterator it = x.keySet().iterator(); it.hasNext();) {
  342. String tag = (String) it.next();
  343. sb.append("<");
  344. sb.append(tag);
  345. sb.append(">");
  346. sb.append(x.get(tag).isString().stringValue());
  347. sb.append("</");
  348. sb.append(tag);
  349. sb.append(">");
  350. }
  351. return sb.toString();
  352. }
  353. }
  354. public int getChidlCount() {
  355. return json.size() - 2;
  356. }
  357. public UIDL getErrors() {
  358. JSONArray a = (JSONArray) ((JSONObject) json.get(1)).get("error");
  359. return new UIDL(a);
  360. }
  361. }