]> source.dussan.org Git - gwtquery.git/commitdiff
Adding a couple of methods in xmlbuilders to parse number and date values in text...
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Wed, 7 Nov 2012 17:56:41 +0000 (18:56 +0100)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Wed, 7 Nov 2012 17:56:41 +0000 (18:56 +0100)
gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java
gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java
gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java

index 397b2513a0c032a77df0490ac336b0e6ef9c9d2d..8c35e5cb2eb7e8ae0f308f997d2dfc0ad72bf4b9 100644 (file)
@@ -15,6 +15,8 @@
  */\r
 package com.google.gwt.query.client.builders;\r
 \r
+import java.util.Date;\r
+\r
 import com.google.gwt.dom.client.Element;\r
 \r
 /**\r
@@ -55,4 +57,15 @@ public interface XmlBuilder {
    * Sets the text content of the element\r
    */\r
   <J> J setText(String t);\r
+\r
+  /**\r
+   * Returns the text content of the element as a number\r
+   */\r
+  double getTextAsNumber();\r
+  \r
+  /**\r
+   * Returns the text content of the element as a date\r
+   */\r
+  Date getTextAsDate();\r
+\r
 }\r
index 9cc43194ae24587da64485cc48be32afc9bfa04e..a2062509b48c67d4e6c3178233c10eecc8171306 100644 (file)
@@ -17,7 +17,10 @@ package com.google.gwt.query.client.builders;
 
 import static com.google.gwt.query.client.GQuery.$;
 
+import java.util.Date;
+
 import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsDate;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.query.client.GQuery;
 import com.google.gwt.query.client.Properties;
@@ -26,7 +29,7 @@ import com.google.gwt.query.client.js.JsUtils;
 public abstract class XmlBuilderBase<J extends XmlBuilderBase<?>> implements XmlBuilder {
   
   //TODO empty document
-  protected GQuery g = $(JsUtils.parseXML("<root/>"));
+  protected GQuery g = $((Element)JsUtils.parseXML("<root/>"));
 
   public void append(String xml) {
     g.append(JsUtils.parseXML(xml));
@@ -66,10 +69,27 @@ public abstract class XmlBuilderBase<J extends XmlBuilderBase<?>> implements Xml
     return g.attr(n);
   }
   
+  @Override
   public String getText() {
     return g.text();
   }
   
+  @Override
+  public double getTextAsNumber() {
+    String t =  g.text().replaceAll("[^\\d\\.\\-]", "");
+    return t.isEmpty() ? 0 : Double.parseDouble(t);
+  }
+  
+  @Override
+  public Date getTextAsDate() {
+    String t =  g.text().trim();
+    if (t.matches("\\d+")) {
+      return new Date(Long.parseLong(t));
+    } else {
+      return new Date((long)JsDate.parse(t));
+    }
+  }
+  
   @SuppressWarnings("unchecked")
   public J load(Object o) {
     assert o == null || o instanceof JavaScriptObject && JsUtils.isElement((JavaScriptObject)o) || o instanceof String;
@@ -99,6 +119,7 @@ public abstract class XmlBuilderBase<J extends XmlBuilderBase<?>> implements Xml
     g.attr(n, v);
   }
 
+  @SuppressWarnings("unchecked")
   @Override
   public <T> T setText(String t) {
     g.text(t);
index 787eb07ccda17271c1e3c6221cc5fa4c0e225742..fc10aaaa7e6019173e11ff112834ffb9973afa20 100644 (file)
@@ -249,7 +249,7 @@ public class JsUtils {
    * defined.
    */
   public static native boolean hasProperty(JavaScriptObject o, String name)/*-{
-               return name in o;
+               return o && name in o;
   }-*/;
 
   /**
index 5dc4b2d26a5c109e59f12a8a694f769899dc6d1f..597c0785e6115b795cffa51c175dcfe129279ae3 100644 (file)
@@ -32,6 +32,7 @@ import com.google.gwt.query.client.builders.Name;
 import com.google.gwt.query.client.builders.XmlBuilder;
 import com.google.gwt.query.client.plugins.ajax.Ajax;
 import com.google.gwt.query.client.plugins.ajax.Ajax.Settings;
+import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.RootPanel;
 
@@ -167,6 +168,69 @@ public class GQueryAjaxTestGwt extends GWTTestCase {
     assertEquals(" pepe text", x.getText());
   }
   
+  interface Feed extends XmlBuilder {
+    interface Tag extends XmlBuilder {
+    }
+    Tag getTitle();
+    Tag getTagline();
+    Tag getFullcount();
+    Tag getModified();
+
+    interface Link extends XmlBuilder {
+      String getHref();
+      String getType();
+    }
+    Link getLink();
+
+    interface Entry extends XmlBuilder {
+      interface Author extends XmlBuilder {
+        Tag getEmail();
+        Tag getName();
+      }
+      Tag getTitle();
+      Tag getSummary();
+      Link getLink();
+      Tag getModified();
+      Tag getIssued();
+      Tag getId();
+      Author getAuthor();
+    }  
+    Entry[] getEntry();
+  }
+  
+  // FIXME: gquery xml does not work well with htmlUnit, FF & Safari works
+  // TODO: test in IE
+  @DoNotRunWith({Platform.HtmlUnitLayout})
+  public void testXmlGmailExample() {
+    Window.alert("run");
+    String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
+        "<feed version='0.3' xmlns='http://purl.org/atom/ns#'>"
+      + " <title>Gmail - Inbox for manolo@...</title>"
+      + " <tagline>New messages in your Gmail Inbox</tagline>"
+      + " <fullcount>1</fullcount>"
+      + " <link rel='alternate' href='http://mail.google.com/mail' type='text/html' />"
+      + " <modified>2012-11-07T10:32:52Z</modified>"
+      + " <entry>"
+      + "  <title>Trending Startups and Updates</title>"
+      + "  <summary>AngelList Weekly Trending Startups Storenvy Tumblr for stores E-Commerce Platforms · San Francisco</summary>"
+      + "  <link rel='alternate' href='http://mail.google.com/mail?account_id=manolo@....&amp;message_id=13ad2e227da1488b&amp;view=conv&amp;extsrc=atom' type='text/html' />"
+      + "  <modified>2012-11-05T23:22:47Z</modified>"
+      + "  <issued>2012-11-05T23:22:47Z</issued>"
+      + "  <id>tag:gmail.google.com,2004:1417840183363061889</id>"
+      + "  <author>"
+      + "   <name>AName</name>"
+      + "   <email>AnEmail</email>"
+      + "  </author>"
+      + " </entry>"
+      + "</feed>";
+    
+    Feed f = GWT.create(Feed.class);
+    f.parse(xml);
+    assertEquals((int)f.getFullcount().getTextAsNumber(), f.getEntry().length);
+    assertEquals(112, f.getModified().getTextAsDate().getYear());
+    assertEquals("AName", f.getEntry()[0].getAuthor().getName().getText());
+  }
+  
   public void testJsonValidService() {
     delayTestFinish(5000);
     // Use a public json service