001/**
002 *  BioJava development code This code may be freely distributed and modified
003 *  under the terms of the GNU Lesser General Public Licence. This should be
004 *  distributed with the code. If you do not have a copy, see:
005 *  http://www.gnu.org/copyleft/lesser.html Copyright for this code is held
006 *  jointly by the individual authors. These should be listed in
007 *
008 *@author    doc comments. For more information on the BioJava project and its
009 *      aims, or to join the biojava-l mailing list, visit the home page at:
010 *      http://www.biojava.org/
011 */
012
013package org.biojava.bio.seq.io.game12;
014
015import org.biojava.bio.seq.StrandedFeature;
016import org.biojava.bio.seq.io.game.ElementRecognizer;
017import org.biojava.bio.symbol.Location;
018import org.biojava.bio.symbol.PointLocation;
019import org.biojava.bio.symbol.RangeLocation;
020import org.biojava.utils.stax.IntElementHandlerBase;
021import org.biojava.utils.stax.StAXContentHandler;
022
023/**
024 *  Handles the GAME &lt;<span>&gt; element.
025 *  Subclass this to parse &lt;<span>&gt; and get your result somewhere useful.
026 *
027 * @author     David Huen
028 * @since      1.2
029 */
030public class GAMESpanHandler
031         extends StAXFeatureHandler {
032    // <span> captures sequence locations.
033
034    // database columns
035    int start;
036    int end;
037    boolean gotStart = false;
038    boolean gotEnd = false;
039    Location loc;
040    StrandedFeature.Strand strand;
041
042    // set up factory method
043    /**
044     *  Description of the Field
045     */
046    public final static StAXHandlerFactory GAME_SPAN_HANDLER_FACTORY
047             =
048        new StAXHandlerFactory() {
049            public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
050                return new GAMESpanHandler(staxenv);
051            }
052        };
053
054
055    /**
056     *  Constructor for the GAMESpanHandler object
057     *
058     *@param  staxenv   Description of the Parameter
059     *@param  parentID  Description of the Parameter
060     */
061    GAMESpanHandler(StAXFeatureHandler staxenv) {
062        // setup environment
063        super(staxenv);
064
065        // setup handlers
066        // <start>
067        super.addHandler(new ElementRecognizer.ByLocalName("start"),
068            new StAXHandlerFactory() {
069                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
070                    return new StartHandler();
071                }
072            }
073                );
074        // <end>
075        super.addHandler(new ElementRecognizer.ByLocalName("end"),
076            new StAXHandlerFactory() {
077                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
078                    return new EndHandler();
079                }
080            }
081                );
082    }
083
084    private class StartHandler extends IntElementHandlerBase {
085        /**
086         *  Sets the intValue attribute of the StartHandler object
087         *
088         *@param  startVal  The new intValue value
089         */
090        protected void setIntValue(int startVal) {
091            start = startVal;
092            gotStart = true;
093        }
094    }
095
096
097    private class EndHandler extends IntElementHandlerBase {
098        /**
099         *  Sets the intValue attribute of the EndHandler object
100         *
101         *@param  endVal  The new intValue value
102         */
103        protected void setIntValue(int endVal) {
104            end = endVal;
105            gotEnd = true;
106        }
107    }
108
109    public void endElementHandler(
110            String nsURI,
111            String localName,
112            String qName,
113            StAXContentHandler contentHandler) {
114        // prevalidate
115        if (!gotStart || !gotEnd) {
116            return;
117        }
118
119        // create a RangeLocation that embodies the info
120        // remember that in their nomenclature, a point
121        // location with strand can only be done by
122        // two coordinates separated by one.
123
124        if (start < end) strand = StrandedFeature.POSITIVE;
125        else if (start > end) strand = StrandedFeature.NEGATIVE;
126        else strand = StrandedFeature.UNKNOWN;
127
128        int min = Math.min(start,end);
129        int max = Math.max(start,end);
130
131        if (Math.abs(start - end) == 1) {
132            // they've got a point location in mind
133            loc = new PointLocation(min);    
134        }
135        else {
136            // range location required
137            loc = new RangeLocation(min, max);
138        }
139    }
140}
141