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.bio.search.SearchContentHandler;
026import org.biojava.utils.stax.DelegationManager;
027import org.biojava.utils.stax.StAXContentHandler;
028import org.biojava.utils.stax.StringElementHandlerBase;
029import org.xml.sax.Attributes;
030import org.xml.sax.SAXException;
031
032/**
033 * <code>AlignmentStAXHandler</code> handles the BlastLikeAlignment
034 * element of BioJava BlastLike XML.
035 *
036 * @author Keith James
037 * @since 1.3
038 */
039public class AlignmentStAXHandler extends SeqSimilarityStAXHandler
040{
041    public static final StAXHandlerFactory ALIGNMENT_HANDLER_FACTORY =
042        new StAXHandlerFactory()
043        {
044            public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
045            {
046                return new AlignmentStAXHandler(ssContext);
047            }
048        };
049
050    /**
051     * Creates a new instance which sends callbacks to the specified
052     * <code>SeqSimilarityStAXAdapter</code>.
053     *
054     * @param ssContext a <code>SeqSimilarityStAXAdapter</code>.
055     */
056    AlignmentStAXHandler(SeqSimilarityStAXAdapter ssContext)
057    {
058        super(ssContext);
059
060        addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE,
061                                                  "QuerySequence"),
062                   new StAXHandlerFactory()
063                   {
064                       public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
065                       {
066                           return new QuerySequenceStAXHandler();
067                       }
068                   });
069
070        addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE,
071                                                  "HitSequence"),
072                   new StAXHandlerFactory()
073                   {
074                       public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext)
075                       {
076                           return new HitSequenceStAXHandler();
077                       }
078                   });
079    }
080
081    /**
082     * <code>QuerySequenceStAXHandler</code> handles the query
083     * sequence.
084     */
085    private class QuerySequenceStAXHandler extends StringElementHandlerBase
086    {
087        private SearchContentHandler sch;
088
089        public void startElement(String            nsURI,
090                                 String            localName,
091                                 String            qName,
092                                 Attributes        attrs,
093                                 DelegationManager dm)
094            throws SAXException
095        {
096            super.startElement(nsURI, localName, qName, attrs, dm);
097
098            sch = ssContext.getSearchContentHandler();
099            sch.addSubHitProperty("querySequenceStart",
100                                  attrs.getValue("startPosition"));
101            sch.addSubHitProperty("querySequenceEnd",
102                                  attrs.getValue("stopPosition"));
103        }
104
105        protected void setStringValue(String s) throws SAXException
106        {
107            sch = ssContext.getSearchContentHandler();
108            sch.addSubHitProperty("querySequence", s);
109        }
110    }
111
112    /**
113     * <code>HitSequenceStAXHandler</code> handles the hit sequence.
114     */
115    private class HitSequenceStAXHandler extends StringElementHandlerBase
116    {
117        private SearchContentHandler sch;
118
119        public void startElement(String            nsURI,
120                                 String            localName,
121                                 String            qName,
122                                 Attributes        attrs,
123                                 DelegationManager dm)
124            throws SAXException
125        {
126            super.startElement(nsURI, localName, qName, attrs, dm);
127
128            sch = ssContext.getSearchContentHandler();
129            sch.addSubHitProperty("subjectSequenceStart",
130                                  attrs.getValue("startPosition"));
131            sch.addSubHitProperty("subjectSequenceEnd",
132                                  attrs.getValue("stopPosition"));
133        }
134
135        protected void setStringValue(String s) throws SAXException
136        {
137            sch = ssContext.getSearchContentHandler();
138            sch.addSubHitProperty("subjectSequence", s);
139        }
140    }
141}