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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. public class UIDL {
  12. JSONArray json;
  13. public UIDL(JSONArray json) {
  14. this.json = json;
  15. }
  16. public String getId() {
  17. JSONValue val = ((JSONObject) json.get(1)).get("id");
  18. if (val == null)
  19. return null;
  20. return ((JSONString) val).stringValue();
  21. }
  22. public String getTag() {
  23. return ((JSONString) json.get(0)).stringValue();
  24. }
  25. public String getStringAttribute(String name) {
  26. JSONValue val = ((JSONObject) json.get(1)).get(name);
  27. if (val == null)
  28. return "";
  29. return ((JSONString) val).stringValue();
  30. }
  31. public Set getAttributeNames() {
  32. HashSet attrs = new HashSet(((JSONObject) json.get(1)).keySet());
  33. attrs.remove("v");
  34. return attrs;
  35. }
  36. public int getIntAttribute(String name) {
  37. JSONValue val = ((JSONObject) json.get(1)).get(name);
  38. if (val == null)
  39. return 0;
  40. double num = ((JSONNumber) val).getValue();
  41. return (int) num;
  42. }
  43. public long getLongAttribute(String name) {
  44. JSONValue val = ((JSONObject) json.get(1)).get(name);
  45. if (val == null)
  46. return 0;
  47. double num = ((JSONNumber) val).getValue();
  48. return (long) num;
  49. }
  50. public boolean getBooleanAttribute(String name) {
  51. JSONValue val = ((JSONObject) json.get(1)).get(name);
  52. if (val == null)
  53. return false;
  54. return ((JSONBoolean) val).booleanValue();
  55. }
  56. public boolean hasAttribute(String name) {
  57. return ((JSONObject) json.get(1)).get(name) != null;
  58. }
  59. public UIDL getChildUIDL(int i) {
  60. JSONValue c = json.get(i + 2);
  61. if (c.isArray() != null)
  62. return new UIDL(c.isArray());
  63. throw new IllegalStateException("Child node " + i
  64. + " is not of type UIDL");
  65. }
  66. public String getChildString(int i) {
  67. JSONValue c = json.get(i + 2);
  68. if (c.isString() != null)
  69. return ((JSONString)c).stringValue();
  70. throw new IllegalStateException("Child node " + i
  71. + " is not of type String");
  72. }
  73. public Iterator getChildIterator() {
  74. return new Iterator() {
  75. int index = 2;
  76. public void remove() {
  77. throw new UnsupportedOperationException();
  78. }
  79. public Object next() {
  80. if (json.size() > index) {
  81. JSONValue c = json.get(index++);
  82. if (c.isString() != null)
  83. return c.isString().stringValue();
  84. else if (c.isArray() != null)
  85. return new UIDL(c.isArray());
  86. else if (c.isObject() != null)
  87. return new XML(c.isObject());
  88. else
  89. throw new IllegalStateException("Illegal child " + c
  90. + " in tag " + getTag() + " at index " + index);
  91. }
  92. return null;
  93. }
  94. public boolean hasNext() {
  95. return json.size() > index;
  96. }
  97. };
  98. }
  99. public String toString() {
  100. String s = "<" + getTag();
  101. for (Iterator i = getAttributeNames().iterator(); i.hasNext();) {
  102. String name = i.next().toString();
  103. s += " " + name + "=" + ((JSONObject) json.get(1)).get(name);
  104. }
  105. s += ">\n";
  106. Iterator i = getChildIterator();
  107. while (i.hasNext()) {
  108. Object c = i.next();
  109. s += c.toString();
  110. }
  111. s += "</" + getTag() + ">\n";
  112. return s;
  113. }
  114. public String getStringVariable(String name) {
  115. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  116. if (v == null)
  117. throw new IllegalArgumentException(
  118. "No variables defined in tag. (requested: " + name + ")");
  119. JSONString t = (JSONString) v.get(name);
  120. if (t == null)
  121. throw new IllegalArgumentException("No such variable: " + name);
  122. return t.stringValue();
  123. }
  124. public int getIntVariable(String name) {
  125. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  126. if (v == null)
  127. throw new IllegalArgumentException(
  128. "No variables defined in tag. (requested: " + name + ")");
  129. JSONNumber t = (JSONNumber) v.get(name);
  130. if (t == null)
  131. throw new IllegalArgumentException("No such variable: " + name);
  132. return (int) t.getValue();
  133. }
  134. public boolean getBooleanVariable(String name) {
  135. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  136. if (v == null)
  137. throw new IllegalArgumentException(
  138. "No variables defined in tag. (requested: " + name + ")");
  139. JSONBoolean t = (JSONBoolean) v.get(name);
  140. if (t == null)
  141. throw new IllegalArgumentException("No such variable: " + name);
  142. return t.booleanValue();
  143. }
  144. public JSONArray getArrayVariable(String name) {
  145. JSONObject v = (JSONObject) ((JSONObject) json.get(1)).get("v");
  146. if (v == null)
  147. throw new IllegalArgumentException(
  148. "No variables defined in tag. (requested: " + name + ")");
  149. JSONArray t = (JSONArray) v.get(name);
  150. if (t == null)
  151. throw new IllegalArgumentException("No such variable: " + name);
  152. return t;
  153. }
  154. public String[] getStingArrayVariable(String name) {
  155. JSONArray a = getArrayVariable(name);
  156. String[] s = new String[a.size()];
  157. for (int i = 0; i < a.size(); i++)
  158. s[i] = ((JSONString) a.get(i)).stringValue();
  159. return s;
  160. }
  161. public int[] getIntArrayVariable(String name) {
  162. JSONArray a = getArrayVariable(name);
  163. int[] s = new int[a.size()];
  164. for (int i = 0; i < a.size(); i++) {
  165. JSONValue v = a.get(i);
  166. s[i] = v.isNumber() != null ? (int) ((JSONNumber) v).getValue()
  167. : Integer.parseInt(v.toString());
  168. }
  169. return s;
  170. }
  171. public class XML {
  172. JSONObject x;
  173. private XML(JSONObject x) {
  174. this.x = x;
  175. }
  176. public String getXMLAsString() {
  177. return x.get("x").toString();
  178. }
  179. }
  180. }