From 3f637680f14e33e04bf9a0283bc2671d8ba35a6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Wed, 7 Nov 2012 18:56:41 +0100 Subject: [PATCH] Adding a couple of methods in xmlbuilders to parse number and date values in text elements. Adding a test for xmlbuilder --- .../gwt/query/client/builders/XmlBuilder.java | 13 ++++ .../query/client/builders/XmlBuilderBase.java | 23 ++++++- .../google/gwt/query/client/js/JsUtils.java | 2 +- .../gwt/query/client/GQueryAjaxTestGwt.java | 64 +++++++++++++++++++ 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 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> implements XmlBuilder { //TODO empty document - protected GQuery g = $(JsUtils.parseXML("")); + protected GQuery g = $((Element)JsUtils.parseXML("")); public void append(String xml) { g.append(JsUtils.parseXML(xml)); @@ -66,10 +69,27 @@ public abstract class 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> implements Xml g.attr(n, v); } + @SuppressWarnings("unchecked") @Override public 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 = "" + + "" + + " Gmail - Inbox for manolo@..." + + " New messages in your Gmail Inbox" + + " 1" + + " " + + " 2012-11-07T10:32:52Z" + + " " + + " Trending Startups and Updates" + + " AngelList Weekly Trending Startups Storenvy Tumblr for stores E-Commerce Platforms · San Francisco" + + " " + + " 2012-11-05T23:22:47Z" + + " 2012-11-05T23:22:47Z" + + " tag:gmail.google.com,2004:1417840183363061889" + + " " + + " AName" + + " AnEmail" + + " " + + " " + + ""; + + 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 -- 2.39.5