Skip to content
Browse files

Add a TextOnly filter to Filters, that Filters Text, but not CDATA.

  • Loading branch information...
1 parent 912ba89 commit 02300a3bd7b4c06a9e06e55364d2f7fc7a4d43c4 @rolfl rolfl committed
View
25 core/src/java/org/jdom2/filter/Filters.java
@@ -54,7 +54,17 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
package org.jdom2.filter;
-import org.jdom2.*;
+import org.jdom2.Attribute;
+import org.jdom2.CDATA;
+import org.jdom2.Comment;
+import org.jdom2.Content;
+import org.jdom2.DocType;
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.EntityRef;
+import org.jdom2.Namespace;
+import org.jdom2.ProcessingInstruction;
+import org.jdom2.Text;
/**
* Factory class of convenience methods to create Filter instances of common
@@ -99,6 +109,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
private static final Filter<Text> ftext =
new ClassFilter<Text>(Text.class);
+ private static final Filter<Text> ftextonly = new TextOnlyFilter();
+
private static final Filter<Element> felement =
new ClassFilter<Element>(Element.class);
@@ -290,6 +302,17 @@ private Filters() {
}
/**
+ * Return a Filter that matches any {@link Text} data (excludes
+ * {@link CDATA} instances).
+ *
+ * @return a Filter that matches any {@link Text} data (which excludes
+ * {@link CDATA} instances).
+ */
+ public static final Filter<Text> textOnly() {
+ return ftextonly;
+ }
+
+ /**
* Return a Filter that matches any Boolean data.
*
* @return a Filter that matches any Boolean data.
View
40 core/src/java/org/jdom2/filter/TextOnlyFilter.java
@@ -0,0 +1,40 @@
+package org.jdom2.filter;
+
+import org.jdom2.Text;
+import org.jdom2.Content.CType;
+
+/**
+ * A filter that matches Text, but not CDATA content.
+ *
+ * @author Rolf Lear
+ *
+ */
+final class TextOnlyFilter extends AbstractFilter<Text> {
+
+ /**
+ * JDOM2 Serialization: Default mechanism
+ */
+ private static final long serialVersionUID = 200L;
+
+ @Override
+ public Text filter(Object content) {
+ if (content instanceof Text) {
+ final Text txt = (Text)content;
+ if (txt.getCType() == CType.Text) {
+ return txt;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public int hashCode() {
+ return this.getClass().hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof TextOnlyFilter;
+ }
+
+}
View
5 test/src/java/org/jdom2/test/cases/filter/TestFilters.java
@@ -171,6 +171,11 @@ public void testText() {
}
@Test
+ public void testTextOnly() {
+ checkFilter(Filters.textOnly(), new Text("txt"), new CDATA("txt"));
+ }
+
+ @Test
public void testFBoolean() {
checkFilter(Filters.fboolean(), Boolean.TRUE, new Object());
}

0 comments on commit 02300a3

Please sign in to comment.
Something went wrong with that request. Please try again.