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.xml.sax.Attributes;
026import org.xml.sax.SAXException;
027
028/**
029 *
030 * @author Hanning Ni    Doubletwist Inc
031 */
032public class AGAVEMapLocationPropHandler  extends StAXPropertyHandler
033{
034
035   // set up factory method
036  public static final StAXHandlerFactory AGAVE_MAP_LOCATION_PROP_HANDLER_FACTORY
037    = new StAXHandlerFactory() {
038    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
039      return new AGAVEMapLocationPropHandler(staxenv);
040    }
041  };
042  private AGAVEMapLocation ml ;
043
044  AGAVEMapLocationPropHandler(StAXFeatureHandler staxenv) {
045    // execute superclass method to setup environment
046    super(staxenv);
047    setHandlerCharacteristics("map_location", true);
048    ml = new AGAVEMapLocation() ;
049    super.addHandler(new ElementRecognizer.ByLocalName("map_position"),
050         AGAVEMapPositionPropHandler.AGAVE_MAP_POSITION_PROP_HANDLER_FACTORY);
051  }
052
053  public void addMapPosition(AGAVEMapPosition pos)
054  {
055      ml.addPosition( pos ) ;
056  }
057  public void startElementHandler(
058                String nsURI,
059                String localName,
060                String qName,
061                Attributes attrs)
062         throws SAXException
063  {
064     ml.setMapType( attrs.getValue("map_type")  ) ;
065     ml.setChromosome( attrs.getValue("chromsome")  ) ;
066     ml.setUnits( attrs.getValue("units")  ) ;
067     ml.setSource( attrs.getValue("source")  ) ;
068     ml.setSubSeqStart( attrs.getValue("sebseq_start")  ) ;
069     ml.setOrientation( attrs.getValue("orientation")  ) ;
070  }
071
072
073
074   public void endElementHandler(
075                String nsURI,
076                String localName,
077                String qName,
078                StAXContentHandler handler)
079                throws SAXException
080  {
081      try{
082         Object ob =UtilHelper.getProperty(staxenv.featureTemplate.annotation,"map_location");
083         if( ob != null )
084             ((List)ob).add( ml ) ;
085         else
086         {
087             List mls = new ArrayList(1) ;
088             mls.add( ml ) ;
089             staxenv.featureTemplate.annotation.setProperty("map_location", mls);
090         }
091      }catch(Exception e){
092         throw new SAXException(e.getMessage() ) ;
093      }
094  }
095}
096