58a59,61 > import java.util.Map; > import java.util.HashMap; > import java.util.Iterator; 63a67,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.
> * 66a83 > * @author Tomislaw 'Cromax' Kitynski 78a96,98 > /** Map of attributes to compare (String name -> String[] values) */ > protected Map attributes; > 112a133,170 > /**Adds an attribute with its acceptable values array.
> > @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.
> > @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; > } > } > } > 116c174 < * @param obj The object to verify. --- > * @param obj The object to verify. 120,142c178,199 < 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; --- > public boolean matches(Object obj) > { > //- This means: > // > // return > // obj is-an Element && > // obj.matchesName() && > // obj.matchesNamespace() && > // obj.matchesAttributes(); > // > if (obj instanceof Element) > { > Element el = (Element)obj; > > 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; 172c229,285 < } --- > > //- 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 the filter.
> > @param element >Element
instance whose attributes are about to
> be matched against those supplied to the filter
> */
> 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