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.biojava.bio.BioError;
025import org.biojava.bio.BioException;
026import org.biojava.bio.seq.DNATools;
027import org.biojava.bio.seq.io.SymbolTokenization;
028import org.biojava.utils.stax.DelegationManager;
029import org.biojava.utils.stax.StAXContentHandler;
030import org.xml.sax.Attributes;
031import org.xml.sax.SAXException;
032
033/**
034 * StAX handler for GAME <residues> elements.
035 * derived from Thomas Down's PropDetailHandler
036 *
037 * <p>
038 * This takes the sequence supplied by &lt;residues&gt; elements
039 * and feeds it to a StreamParser associated with a SeqIOLIstener
040 * instance.
041 *
042 * @author David Huen
043 * @author Thomas Down
044 * @since 1.8
045 */
046public class GAMEResiduesPropHandler extends SequenceContentHandlerBase {
047  public static final StAXHandlerFactory GAME_RESIDUES_PROP_HANDLER_FACTORY = new StAXHandlerFactory() {
048          public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
049                 return new GAMEResiduesPropHandler(staxenv);
050          }
051            } ;
052
053  private StAXFeatureHandler staxenv;
054
055  public GAMEResiduesPropHandler(StAXFeatureHandler staxenv) {
056    super();
057    this.staxenv = staxenv;
058  }
059
060  public void startElement(String nsURI,
061                                    String localName,
062                                    String qName,
063                                    Attributes attrs,
064                                    DelegationManager dm)
065                     throws SAXException
066  {
067    super.startElement(nsURI, localName, qName, attrs, dm);
068
069    // set up StreamParser
070    try {
071        SymbolTokenization tokens = DNATools.getDNA().getTokenization("token");
072        super.setStreamParser(tokens.parseStream(staxenv.featureListener));
073    } catch (BioException ex) {
074        throw new BioError("Assertion failed: couldn't get tokenization from DNA alphabet");
075    }
076  }
077
078}