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 */
021package org.biojava.bio.seq.io.agave;
022import java.util.ListIterator;
023
024import org.biojava.bio.symbol.RangeLocation;
025import org.xml.sax.Attributes;
026import org.xml.sax.SAXException;
027
028/**
029 * 
030 * @author Hanning Ni    Doubletwist Inc
031 */
032public class AGAVEQueryRegionPropHandler extends StAXFeatureHandler
033                                         implements AGAVEDbIdCallbackItf {
034
035   // set up factory metho
036  public static final StAXHandlerFactory AGAVE_QUERY_REGION_PROP_HANDLER_FACTORY
037    = new StAXHandlerFactory() {
038    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
039      return new AGAVEQueryRegionPropHandler(staxenv);
040    }
041  };
042   private AGAVEQueryRegion region ;
043   AGAVEQueryRegionPropHandler(StAXFeatureHandler staxenv) {
044    // execute superclass method to setup environment
045    super(staxenv);
046    setHandlerCharacteristics("query_region", true);
047    super.addHandler(new ElementRecognizer.ByLocalName("db_id"),
048         AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY);
049    region = new AGAVEQueryRegion() ;
050   }
051
052  public void startElementHandler(
053                String nsURI,
054                String localName,
055                String qName,
056                Attributes attrs)
057         throws SAXException
058  {
059      region.setStart( new Integer(attrs.getValue( "start" )).intValue() );
060      region.setEnd( new Integer(attrs.getValue( "end" ) ).intValue() );
061  }
062
063  public void addDbId(AGAVEDbId db_id)
064  {
065     try{
066       region.setDbId( db_id ) ;
067     }catch(Exception e){
068       e.printStackTrace() ;
069     }
070  }
071
072
073   public void endElementHandler(
074                String nsURI,
075                String localName,
076                String qName,
077                StAXContentHandler handler)
078                throws SAXException
079  {
080        try{
081           staxenv.featureTemplate.annotation.setProperty("query_region", region) ;
082        }catch(Exception e){
083          throw new SAXException( e.getMessage() ) ;
084        }
085        int currLevel = staxenv.getLevel();
086        if (currLevel >=1)
087        {
088            ListIterator li = staxenv.getHandlerStackIterator(currLevel);
089            while (li.hasPrevious())
090            {
091                Object ob = li.previous();
092                if (ob instanceof AGAVEFeatureCallbackItf)
093                {
094                    ((AGAVEFeatureCallbackItf) ob).reportFeature(
095                          new RangeLocation(region.getStart(), region.getEnd() ) );
096                    return;
097                }
098            }
099
100        }
101  }
102}