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 */
021
022package org.biojava.bio.seq.io.game;
023
024import org.biojava.bio.seq.Feature;
025import org.biojava.bio.seq.StrandedFeature;
026import org.biojava.bio.symbol.PointLocation;
027import org.biojava.bio.symbol.RangeLocation;
028import org.biojava.utils.stax.IntElementHandlerBase;
029import org.biojava.utils.stax.StAXContentHandler;
030
031/**
032 * Handles the GAME <span> element
033 * Currently, it just ignores it!
034 *
035 * @author David Huen
036 * @since 1.2
037 */
038public class GAMESpanPropHandler 
039               extends StAXPropertyHandler {
040  // the <span> element supplies limits of a sequence span.
041  // unfortunately, the spans can be either numeric or
042  // alphanumeric (with cytological map_position).
043  // set up factory method
044  public static final StAXHandlerFactory GAME_SPAN_PROP_HANDLER_FACTORY 
045    = new StAXHandlerFactory() {
046    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
047      return new GAMESpanPropHandler(staxenv);
048    }
049  };
050
051  private int start = 0;
052  private int stop = 0;
053  private StAXFeatureHandler staxenv;
054
055  GAMESpanPropHandler(StAXFeatureHandler staxenv) {
056    // execute superclass method to setup environment
057    super(staxenv);
058    setHandlerCharacteristics("span", true);
059   
060    // cache environment: this is of PREVIOUS feature handler as
061    // delegation is invoked in StaxFeatureHandler itself which means
062    // that the this pointer that is passed is the Feature one.
063    this.staxenv = staxenv;
064
065    // setup handlers
066    super.addHandler(new ElementRecognizer.ByLocalName("start"),
067//      GAMEStartEndPropHandler.GAME_STARTEND_PROP_HANDLER_FACTORY);
068      new StAXHandlerFactory() {
069           public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
070             return new StartHandler(); }
071      }
072    );
073
074    super.addHandler(new ElementRecognizer.ByLocalName("end"),
075//      GAMEStartEndPropHandler.GAME_STARTEND_PROP_HANDLER_FACTORY);
076      new StAXHandlerFactory() {
077           public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
078             return new StopHandler(); }
079      }
080    );
081  }
082
083  private class StartHandler extends IntElementHandlerBase
084  {
085    protected void setIntValue(int val)
086    {
087      start = val;
088    }
089  }
090 
091  private class StopHandler extends IntElementHandlerBase
092  {
093    protected void setIntValue(int val)
094    {
095      stop = val;
096    }
097  }
098
099/*
100  public void startElementHandler(
101                String nsURI,
102                String localName,
103                String qName,
104                Attributes attrs)
105         throws SAXException
106  {
107    System.out.println("GAMESpanPropHandler.startElementHandler entered.");
108  }
109*/
110  public void endElementHandler(
111                String nsURI,
112                String localName,
113                String qName,
114                StAXContentHandler handler)
115  {
116    // check that it IS a StrandedFeature.Template
117    boolean isStrandedTemplate = staxenv.featureTemplate instanceof StrandedFeature.Template;
118
119    Feature.Template templ = staxenv.featureTemplate;
120
121    // go set strandedness and range
122    if (start < stop) {
123      templ.location = new RangeLocation(start + 1, stop);
124      if (isStrandedTemplate)
125        ((StrandedFeature.Template) templ).strand = StrandedFeature.POSITIVE;
126    }
127    else if (start > stop) {
128      staxenv.featureTemplate.location = new RangeLocation(stop + 1, start);
129      if (isStrandedTemplate)
130        ((StrandedFeature.Template) templ).strand = StrandedFeature.NEGATIVE;
131    }
132    else {
133      staxenv.featureTemplate.location = new PointLocation(start);
134      if (isStrandedTemplate)
135        ((StrandedFeature.Template) templ).strand = StrandedFeature.UNKNOWN;
136    } 
137  }
138}
139