001/*
002*                    BioJava development code
003*
004* This code may be freely distributed and modified under the
005* terms of the GNU Lesser General Public Licence.  This should
006* be distributed with the code.  If you do not have a copy,
007* see:
008*
009*      http://www.gnu.org/copyleft/lesser.html
010*
011* Copyright for this code is held jointly by the individual
012* authors.  These should be listed in @author doc comments.
013*
014* For more information on the BioJava project and its aims,
015* or to join the biojava-l mailing list, visit the home page
016* at:
017*
018*      http://www.biojava.org/
019*
020*/
021
022package org.biojava.bio.program.xff;
023
024import org.xml.sax.Attributes;
025
026/**
027 * Simple interface for filtering SAX/StAX startElement events.
028 *
029 * <p>
030 * A number of standard implementations are provided for your convenience. To
031 * implement your own filters, just implement the filterStartElement method.
032 * </p>
033 *
034 * @author Thomas Down
035 * @author Matthew Pocock
036 * @since 1.2
037 */
038
039public interface ElementRecognizer {
040  /**
041   * Recognize an element based upon the start element parameters.
042   *
043   * @param nsURI     the uri of the element to filter
044   * @param localName the local name of the element to filter
045   * @param qName     the qName of the element to filter
046   * @param attrs     the attributes associated with the element to filter
047   * @return          true if this element is accepted, false otherwise
048   */
049  public boolean filterStartElement(String nsURI, String localName, String qName, Attributes attrs);
050
051  public static final ElementRecognizer ALL = new AllElementRecognizer();
052
053  public static class AllElementRecognizer implements ElementRecognizer {
054    public boolean filterStartElement(String nsURI,
055                                      String localName,
056                                      String qName,
057                                      Attributes attrs)
058    {
059      return true;
060    }
061
062    public String toString()
063    {
064      return "ALL";
065    }
066  }
067
068  /**
069   * Filter elements on the existence of a specified attribute.
070   */
071
072  public static class HasAttribute implements ElementRecognizer {
073    private String attributeName;
074
075    public HasAttribute(String name) {
076      attributeName = name;
077    }
078
079    public boolean filterStartElement(String nsURI,
080                                      String localName,
081                                      String qName,
082                                      Attributes attrs)
083    {
084      return (attrs.getValue(attributeName) != null);
085    }
086
087    public String toString()
088    {
089      return "HasAttribute[name=" + attributeName + "]";
090    }
091  }
092
093  /**
094   * Filter elements by name and namespace.
095   */
096
097  public static class ByNSName implements ElementRecognizer {
098    private String nsURI;
099    private String localName;
100
101    public ByNSName(String nsURI, String localName) {
102      this.nsURI = nsURI;
103      this.localName = localName;
104    }
105
106    public boolean filterStartElement(String nsURI,
107                                      String localName,
108                                      String qName,
109                                      Attributes attrs)
110    {
111      return (localName.equals(this.localName) && nsURI.equals(this.nsURI));
112    }
113
114    public String toString()
115    {
116      return "ByNSName[uri=" + nsURI + " name=" + localName + "]";
117    }
118  }
119
120  /**
121   * Filter elements by local name (not recommended).
122   */
123
124  public static class ByLocalName implements ElementRecognizer {
125    private String localName;
126
127    public ByLocalName(String localName) {
128      this.localName = localName;
129    }
130
131    public boolean filterStartElement(String nsURI,
132                                      String localName,
133                                      String qName,
134                                      Attributes attrs)
135    {
136      return (localName.equals(this.localName));
137    }
138
139    public String toString()
140    {
141      return "ByLocalName[name=" + localName + "]";
142    }
143  }
144}