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.

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