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.ssbind;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.biojava.bio.program.xff.ElementRecognizer;
028import org.biojava.utils.stax.DelegationManager;
029import org.biojava.utils.stax.StAXContentHandler;
030import org.biojava.utils.stax.StAXContentHandlerBase;
031import org.xml.sax.Attributes;
032import org.xml.sax.SAXException;
033
034/**
035 * <code>SeqSimilarityStAXHandler</code> is a base class for creating
036 * modular StAX handlers which send callbacks to a
037 * <code>SeqSimilarityStAXAdapter</code>.
038 *
039 * @author Keith James
040 * @since 1.3
041 */
042public class SeqSimilarityStAXHandler extends StAXContentHandlerBase
043{
044    // Available handler bindings
045    private List bindings;
046    // Incremented on startElement, decremented on endElement. Used to
047    // identify which method calls to handle here and which to
048    // delegate.
049    private int depth;
050
051    // The target handler
052    protected SeqSimilarityStAXAdapter ssContext;
053
054    /**
055     * Creates a new <code>SeqSimilarityStAXHandler</code> which
056     * simply maintains a list of <code>StAXHandlerBinding</code>s and
057     * delegates to any suitable <code>StAXContentHandler</code> bound
058     * by one of them.
059     */
060    public SeqSimilarityStAXHandler(SeqSimilarityStAXAdapter ssContext)
061    {
062        this.ssContext = ssContext;
063        bindings = new ArrayList();
064    }
065
066    public void startElement(String            nsURI,
067                             String            localName,
068                             String            qName,
069                             Attributes        attrs,
070                             DelegationManager dm)
071        throws SAXException
072    {
073        depth++;
074
075        if (depth == 1)
076        {
077            handleStartElement(nsURI, localName, qName, attrs);
078        }
079        else
080        {
081            for (int i = bindings.size(); --i >= 0;)
082            {
083                StAXHandlerBinding b = (StAXHandlerBinding) bindings.get(i);
084            
085                if (b.recognizer.filterStartElement(nsURI, localName, qName, attrs))
086                {
087                    dm.delegate(b.factory.getHandler(ssContext));
088                    return;
089                }
090            }
091        }
092    }
093
094    public void endElement(String             nsURI,
095                           String             localName,
096                           String             qName,
097                           StAXContentHandler handler)
098        throws SAXException
099    {
100        depth--;
101
102        if (depth == 0)
103        {
104            handleEndElement(nsURI, localName, qName);
105        }
106    }
107
108    protected void addHandler(ElementRecognizer recognizer,
109                              StAXHandlerFactory factory)
110    {
111        bindings.add(new StAXHandlerBinding(recognizer, factory));
112    }
113
114    protected void handleStartElement(String     nsURI,
115                                      String     localName,
116                                      String     qName,
117                                      Attributes attrs)
118        throws SAXException { }
119
120    protected void handleEndElement(String nsURI,
121                                    String localName,
122                                    String qName)
123        throws SAXException { }
124}