aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2012-11-07 18:56:41 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2012-11-07 18:56:41 +0100
commit3f637680f14e33e04bf9a0283bc2671d8ba35a6b (patch)
treedc9382e778163460d3a817039d02b57ae3c1c01c
parent59d010b8a58fa01431c7df89d6da9d726a94a828 (diff)
downloadgwtquery-3f637680f14e33e04bf9a0283bc2671d8ba35a6b.tar.gz
gwtquery-3f637680f14e33e04bf9a0283bc2671d8ba35a6b.zip
Adding a couple of methods in xmlbuilders to parse number and date values in text elements. Adding a test for xmlbuilder
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java13
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java23
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java2
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java64
4 files changed, 100 insertions, 2 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java
index 397b2513..8c35e5cb 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java
@@ -15,6 +15,8 @@
*/
package com.google.gwt.query.client.builders;
+import java.util.Date;
+
import com.google.gwt.dom.client.Element;
/**
@@ -55,4 +57,15 @@ public interface XmlBuilder {
* Sets the text content of the element
*/
<J> J setText(String t);
+
+ /**
+ * Returns the text content of the element as a number
+ */
+ double getTextAsNumber();
+
+ /**
+ * Returns the text content of the element as a date
+ */
+ Date getTextAsDate();
+
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java
index 9cc43194..a2062509 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java
@@ -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);
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
index 787eb07c..fc10aaaa 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
@@ -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;
}-*/;
/**
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java
index 5dc4b2d2..597c0785 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java
@@ -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