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.xml.sax.Attributes;
025import org.xml.sax.SAXException;
026
027/**
028 * 
029 * @author Hanning Ni    Doubletwist Inc
030 */
031public class AGAVEMapPositionPropHandler  extends StAXPropertyHandler
032    implements AGAVEDbIdCallbackItf {
033
034   // set up factory method
035  public static final StAXHandlerFactory AGAVE_MAP_POSITION_PROP_HANDLER_FACTORY
036    = new StAXHandlerFactory() {
037    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
038      return new AGAVEMapPositionPropHandler(staxenv);
039    }
040  };
041  private AGAVEMapPosition mp ;
042
043   AGAVEMapPositionPropHandler(StAXFeatureHandler staxenv) {
044    // execute superclass method to setup environment
045    super(staxenv);
046    setHandlerCharacteristics("map_position", true);
047    mp = new AGAVEMapPosition() ;
048    super.addHandler(new ElementRecognizer.ByLocalName("db_id"),
049         AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY);
050
051  }
052
053  public void addDbId(AGAVEDbId db_id)
054  {
055     mp.addDbId( db_id) ;
056  }
057  public void startElementHandler(
058                String nsURI,
059                String localName,
060                String qName,
061                Attributes attrs)
062         throws SAXException
063  {
064     mp.setPos( attrs.getValue("pos")  ) ;
065  }
066
067
068
069   public void endElementHandler(
070                String nsURI,
071                String localName,
072                String qName,
073                StAXContentHandler handler)
074                throws SAXException
075  {
076        int currLevel = staxenv.getLevel();
077        if (currLevel >=1)
078        {
079            ListIterator li = staxenv.getHandlerStackIterator(currLevel);
080            while (li.hasPrevious())
081            {
082                Object ob = li.previous();
083                if (ob instanceof AGAVEMapLocationPropHandler)
084                {
085                    ((AGAVEMapLocationPropHandler) ob).addMapPosition( mp );
086                    return;
087                }
088            }
089
090        }
091  }
092}