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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. import com.google.gwt.json.client.JSONArray;
  5. import com.google.gwt.json.client.JSONObject;
  6. import com.google.gwt.json.client.JSONString;
  7. public class UIDL {
  8. JSONObject json;
  9. public UIDL(JSONObject json) {
  10. this.json = json;
  11. }
  12. public int getId() {
  13. String id = getAttribute("id");
  14. if (id==null) return -1;
  15. return Integer.parseInt(id);
  16. }
  17. public String getTag() {
  18. Set keys = json.keySet();
  19. if (keys.size() != 1)
  20. throw new IllegalStateException("Expected JSON Object to contain exactly one key, now contains "+ json.keySet());
  21. return "" + keys.iterator().next();
  22. }
  23. public String getAttribute(String name) {
  24. JSONObject attrs = (JSONObject) json.get("attr");
  25. if (attrs == null) return null;
  26. return ""+ attrs.get(name);
  27. }
  28. public Iterator getChildIterator() {
  29. return new Iterator() {
  30. JSONArray children = (JSONArray) ((JSONObject)json.get(getTag())).get("children");
  31. int index=0;
  32. public void remove() {
  33. throw new UnsupportedOperationException();
  34. }
  35. public Object next() {
  36. if (children != null)
  37. return new UIDL((JSONObject)children.get(index++));
  38. return null;
  39. }
  40. public boolean hasNext() {
  41. return children != null && children.size() > index;
  42. }
  43. };
  44. }
  45. public String toString() {
  46. String s = "<"+getTag()+ " ";
  47. s +="id="+getId();
  48. s += ">\n";
  49. Iterator i = getChildIterator();
  50. while (i.hasNext()) {
  51. UIDL c = (UIDL) i.next();
  52. s += c.toString();
  53. }
  54. s += "</"+getTag()+">\n";
  55. return s;
  56. }
  57. }