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 org.biojava.bio.program.xff.ElementRecognizer;
025import org.biojava.utils.stax.DelegationManager;
026import org.biojava.utils.stax.StAXContentHandler;
027import org.biojava.utils.stax.StAXContentHandlerBase;
028import org.biojava.utils.stax.StringElementHandlerBase;
029import org.xml.sax.Attributes;
030import org.xml.sax.SAXException;
031
032/**
033 * <code>HeaderStAXHandler</code> handles the Header element of
034 * BioJava BlastLike XML.
035 *
036 * @author Keith James
037 * @author Matthew Pocock
038 * @since 1.3
039 */
040public class HeaderStAXHandler extends SeqSimilarityStAXHandler
041{
042    public static final StAXHandlerFactory HEADER_HANDLER_FACTORY =
043        new StAXHandlerFactory()
044        {
045            public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
046            {
047                return new HeaderStAXHandler(ssContext);
048            }
049        };
050
051    /**
052     * Creates a new instance which sends callbacks to the specified
053     * <code>SeqSimilarityStAXAdapter</code>.
054     *
055     * @param ssContext a <code>SeqSimilarityStAXAdapter</code>.
056     */
057    HeaderStAXHandler(SeqSimilarityStAXAdapter ssContext)
058    {
059        super(ssContext);
060
061        addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE,
062                                                  "QueryId"),
063                   new StAXHandlerFactory()
064                   {
065                       public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
066                       {
067                           return new QueryIDStAXHandler();
068                       }
069                   });
070
071        addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE,
072                                                  "QueryDescription"),
073                   new StAXHandlerFactory()
074                   {
075                       public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
076                       {
077                           return new QueryDescriptionStAXHandler();
078                       }
079                   });
080
081        addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE,
082                                                  "DatabaseId"),
083                   new StAXHandlerFactory()
084                   {
085                       public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
086                       {
087                           return new DatabaseIDStAXHandler();
088                       }
089                   });
090    }
091
092    /**
093     * <code>QueryIDStAXHandler</code> handles the query sequence ID.
094     */
095    private class QueryIDStAXHandler extends StAXContentHandlerBase
096    {
097        public void startElement(String            uri,
098                                 String            localName,
099                                 String            qName,
100                                 Attributes        attr,
101                                 DelegationManager dm)
102        throws SAXException
103        {
104            ssContext.getSearchContentHandler().setQueryID(attr.getValue("id"));
105            if (attr.getValue("queryLength") != null)
106            {
107                ssContext.getSearchContentHandler().addSearchProperty("queryLength",
108                                attr.getValue("queryLength"));
109            }
110        }
111    }
112
113    /**
114     * <code>QueryDescriptionStAXHandler</code> handles the hit
115     * description.
116     */
117    private class QueryDescriptionStAXHandler extends StringElementHandlerBase
118    {
119        protected void setStringValue(String s)
120        {
121            ssContext.getSearchContentHandler().addSearchProperty("queryDescription", s);
122        }
123    }
124
125    /**
126     * <code>DatabaseIDStAXHandler</code> handles the database ID.
127     */
128    private class DatabaseIDStAXHandler extends StAXContentHandlerBase
129    {
130        public void startElement(String            uri,
131                                 String            localName,
132                                 String            qName,
133                                 Attributes        attr,
134                                 DelegationManager dm)
135        throws SAXException
136        {
137            ssContext.getSearchContentHandler().setDatabaseID(attr.getValue("id"));
138        }
139    }
140}