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.SAXException;
025
026/**
027 * handle AGAVE xref
028 * @author Hanning Ni ,      Doubletwist Inc
029 */
030public class AGAVEXrefPropHandler  extends StAXPropertyHandler
031     implements AGAVEDbIdCallbackItf, AGAVEDbIdPropCallbackItf
032{
033   // set up factory method
034  public static final StAXHandlerFactory AGAVE_XREF_PROP_HANDLER_FACTORY
035    = new StAXHandlerFactory() {
036    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
037      return new AGAVEXrefPropHandler(staxenv);
038    }
039  };
040
041  private AGAVEXref xref ;
042
043  AGAVEXrefPropHandler(StAXFeatureHandler staxenv) {
044    // execute superclass method to setup environment
045    super(staxenv);
046    setHandlerCharacteristics("xref", true);
047    xref = new AGAVEXref() ;
048    super.addHandler(new ElementRecognizer.ByLocalName("db_id"),
049         AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY);
050    super.addHandler(new ElementRecognizer.ByLocalName("xref_property"),
051         AGAVEXrefPropPropHandler.AGAVE_XREF_PROP_PROP_HANDLER_FACTORY);
052
053  }
054
055  public void addDbId(AGAVEDbId db_id)
056  {
057      xref.setDbId( db_id) ;
058  }
059
060  public void addProperty( AGAVEProperty prop)
061  {
062     xref.addProp( prop ) ;
063  }
064
065   public void endElementHandler(
066                String nsURI,
067                String localName,
068                String qName,
069                StAXContentHandler handler)
070                throws SAXException
071  {
072        int currLevel = staxenv.getLevel();
073        if (currLevel >=1)
074        {
075            ListIterator li = staxenv.getHandlerStackIterator(currLevel);
076            while (li.hasPrevious())
077            {
078                Object ob = li.previous();
079                if (ob instanceof AGAVEXrefCallbackItf)
080                {
081                    ((AGAVEXrefCallbackItf) ob).addXref( xref );
082                    return;
083                }
084            }
085
086        }
087  }
088}