001/**
002 *  BioJava development code This code may be freely distributed and modified
003 *  under the terms of the GNU Lesser General Public Licence. This should be
004 *  distributed with the code. If you do not have a copy, see:
005 *  http://www.gnu.org/copyleft/lesser.html Copyright for this code is held
006 *  jointly by the individual authors. These should be listed in
007 *
008 *@author    doc comments. For more information on the BioJava project and its
009 *      aims, or to join the biojava-l mailing list, visit the home page at:
010 *      http://www.biojava.org/
011 */
012
013package org.biojava.bio.seq.io.game12;
014
015import org.biojava.bio.seq.io.ParseException;
016import org.biojava.bio.seq.io.SeqIOListener;
017import org.biojava.bio.seq.io.game.ElementRecognizer;
018import org.biojava.utils.stax.StAXContentHandler;
019import org.xml.sax.Attributes;
020import org.xml.sax.SAXException;
021
022/**
023 *  Handles the root GAME element
024 *
025 * @author     David Huen
026 * @since      1.2
027 */
028public class GAMEHandler
029         extends StAXFeatureHandler {
030    // there is only one GAME element encompassing the entire file
031    // in Gadfly GAME files.
032
033    /**
034     *  Constructor for the GAMEHandler object
035     *
036     */
037    public GAMEHandler(SeqIOListener listener) {
038        // initialise environment
039        this.staxenv = this;
040        this.listener = listener;
041
042        // setup handlers
043        // <seq>
044        super.addHandler(new ElementRecognizer.ByLocalName("seq"),
045                GAMESeqHandler.GAME_SEQ_HANDLER_FACTORY);
046        // <map_position>
047//        super.addHandler(new ElementRecognizer.ByLocalName("map_position"),
048//                GAMEMapPosHandler.GAME_MAP_POS_HANDLER_FACTORY);
049        // <annotation>
050        super.addHandler(new ElementRecognizer.ByLocalName("annotation"),
051                GAMEAnnotationHandler.GAME_ANNOTATION_HANDLER_FACTORY);
052        // <computational_analysis>
053//        super.addHandler(new ElementRecognizer.ByLocalName("computational_analysis"),
054//                GAMEComputationalHandler.GAME_COMP_HANDLER_FACTORY);
055    }
056
057    /**
058     *  Description of the Method
059     *
060     *@param  nsURI             Description of the Parameter
061     *@param  localName         Description of the Parameter
062     *@param  qName             Description of the Parameter
063     *@param  attrs             Description of the Parameter
064     *@exception  SAXException  Description of the Exception
065     */
066    public void startElementHandler(
067            String nsURI,
068            String localName,
069            String qName,
070            Attributes attrs)
071             throws SAXException 
072    {
073//        System.out.println("GAMEHandler startElementHandler called, localName: " + localName);
074        // check the element type
075        if (!(localName.equals("game"))) {
076            throw new SAXException("first element of file is not a game element");
077        }
078
079        // check file version
080        String version = attrs.getValue("version");
081        if (!(version.equals("1.2"))) {
082            throw new SAXException("GAME version is not 1.2");
083        }
084
085        // inform listener of new sequence
086        try {
087            listener.startSequence();
088        }
089        catch (ParseException pe) {
090            pe.printStackTrace();
091            throw new SAXException("error in GAMEAnnotationHandler.");
092        }
093    }
094
095    public void endElementHandler(
096            String nsURI,
097            String localName,
098            String qName,
099            StAXContentHandler contentHandler)
100        throws SAXException
101    {
102        // indicate end of sequence
103        try {
104            listener.endSequence();
105        }
106        catch (ParseException pe) {
107            pe.printStackTrace();
108            throw new SAXException("error in GAMEAnnotationHandler.");
109        }
110    }
111}
112