From 880072663270ae9c9f85adf5857e285f0f2bfa31 Mon Sep 17 00:00:00 2001 From: Manuel Carrasco MoƱino Date: Mon, 26 Nov 2012 10:04:48 +0100 Subject: Adding getTextAsBoolean and getTextAsEnum to xmlbuilders --- .../com/google/gwt/query/client/builders/XmlBuilder.java | 10 ++++++++++ .../google/gwt/query/client/builders/XmlBuilderBase.java | 14 ++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'gwtquery-core') 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 8c35e5cb..dfb110b4 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 @@ -68,4 +68,14 @@ public interface XmlBuilder { */ Date getTextAsDate(); + /** + * Returns whether the text content of the element is true or false. + * It is false when the string matches false, off, 0 or empty. + */ + boolean getTextAsBoolean(); + + /** + * Returns the Enum value of the text content of the element. + */ + > T getTextAsEnum(Class clazz); } 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 a2062509..24582c53 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 @@ -69,18 +69,15 @@ 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+")) { @@ -90,6 +87,16 @@ public abstract class XmlBuilderBase> implements Xml } } + public boolean getTextAsBoolean() { + String t = g.text().trim().toLowerCase(); + return !t.matches("^(|false|off|0)$"); + } + + public > T getTextAsEnum(Class clazz) { + String t = g.text().trim(); + return Enum.valueOf(clazz, t); + } + @SuppressWarnings("unchecked") public J load(Object o) { assert o == null || o instanceof JavaScriptObject && JsUtils.isElement((JavaScriptObject)o) || o instanceof String; @@ -120,7 +127,6 @@ public abstract class XmlBuilderBase> implements Xml } @SuppressWarnings("unchecked") - @Override public T setText(String t) { g.text(t); return (T)this; -- cgit v1.2.3