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 * Deals with database crossreferences
029 *
030 * @author Hanning Ni    Doubletwist Inc
031 */
032public class AGAVEDbIdPropHandler
033               extends StAXPropertyHandler
034{
035
036   public static final StAXHandlerFactory AGAVE_DBID_PROP_HANDLER_FACTORY
037    = new StAXHandlerFactory() {
038    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
039      return new AGAVEDbIdPropHandler(staxenv);
040    }
041   };
042
043   private AGAVEDbId db_id ;
044   AGAVEDbIdPropHandler(StAXFeatureHandler staxenv) {
045    // execute superclass method to setup environment
046    super(staxenv);
047    setHandlerCharacteristics("db_id", true);
048  }
049
050  public void startElementHandler(
051                String nsURI,
052                String localName,
053                String qName,
054                Attributes attrs)
055         throws SAXException
056  {
057      db_id = new AGAVEDbId() ;
058      db_id.setId( attrs.getValue( "id" ) );
059      db_id.setVersion( attrs.getValue( "version" ) );
060      db_id.setDbCode( attrs.getValue( "db_code" ) ) ;
061  }
062
063  public void endElementHandler(
064                String nsURI,
065                String localName,
066                String qName,
067                StAXContentHandler handler)
068                throws SAXException
069  {
070       int currLevel = staxenv.getLevel();
071       if (currLevel >=1) {
072           ListIterator li = staxenv.getHandlerStackIterator(currLevel);
073           while( li.hasPrevious() )
074          {
075              Object ob =   li.previous() ;
076              if( ob instanceof AGAVEDbIdCallbackItf )
077              {
078                  ( (AGAVEDbIdCallbackItf) ob ).addDbId( db_id ) ;
079                 //  return ;
080              }
081           }
082       }
083       try{
084       //if no suitable handler found, save it to annotation
085        staxenv.featureTemplate.annotation.setProperty( "db_id", db_id) ;
086       }catch(Exception e){
087          throw new SAXException( e.getMessage() ) ;
088      }
089  }
090
091
092}