*/\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
* 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
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;
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));
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;
g.attr(n, v);
}
+ @SuppressWarnings("unchecked")
@Override
public <T> T setText(String t) {
g.text(t);
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;
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@....&message_id=13ad2e227da1488b&view=conv&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