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.ArrayList;
023import java.util.List;
024
025import org.biojava.utils.ChangeVetoException;
026import org.xml.sax.SAXException;
027
028/**
029 * Deals with database crossreferences (xrefs)
030 *
031 * @author Hanning Ni    Doubletwist Inc
032 */
033public class AGAVEXrefsPropHandler
034               extends StAXPropertyHandler implements AGAVEDbIdCallbackItf, AGAVEXrefCallbackItf
035{
036  // set up factory method
037  public static final StAXHandlerFactory AGAVE_XREFS_PROP_HANDLER_FACTORY
038    = new StAXHandlerFactory() {
039    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
040      return new AGAVEXrefsPropHandler(staxenv);
041    }
042  };
043  private AGAVEXrefs xrefs ;
044
045   AGAVEXrefsPropHandler(StAXFeatureHandler staxenv) {
046    // execute superclass method to setup environment
047    super(staxenv);
048    setHandlerCharacteristics("xrefs", true);
049    xrefs = new AGAVEXrefs() ;
050    super.addHandler(new ElementRecognizer.ByLocalName("db_id"),
051         AGAVEDbIdPropHandler.AGAVE_DBID_PROP_HANDLER_FACTORY);
052    super.addHandler(new ElementRecognizer.ByLocalName("xref"),
053         AGAVEXrefPropHandler.AGAVE_XREF_PROP_HANDLER_FACTORY);
054
055  }
056
057  public void addDbId(AGAVEDbId db_id)
058  {
059      xrefs.addDbId( db_id) ;
060  }
061
062  public void addXref( AGAVEXref xref)
063  {
064      xrefs.addXref( xref ) ;
065  }
066
067   public void endElementHandler(
068                String nsURI,
069                String localName,
070                String qName,
071                StAXContentHandler handler)
072                throws SAXException
073  {
074       try{
075          List set = (List)UtilHelper.getProperty(staxenv.featureTemplate.annotation,"xrefs") ;
076          if( set == null )
077          {
078              set = new ArrayList(1);
079              set.add( xrefs );
080              staxenv.featureTemplate.annotation.setProperty( "xrefs", set) ;
081          }
082          else
083          {
084              set.add( xrefs );
085          }
086       }catch(ChangeVetoException e){
087         throw new SAXException( e.getMessage() ) ;
088       }
089  }
090}