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.seq.io.StreamParser;
025import org.biojava.bio.symbol.IllegalSymbolException;
026import org.biojava.utils.stax.DelegationManager;
027import org.biojava.utils.stax.StAXContentHandler;
028import org.biojava.utils.stax.StAXContentHandlerBase;
029import org.xml.sax.Attributes;
030import org.xml.sax.SAXException;
031
032/**
033 * StAX handler for elements containing sequence
034 *
035 * @author David Huen
036 * @since 1.8
037 */
038public class SequenceContentHandlerBase extends StAXContentHandlerBase 
039{
040    private int level = 0;
041    private StreamParser parser;
042
043    public void startElement(String nsURI,
044                             String localName,
045                             String qName,
046                             Attributes attrs,
047                             DelegationManager dm)
048         throws SAXException
049    {
050        level++;
051        if (level > 1) {
052            throw new SAXException("Found child element when expecting character data");
053        }
054    }
055
056    public void endElement(String nsURI,
057                           String localName,
058                           String qName,
059                           StAXContentHandler handler)
060        throws SAXException
061    {
062        level--;
063    }
064
065/**
066 * assign a StreamParser object to instance.
067 */
068    public void setStreamParser(StreamParser parser)
069    {
070       this.parser = parser;
071    }
072
073    public void characters(char[] ch, int start, int length) 
074        throws SAXException
075    {
076       int cnt=0;
077       int bstart = start;
078       int bcnt=0;
079       // call parser to pass Symbols to SeqIOListener
080       try {
081//         System.out.println("SequenceContentHandlerBase: " + start + " " + length);
082         while (cnt < length) {
083           // clear whitespace
084           while (cnt < length && (!(Character.isLetter(ch[start + cnt]))) ) cnt++;
085
086           // map length of non-whitespace
087           bstart = start + cnt; bcnt = 0;
088           while (cnt < length && (Character.isLetter(ch[start + cnt])) ) {
089             cnt++;
090             bcnt++;
091           }
092
093           // process current block
094           parser.characters(ch, bstart, bcnt);
095         }
096         parser.close();
097         
098       }
099       catch (IllegalSymbolException ise) {
100         throw new SAXException("SequenceContentHandlerBase: illegal symbol encountered.");
101       }
102    }
103}