1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties.xml.dom;
23
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.commons.lang.builder.ToStringBuilder;
31 import org.dom4j.Element;
32 import org.dom4j.Node;
33 import org.dom4j.NodeFilter;
34
35 /***
36 * A node filter that only accepts elements with a specified name.
37 *
38 * @author Christian Siefkes
39 * @version $Revision: 1.5 $, $Date: 2004/09/07 10:02:00 $, $Author: siefkes $
40 */
41 public class ElementNameFilter implements NodeFilter {
42
43 /***
44 * An immutable set of element name strings whose format must be compatible
45 * to {@link DOMUtils#name(Element)}. An elements is accepted iff its name
46 * is contained in this set.
47 */
48 private final Set<String> names;
49
50 /***
51 * Creates a new instance.
52 *
53 * @param nameSet set of element name strings to accept, format must be
54 * compatible to {@link DOMUtils#name(Element)}
55 */
56 public ElementNameFilter(final Set<String> nameSet) {
57 super();
58
59 names = Collections.unmodifiableSet(nameSet);
60 }
61
62 /***
63 * Creates a new instance.
64 *
65 * @param nameList array of element name strings to accept, format must be
66 * compatible to {@link DOMUtils#name(Element)}
67 */
68 public ElementNameFilter(final List<String> nameList) {
69
70 this(new HashSet<String>(nameList));
71 }
72
73 /***
74 * Creates a new instance.
75 *
76 * @param nameArray array of element name strings to accept, format must be
77 * compatible to {@link DOMUtils#name(Element)}
78 */
79 public ElementNameFilter(final String[] nameArray) {
80
81 this(Arrays.asList(nameArray));
82 }
83
84 /***
85 * Creates a new instance that accepts only elements of a single type.
86 *
87 * @param elementName Only elements with this name are accepted, format must
88 * be compatible to {@link DOMUtils#name(Element)}
89 */
90 public ElementNameFilter(final String elementName) {
91
92 this(Collections.singleton(elementName));
93 }
94
95 /***
96 * Returns the set of element names accepted by this filter.
97 *
98 * @return an immutable set containing the element names (Strings) accepted
99 * by this filter
100 */
101 public Set getNames() {
102 return names;
103 }
104
105 /***
106 * Tests whether a node is accected by this filter. To match a node must
107 * fulfill two conditions: (a) it must be an {@link Element} and (b)
108 * {@link #getNames()} must contain the name of the element as returned by
109 * {@link DOMUtils#name(Element)}.
110 *
111 * @param node the node to test
112 * @return <code>true</code> iff this filter matches the given node
113 */
114 public boolean matches(final Node node) {
115 final boolean result;
116 if (node instanceof Element) {
117 final Element element = (Element) node;
118 result = names.contains(DOMUtils.name(element));
119 } else {
120 result = false;
121 }
122 return result;
123 }
124
125 /***
126 * Returns a string representation of this object.
127 *
128 * @return a textual representation
129 */
130 public String toString() {
131 return new ToStringBuilder(this)
132 .append("names", names)
133 .toString();
134 }
135
136 }