diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2012-11-26 10:04:48 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2012-11-26 10:04:48 +0100 |
commit | 880072663270ae9c9f85adf5857e285f0f2bfa31 (patch) | |
tree | a77d5a7f03d22b50c7508d7344ccff9678d0752f /gwtquery-core | |
parent | 82f3f28a923a5d7a5c13260cbb9fc98f624db7d9 (diff) | |
download | gwtquery-880072663270ae9c9f85adf5857e285f0f2bfa31.tar.gz gwtquery-880072663270ae9c9f85adf5857e285f0f2bfa31.zip |
Adding getTextAsBoolean and getTextAsEnum to xmlbuilders
Diffstat (limited to 'gwtquery-core')
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilder.java | 10 | ||||
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/builders/XmlBuilderBase.java | 14 |
2 files changed, 20 insertions, 4 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 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 extends Enum<T>> T getTextAsEnum(Class<T> 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<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+")) { @@ -90,6 +87,16 @@ public abstract class XmlBuilderBase<J extends XmlBuilderBase<?>> implements Xml } } + public boolean getTextAsBoolean() { + String t = g.text().trim().toLowerCase(); + return !t.matches("^(|false|off|0)$"); + } + + public <T extends Enum<T>> T getTextAsEnum(Class<T> 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<J extends XmlBuilderBase<?>> implements Xml } @SuppressWarnings("unchecked") - @Override public <T> T setText(String t) { g.text(t); return (T)this; |