]> source.dussan.org Git - vaadin-framework.git/commitdiff
drafting implementation
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Wed, 6 Jun 2007 09:18:43 +0000 (09:18 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Wed, 6 Jun 2007 09:18:43 +0000 (09:18 +0000)
svn changeset:1617/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/UIDL.java

index 1b391a93c856e3778ee60a1faaeb16b8513fcb26..54a6eeda0865cf409e9e98fc0f9a9ee530969dbd 100644 (file)
@@ -4,40 +4,55 @@ import java.util.Iterator;
 import java.util.Set;
 
 import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONBoolean;
+import com.google.gwt.json.client.JSONNumber;
 import com.google.gwt.json.client.JSONObject;
 import com.google.gwt.json.client.JSONString;
+import com.google.gwt.json.client.JSONValue;
 
 public class UIDL {
 
-       JSONObject json;
+       JSONArray json;
        
-       public UIDL(JSONObject json) {
+       public UIDL(JSONArray json) {
                this.json = json;
        }
        
-       public int getId() {
-               String id = getAttribute("id");
-               if (id==null) return -1;
-               return Integer.parseInt(id);
+       public String getId() {
+               return getStringAttribute("id");
        }
        
        public String getTag() {
-               Set keys = json.keySet();
-               return "" + keys.iterator().next();
+               return json.get(0).toString();
        }
        
-       public String getAttribute(String name) {
-               JSONObject attrs = (JSONObject) json.get("a");
-               if (attrs == null) return null;
-               return ""+ attrs.get(name);
+       public String getStringAttribute(String name) {
+               JSONValue val = ((JSONObject)json.get(1)).get(name);
+               return ((JSONString)val).stringValue();
+       }
+       
+       public int getIntAttribute(String name) {
+               JSONValue val = ((JSONObject)json.get(1)).get(name);
+               double num = ((JSONNumber)val).getValue();
+               return (int) num;
+       }
+       
+       public long getLongAttribute(String name) {
+               JSONValue val = ((JSONObject)json.get(1)).get(name);
+               double num = ((JSONNumber)val).getValue();
+               return (long) num;
+       }
+       
+       public boolean getBooleanAttribute(String name) {
+               JSONValue val = ((JSONObject)json.get(1)).get(name);
+               return ((JSONBoolean)val).booleanValue();
        }
        
        public Iterator getChildIterator() {
                
                return new Iterator() {
                        
-                       JSONArray children = (JSONArray) ((JSONObject)json.get(getTag())).get("c");
-                       int index=0;
+                       int index=2;
                
                        public void remove() {
                                throw new UnsupportedOperationException();
@@ -45,13 +60,18 @@ public class UIDL {
                
                        public Object next() {
                                
-                               if (children != null)
-                                       return new UIDL((JSONObject)children.get(index++));
+                               if (json.size() > index) {
+                                       JSONValue c = json.get(index++);
+                                       if (c.isString() != null) return c.isString().stringValue();
+                                       else if (c.isArray() != null) return new UIDL(c.isArray());
+                                       else if (c.isObject() != null) return new XML(c.isObject());
+                                       else throw new IllegalStateException("Illegal child of type "+c.getClass().toString()+" in tag " + getTag() + " at index " + index);
+                               }
                                return null;
                        }
                
                        public boolean hasNext() {
-                               return children != null && children.size() > index;
+                               return json.size() > index;
                        }
                
                };
@@ -65,7 +85,7 @@ public class UIDL {
 
                Iterator i = getChildIterator();
                while (i.hasNext()) {
-                       UIDL c = (UIDL) i.next();
+                       Object c = i.next();
                        s += c.toString();
                }
 
@@ -73,4 +93,15 @@ public class UIDL {
                
                return s;
        }
+       
+       public class XML {
+               JSONObject x;
+               private XML(JSONObject x) {
+                       this.x = x;
+               }
+               
+               public String getXMLAsString() {
+                       return x.get("x").toString();
+               }
+       }
 }