1c1 < /*-- --- > /*-- 7c7 < --- > 11c11 < --- > 14c14 < --- > 16,17c16,17 < notice, this list of conditions, and the disclaimer that follows < these conditions in the documentation and/or other materials --- > notice, this list of conditions, and the disclaimer that follows > these conditions in the documentation and/or other materials 23c23 < --- > 27,29c27,29 < < In addition, we request (but do not require) that you include in the < end-user documentation provided with the redistribution and/or in the --- > > In addition, we request (but do not require) that you include in the > end-user documentation provided with the redistribution and/or in the 33c33 < Alternatively, the acknowledgment may be graphical using the logos --- > Alternatively, the acknowledgment may be graphical using the logos 49,50c49,50 < This software consists of voluntary contributions made by many < individuals on behalf of the JDOM Project and was originally --- > This software consists of voluntary contributions made by many > individuals on behalf of the JDOM Project and was originally 54c54 < --- > 58a59 > import java.util.*; 64a66,79 > *
Additionaly, ElementFilter
allows to narrow the result list
> * by matching against elements' attribute(s) and ev. their value(s).
> * Element
will be added to the result list if ALL of the supplied
> * attributes exists
> * in the Element
(AND operation) and if each of these attributes'
> * values matches ANY corresponding value (OR operation).
There is no OR-match for attributes' names, since DTD does not allow to > * define attributes in a mutualy exclusive manner however, because of #IMPLIED > * clause, such a behaviour might be potentialy desired.
> * > *There is no AND-match for attribute's values for obvious reason. Matching > * against regular expressions (regexp) might be considered.
> * 66a82 > * @author Tomislaw 'Cromax' Kitynski 71,72c87 < private static final String CVS_ID = < "@(#) $RCSfile: ElementFilter.java,v $ $Revision: 1.8 $ $Date: 2003/04/02 01:56:58 $ $Name: jdom_1_0_b9_rc2 $"; --- > //- fields.protected.instance -- 79c94,104 < --- > > /** Map of attributes to compare (String name -> String[] values) */ > protected Map attributes; > > //- fields.private.static -- > > private static final String CVS_ID = > "@(#) $RCSfile: ElementFilter.java,v $ $Revision: 1.8 $ $Date: 2003/04/02 01:56:58 $ $Name: jdom_1_0_b9_rc2 $"; > > //- constructors.public -- > 114,144c139,141 < /** < * Check to see if the object matches a predefined set of rules. < * < * @param obj The object to verify. < * @returntrue
if the objected matched a predfined
< * set of rules.
< */
< public boolean matches(Object obj) {
< if (obj instanceof Element) {
< Element element = (Element) obj;
< if (name == null) {
< if (namespace == null) {
< return true;
< }
< else {
< return namespace.equals(element.getNamespace());
< }
< }
< else {
< if (name.equals(element.getName())) {
< if (namespace == null) {
< return true;
< }
< else {
< return namespace.equals(element.getNamespace());
< }
< }
< }
< }
< return false;
< }
---
> //- methods.public.instance --
>
> /** Adds attribute with its acceptable values array.
145a143,222 > #author Cromax > #version 1.0, 2004-01-01 > @param name > attribute name > @param values > array of acceptable attribute's values to match against; > ifvalues == null
then we are looking just
> for the presence of the name
> */
> public void addAttribute(String name, String[] values)
> {
> if (this.attributes == null)
> {
> this.attributes = new HashMap();
> }
>
> this.attributes.put(name, values);
> }
>
> /** Remove given attribute with its values from the filter.
>This might be useful if the filter is about to be reused.
> > #author Cromax > #version 1.0, 2004-01-01 > @param name > attribute name to remove > */ > public void removeAttribute(String name) > { > if (this.attributes != null) > { > this.attributes.remove(name); > > if (this.attributes.size() == 0) > { > this.attributes = null; > } > } > } > > /**Check to see if the object matches a predefined set of rules.
> > #author ??? > #author Cromax > #version 1.1, 2004-01-01 > > @param obj > The object to verify. > @return >true
if the objected matched a predfined set of rules.
>
> #history 1.2, Cromax: Element casting done just once (as in old code)
> #history 1.1, Cromax: added matching against attributes, code cleanup
> */
> public boolean matches(Object obj)
> {
> if (obj instanceof Element)
> {
> Element el = (Element)obj;
>
> //- This means:
> // return
> // obj is-an Element &&
> // obj.matchesName() &&
> // obj.matchesNamespace() &&
> // obj.matchesAttributes();
> return
> (this.name == null || this.name.equals(el.getName())) &&
> (this.namespace == null ||
> this.namespace.equals(el.getNamespace())) &&
> (this.attributes == null || this.matchesAttributes(el));
> }
>
> return false;
> }
>
> //- methods.public.instance.overriden:Object --
>
> //- There is no check against attributes's values --- maybe it shall be?
> //
169c246,308
< }
---
>
> //- methods.private.instance --
>
> //- This method is currently private, but maybe there is
> // a good reason, to make it protected? In that case we
> // can't be sure if this.attributes != null.
> //
> /** Tells if given element's attributes' values match those set > in filter.
> > #author Cromax > #version 1.1, 2004-01-02 > @param element >Element
instance whose attributes are about to
> be matched against those supplied to the filter
>
> #history 1.1, Cromax: added support for null
values
> */
> private boolean matchesAttributes(Element element)
> {
> //- At this point we know for sure that attributes != null.
> Iterator it = this.attributes.keySet().iterator();
>
> //- Now proceed all the attributes.
> nextAttr:
> while (it.hasNext())
> {
> String name = (String)it.next();
> String value = element.getAttributeValue(name);
>
> //- There is no such attribute -> doesn't match, leave.
> if (value == null)
> {
> return false;
> }
>
> String[] values = (String[])this.attributes.get(name);
>
> //- No values supplied, ie. we're checking just for the name
> // and it matches, continue.
> //
> if (values == null)
> {
> continue;
> }
>
> //- Match against values.
> for (int i = 0; i < values.length; i++)
> {
> if (value.equals(values[i]))
> {
> continue nextAttr;
> }
> }
>
> //- None of the values matches, leave.
> return false;
> }
>
> //- All the requested attributes match their values, hurray! ;-)
> return true;
> }
> }
\ Brak znaku nowej linii na ko'ncu pliku