001: /*--
002:
003: $Id: ElementFilter.java,v 1.18 2004/09/07 06:37:20 jhunter Exp $
004:
005: Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions
010: are met:
011:
012: 1. Redistributions of source code must retain the above copyright
013: notice, this list of conditions, and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright
016: notice, this list of conditions, and the disclaimer that follows
017: these conditions in the documentation and/or other materials
018: provided with the distribution.
019:
020: 3. The name "JDOM" must not be used to endorse or promote products
021: derived from this software without prior written permission. For
022: written permission, please contact <request_AT_jdom_DOT_org>.
023:
024: 4. Products derived from this software may not be called "JDOM", nor
025: may "JDOM" appear in their name, without prior written permission
026: from the JDOM Project Management <request_AT_jdom_DOT_org>.
027:
028: In addition, we request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by the
032: JDOM Project (http://www.jdom.org/)."
033: Alternatively, the acknowledgment may be graphical using the logos
034: available at http://www.jdom.org/images/logos.
035:
036: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
040: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: SUCH DAMAGE.
048:
049: This software consists of voluntary contributions made by many
050: individuals on behalf of the JDOM Project and was originally
051: created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
052: Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
053: on the JDOM Project, please see <http://www.jdom.org/>.
054:
055: */
056:
057: package org.jdom.filter;
058:
059: import java.io.*;
060: import org.jdom.*;
061:
062: /**
063: * A Filter that only matches {@link org.jdom.Element} objects.
064: *
065: * @version $Revision: 1.18 $, $Date: 2004/09/07 06:37:20 $
066: * @author Jools Enticknap
067: * @author Bradley S. Huffman
068: */
069: public class ElementFilter extends AbstractFilter {
070:
071: private static final String CVS_ID = "@(#) $RCSfile: ElementFilter.java,v $ $Revision: 1.18 $ $Date: 2004/09/07 06:37:20 $ $Name: jdom_1_0 $";
072:
073: /** The element name */
074: private String name;
075:
076: /** The element namespace */
077: private transient Namespace namespace;
078:
079: /**
080: * Select only the Elements.
081: */
082: public ElementFilter() {
083: }
084:
085: /**
086: * Select only the Elements with the supplied name in any Namespace.
087: *
088: * @param name The name of the Element.
089: */
090: public ElementFilter(String name) {
091: this .name = name;
092: }
093:
094: /**
095: * Select only the Elements with the supplied Namespace.
096: *
097: * @param namespace The namespace the Element lives in.
098: */
099: public ElementFilter(Namespace namespace) {
100: this .namespace = namespace;
101: }
102:
103: /**
104: * Select only the Elements with the supplied name and Namespace.
105: *
106: * @param name The name of the Element.
107: * @param namespace The namespace the Element lives in.
108: */
109: public ElementFilter(String name, Namespace namespace) {
110: this .name = name;
111: this .namespace = namespace;
112: }
113:
114: /**
115: * Check to see if the object matches a predefined set of rules.
116: *
117: * @param obj The object to verify.
118: * @return <code>true</code> if the objected matched a predfined
119: * set of rules.
120: */
121: public boolean matches(Object obj) {
122: if (obj instanceof Element) {
123: Element el = (Element) obj;
124: return (this .name == null || this .name.equals(el.getName()))
125: && (this .namespace == null || this .namespace
126: .equals(el.getNamespace()));
127: }
128: return false;
129: }
130:
131: /**
132: * Returns whether the two filters are equivalent (i.e. the
133: * matching names and namespace are equivalent).
134: *
135: * @param obj the object to compare against
136: * @return whether the two filters are equal
137: */
138: public boolean equals(Object obj) {
139: // Generated by IntelliJ
140: if (this == obj)
141: return true;
142: if (!(obj instanceof ElementFilter))
143: return false;
144:
145: final ElementFilter filter = (ElementFilter) obj;
146:
147: if (name != null ? !name.equals(filter.name)
148: : filter.name != null)
149: return false;
150: if (namespace != null ? !namespace.equals(filter.namespace)
151: : filter.namespace != null)
152: return false;
153:
154: return true;
155: }
156:
157: public int hashCode() {
158: // Generated by IntelliJ
159: int result;
160: result = (name != null ? name.hashCode() : 0);
161: result = 29 * result
162: + (namespace != null ? namespace.hashCode() : 0);
163: return result;
164: }
165:
166: // Support a custom Namespace serialization so no two namespace
167: // object instances may exist for the same prefix/uri pair
168: private void writeObject(ObjectOutputStream out) throws IOException {
169:
170: out.defaultWriteObject();
171:
172: // We use writeObject() and not writeUTF() to minimize space
173: // This allows for writing pointers to already written strings
174: out.writeObject(namespace.getPrefix());
175: out.writeObject(namespace.getURI());
176: }
177:
178: private void readObject(ObjectInputStream in) throws IOException,
179: ClassNotFoundException {
180:
181: in.defaultReadObject();
182:
183: namespace = Namespace.getNamespace((String) in.readObject(),
184: (String) in.readObject());
185: }
186: }
|