Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UIDL.java 9.0KB

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