View Javadoc

1   /*
2    * Copyright (C) 2003-2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
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          // make set immutable
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          // convert list to set
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          // convert array to list
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          // wrap in set
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 }