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.seq.io.game;
023
024import org.xml.sax.Attributes;
025
026/**
027 * Simple interface for filtering SAX/StAX startElement events
028 *
029 * @author Thomas Down
030 * @since 1.2
031 */
032
033// this class really should be refactored elsewhere.
034
035public interface ElementRecognizer {
036    public static final ElementRecognizer ALL = new AllElementRecognizer();
037
038    public static class AllElementRecognizer implements ElementRecognizer {
039        public boolean filterStartElement(String nsURI,
040                                              String localName,
041                                              String qName,
042                                              Attributes attrs)
043        {
044            return true;
045        }
046    }
047
048    /**
049     * Filter elements on the existence of a specified attribute.
050     */
051
052    public static class HasAttribute implements ElementRecognizer {
053        private String attributeName;
054
055        public HasAttribute(String name) {
056            attributeName = name;
057        }
058
059        public boolean filterStartElement(String nsURI,
060                                          String localName,
061                                          String qName,
062                                          Attributes attrs)
063        {
064            return (attrs.getValue(attributeName) != null);
065        }
066    }
067
068    /**
069     * Filter elements by name and namespace.
070     */
071
072    public static class ByNSName implements ElementRecognizer {
073        private String nsURI;
074        private String localName;
075
076        public ByNSName(String nsURI, String localName) {
077            this.nsURI = nsURI;
078            this.localName = localName;
079        }
080
081        public boolean filterStartElement(String nsURI,
082                                          String localName,
083                                          String qName,
084                                          Attributes attrs)
085        {
086            return (localName.equals(this.localName) && nsURI.equals(this.nsURI));
087        }
088    }
089
090    /**
091     * Filter elements by local name (not recommended).
092     */
093
094    public static class ByLocalName implements ElementRecognizer {
095        private String localName;
096
097        public ByLocalName(String localName) {
098            this.localName = localName;
099        }
100
101        public boolean filterStartElement(String nsURI,
102                                          String localName,
103                                          String qName,
104                                          Attributes attrs)
105        {
106            return (localName.equals(this.localName));
107        }
108    }
109
110    public boolean filterStartElement(String nsURI, String localName, String qName, Attributes attrs);
111}