[jdom-interest] Code for a new implementation of ElementFilter in attachment
tshitshi kia ntoni
tkiantoni at yahoo.co.uk
Mon Jul 26 15:51:46 PDT 2004
Skipped content of type multipart/alternative-------------- next part --------------
/*
* Created on 21 juil. 2004
* Tshitshi Kia Ntoni
*
Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package genie.core.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.filter.AbstractFilter;
/**
* A Filter that only matches {@link org.jdom.Element} objects
* that feet the supplied, if so, element name, element namespace, attribute name,
* attribute namespace, attribute value and attribute type.
* The attribute type is passed through a filtermask.
* With the implementation it is easy to look up an element according to
* its id value as to any attribue value.
* <code>Document document ....;</code>
* <code>ElementFilter2 filter = new ElementFilter2();</code>
* <code>filter.setFilterMask(ElementFilter2.ID)</code>
* <code>filter.setAttributeValue(aIdValue)</code>
* No need of implementing specific
* Document and Element classes as done in org.jdom.contrib.ids package.
* @version 1, $Date: 2004/06/21 $
* @author Tshitshi Kia Ntoni
*/
public class ElementFilter2 extends AbstractFilter {
/**
* Attribute type: the attribute value is a string.
*
* @see #getAttributeType
*/
public final static int CDATA = 2;
/**
* <p>
* Attribute type: the attribute value is a list of entity names.
* </p>
*
* @see #getAttributeType
*/
public final static int ENTITIES = 64;
/**
* Attribute type: the attribute value is the elementName of an entity.
*
* @see #getAttributeType
*/
public final static int ENTITY = 32;
/**
* Attribute type: the attribute value is a elementName token from an
* enumeration.
*
* @see #getAttributeType
*/
public final static int ENUMERATED = 1016;
/**
* Attribute type: the attribute value is a unique identifier.
*
* @see #getAttributeType
*/
public final static int ID = 4;
/**
* Attribute type: the attribute value is a reference to a
* unique identifier.
*
* @see #getAttributeType
*/
public final static int IDREF = 8;
/**
* Attribute type: the attribute value is a list of references to
* unique identifiers.
*
* @see #getAttributeType
*/
public final static int IDREFS = 16;
/**
* Attribute type: the attribute value is a elementName token.
* <p>
* According to SAX 2.0 specification, attributes of enumerated
* types should be reported as "NMTOKEN" by SAX parsers. But the
* major parsers (Xerces and Crimson) provide specific values
* that permit to recognize them as {@link #ENUMERATED}.
*
* @see #getAttributeType
*/
public final static int NMTOKEN = 128;
/**
* Attribute type: the attribute value is a list of elementName tokens.
*
* @see #getAttributeType
*/
public final static int NMTOKENS = 254;
/**
* Attribute type: the attribute value is the elementName of a notation.
*
* @see #getAttributeType
*/
public final static int NOTATION = 508;
/** Mask for JDOM {@link Element} objects with an {@link Attribute}
* whose type is {@link Attribute.UNDECLARED_TYPE}
*/
public final static int UNDECLARED = 1;
/** The attribute name */
private String attributeName;
/** The attribute namespace */
private transient Namespace attributeNamespace;
/** The attribute value */
private String attributeValue;
/** The element name */
private String elementName;
/** The element namespace */
private transient Namespace elementNamespace;
/** The JDOM object mask */
private int filterMask;
/**
* Select only the Elements with any name in any Namespace.
*
*/
public ElementFilter2() {
setDefaultMask();
}
/**
* Select only the Elements with any name in any Namespace
* and an attribute whose type matches the mask.
*
* @param mask
*/
public ElementFilter2(boolean allVisible) {
if (allVisible) {
setDefaultMask();
}
else {
filterMask &= ~filterMask;
}
}
/**
* Select only the Elements with any name in any Namespace
* and an attribute whose type matches the mask.
*
* @param mask
*/
public ElementFilter2(int mask) {
setFilterMask(mask);
}
/**
* Returns whether the two filters are equivalent (i.e. the matching names
* and namespace are equivalent).
*
* @param obj the object to compare against
* @return whether the two filters are equal
*/
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof ElementFilter2)) return false;
final ElementFilter2 filter = (ElementFilter2) obj;
if (elementName != null ? !elementName.equals(filter.elementName) : filter.elementName != null){
return false;
}
if (elementNamespace != null ? !elementNamespace.equals(filter.elementNamespace) : filter.elementNamespace != null){
return false;
}
if (attributeName != null ? !attributeName.equals(filter.attributeName) : filter.attributeName != null){
return false;
}
if (attributeNamespace != null ? !attributeNamespace.equals(filter.attributeNamespace) : filter.attributeNamespace != null){
return false;
}
if (filterMask != filter.filterMask){
return false;
}
return true;
}
/**
* @return Returns the attributeName.
*/
public String getAttributeName() {
return attributeName;
}
/**
* @return Returns the attributeNamespace.
*/
public Namespace getAttributeNamespace() {
return attributeNamespace;
}
/**
* @return Returns the attributeValue.
*/
public String getAttributeValue() {
return attributeValue;
}
/**
* get the filter to allow all legal JDOM Element.
*/
public int getDefaultMask() {
return CDATA | ENTITIES | ENTITY |
ENUMERATED | ID | IDREF |
IDREFS | NMTOKEN | NMTOKENS |
NOTATION | UNDECLARED;
}
/**
* @return Returns the elementName.
*/
public String getElementName() {
return elementName;
}
/**
* @return Returns the elementNamespace.
*/
public Namespace getElementNamespace() {
return elementNamespace;
}
/**
* Return current filtering mask.
*/
public int getFilterMask() {
return filterMask;
}
public int hashCode() {
int result;
result = (elementName != null ? elementName.hashCode() : 0);
result += (elementNamespace != null ? elementNamespace.hashCode() : 0);
result += (attributeName != null ? attributeName.hashCode() : 0);
result += (attributeNamespace != null ? attributeNamespace.hashCode() : 0);
result += filterMask;
return result;
}
/* (non-Javadoc)
* @see org.jdom.filter.Filter#matches(java.lang.Object)
*/
public boolean matches(Object arg0) {
if (arg0 instanceof Element) {
Element el = (Element) arg0;
if (filterMask - getDefaultMask() == 0) {
//System.out.println("DefaultMask");
if (elementName == null && attributeName == null && attributeValue == null) {
return true;
}
return validElement(el, Attribute.UNDECLARED_TYPE) |
validElement(el, Attribute.CDATA_TYPE) |
validElement(el, Attribute.ID_TYPE) |
validElement(el, Attribute.IDREF_TYPE) |
validElement(el, Attribute.IDREFS_TYPE) |
validElement(el, Attribute.ENTITY_TYPE) |
validElement(el, Attribute.ENTITIES_TYPE) |
validElement(el, Attribute.ENUMERATED_TYPE) |
validElement(el, Attribute.NMTOKEN_TYPE) |
validElement(el, Attribute.NMTOKENS_TYPE) |
validElement(el, Attribute.NOTATION_TYPE);
} else if ((filterMask & UNDECLARED) != 0) {
//System.out.println("UNDECLARED");
return validElement(el, Attribute.UNDECLARED_TYPE);
} else if ((filterMask & CDATA) != 0) {
//System.out.println("CDATA");
return validElement(el, Attribute.CDATA_TYPE);
} else if ((filterMask & ID) != 0) {
//System.out.println("ID");
return validElement(el, Attribute.ID_TYPE);
} else if ((filterMask & IDREF) != 0) {
//System.out.println("IDREF");
return validElement(el, Attribute.IDREF_TYPE);
} else if ((filterMask & IDREFS) != 0) {
//System.out.println("IDREFS");
return validElement(el, Attribute.IDREFS_TYPE);
} else if ((filterMask & ENTITY) != 0) {
//System.out.println("ENTITY");
return validElement(el, Attribute.ENTITY_TYPE);
} else if ((filterMask & ENTITIES) != 0) {
//System.out.println("ENTITIES");
return validElement(el, Attribute.ENTITIES_TYPE);
} else if ((filterMask & ENUMERATED) != 0) {
//System.out.println("ENUMERATED");
return validElement(el, Attribute.ENUMERATED_TYPE);
} else if ((filterMask & NMTOKEN) != 0) {
//System.out.println("NMTOKEN");
return validElement(el, Attribute.NMTOKEN_TYPE);
} else if ((filterMask & NMTOKENS) != 0) {
//System.out.println("NMTOKENS");
return validElement(el, Attribute.NMTOKENS_TYPE);
} else if ((filterMask & NOTATION) != 0) {
//System.out.println("NOTATION");
return validElement(el, Attribute.NOTATION_TYPE);
}
}
return false;
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
elementNamespace = Namespace.getNamespace(
(String) in.readObject(), (String) in.readObject());
attributeNamespace = Namespace.getNamespace(
(String) in.readObject(), (String) in.readObject());
}
/**
* @param attributeName The attributeName to set.
*/
public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}
/**
* @param attributeNamespace The attributeNamespace to set.
*/
public void setAttributeNamespace(Namespace attributeNamespace) {
this.attributeNamespace = attributeNamespace;
}
/**
* @param attributeValue The attributeValue to set.
*/
public void setAttributeValue(String attributeValue) {
this.attributeValue = attributeValue;
}
/**
* Set visiblity of <code>Elements</code> with <code>Cdata</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setCdataVisible(boolean visible) {
if (visible) {
filterMask |= CDATA;
}
else {
filterMask &= ~CDATA;
}
}
/**
* Set this filter to allow all legal JDOM Element.
*/
public void setDefaultMask() {
filterMask = CDATA | ENTITIES | ENTITY |
ENUMERATED | ID | IDREF |
IDREFS | NMTOKEN | NMTOKENS |
NOTATION | UNDECLARED;
}
/**
* @param elementName The elementName to set.
*/
public void setElementName(String elementName) {
this.elementName = elementName;
}
/**
* @param elementNamespace The elementNamespace to set.
*/
public void setElementNamespace(Namespace elementNamespace) {
this.elementNamespace = elementNamespace;
}
/**
* Set visiblity of <code>Elements</code> with <code>Entity</code> list type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setEntitiesVisible(boolean visible) {
if (visible) {
filterMask |= ENTITIES;
}
else {
filterMask &= ~ENTITIES;
}
}
/**
* Set visiblity of <code>Elements</code> with <code>Entity</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setEntityVisible(boolean visible) {
if (visible) {
filterMask |= ENTITY;
}
else {
filterMask &= ~ENTITY;
}
}
/**
* Set visiblity of <code>Elements</code> with <code>enumerated</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setEnumeratedVisible(boolean visible) {
if (visible) {
filterMask |= ENUMERATED;
}
else {
filterMask &= ~ENUMERATED;
}
}
/**
* Set filtering mask.
* @param mask
*/
public void setFilterMask(int mask) {
setDefaultMask();
filterMask &= mask;
}
/**
* Set visiblity of <code>Elements</code> with <code>Idref</code> list type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setIdRefsVisible(boolean visible) {
if (visible) {
filterMask |= IDREFS;
}
else {
filterMask &= ~IDREFS;
}
}
/**
* Set visiblity of <code>Elements</code> with <code>Idref</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setIdRefVisible(boolean visible) {
if (visible) {
filterMask |= IDREF;
}
else {
filterMask &= ~IDREF;
}
}
/**
* Set visiblity of <code>Elements</code> with <code>Id</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setIdVisible(boolean visible) {
if (visible) {
filterMask |= ID;
}
else {
filterMask &= ~ID;
}
}
/**
* Set visiblity of <code>Element</code> objects.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setNmTokensVisible(boolean visible) {
if (visible) {
filterMask |= NMTOKENS;
}
else {
filterMask &= ~NMTOKENS;
}
}
/**
* Set visiblity of <code>Element</code> objects.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setNmTokenVisible(boolean visible) {
if (visible) {
filterMask |= NMTOKEN;
}
else {
filterMask &= ~NMTOKEN;
}
}
/**
* Set visiblity of <code>Element</code> objects.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setNotationVisible(boolean visible) {
if (visible) {
filterMask |= NOTATION;
}
else {
filterMask &= ~NOTATION;
}
}
/**
* Set visiblity of <code>Elements</code> with <code>Undeclared</code> type <code>Attribute</code>.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setUndeclaredVisible(boolean visible) {
if (visible) {
filterMask |= UNDECLARED;
}
else {
filterMask &= ~UNDECLARED;
}
}
// called if attribute value unspecified
private boolean validAttributeType(Attribute attribute, int attributeType) {
if (attribute == null) {
return false;
}
return attribute.getAttributeType() == attributeType;
}
// called if attribute value specified
private boolean validAttributeValueAndType(Attribute attribute, int attributeType) {
if (attribute == null) {
return false;
}
return attributeValue.equals(attribute.getValue()) && attribute.getAttributeType() == attributeType;
}
private boolean validElement(Element element, int attributeType) {
if (elementName != null) {
return validSpecifiedElement(element, attributeType);
} else {
return validUnspecifiedElement(element, attributeType);
}
}
// called if attribute name specified
private boolean validSpecifiedAttribute(Attribute attribute, int attributeType) {
if (attribute == null) {
return false;
}
if (attributeName.equals(attribute.getName()) && attributeValue != null){
return validAttributeValueAndType(attribute, attributeType);
}
if (attributeName.equals(attribute.getName()) && attributeValue == null){
return validAttributeType(attribute, attributeType);
}
return false;
}
// called if element name specified
private boolean validSpecifiedElement(Element element, int attributeType) {
if (this.elementName.equals(element.getName()) && attributeName != null ) {
return validSpecifiedAttribute(element.getAttribute(attributeName), attributeType);
}
if (this.elementName.equals(element.getName()) && attributeName == null ){
Iterator attributes = element.getAttributes().iterator();
while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next();
if (validUnspecifiedAttribute(attribute, attributeType)) {
return true;
}
}
}
return false;
}
// called if attribute name unspecified
private boolean validUnspecifiedAttribute(Attribute attribute, int attributeType) {
if (attribute == null) {
return false;
}
if (attributeValue != null){
return validAttributeValueAndType(attribute, attributeType);
} else {
return validAttributeType(attribute, attributeType);
}
}
// called if element name unspecified
private boolean validUnspecifiedElement(Element element, int attributeType) {
if (this.elementName == null && attributeName != null) {
return validSpecifiedAttribute(element.getAttribute(attributeName), attributeType);
}
if (this.elementName == null && attributeName == null){
Iterator attributes = element.getAttributes().iterator();
while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next();
if (attributeValue != null && validAttributeValueAndType(attribute, attributeType)) {
return true;
}
if (attributeValue == null && validAttributeType(attribute, attributeType)) {
return true;
}
}
}
return false;
}
// Support a custom Namespace serialization so no two namespace
// object instances may exist for the same prefix/uri pair
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
// We use writeObject() and not writeUTF() to minimize space
// This allows for writing pointers to already written strings
out.writeObject(elementNamespace.getPrefix());
out.writeObject(elementNamespace.getURI());
out.writeObject(attributeNamespace.getPrefix());
out.writeObject(attributeNamespace.getURI());
}
}
More information about the jdom-interest
mailing list