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.program.xff;
023
024import org.biojava.bio.seq.Feature;
025import org.biojava.bio.seq.StrandedFeature;
026import org.biojava.utils.stax.DelegationManager;
027import org.biojava.utils.stax.StAXContentHandler;
028import org.xml.sax.Attributes;
029import org.xml.sax.SAXException;
030
031/**
032 * StAX handler for XFF strandedFeature type.  By default, XFFFeatureSetHandler
033 * uses this handler for all feature elements which have the <code>strand</code>
034 * attribute.
035 *
036 * <p>
037 * Like the basic <code>FeatureHandler</code>, this class can be subclassed
038 * to give handlers for more specialized feature types.
039 * </p>
040 *
041 * @author Thomas Down
042 * @since 1.2
043 */
044
045public class StrandedFeatureHandler extends FeatureHandler {
046    boolean inFeature = false;
047
048    public static final XFFPartHandlerFactory STRANDEDFEATURE_HANDLER_FACTORY = new XFFPartHandlerFactory() {
049            public StAXContentHandler getPartHandler(XFFFeatureSetHandler xffenv) {
050                return new StrandedFeatureHandler(xffenv);
051            }
052        } ;
053
054    public StrandedFeatureHandler(XFFFeatureSetHandler xffenv) {
055        super(xffenv);
056    }
057
058    protected Feature.Template createFeatureTemplate() {
059        return new StrandedFeature.Template();
060    }
061
062    protected StrandedFeature.Template getStrandedFeatureTemplate() {
063        return (StrandedFeature.Template) getFeatureTemplate();
064    }
065
066    public void startElement(String nsURI,
067                             String localName,
068                             String qName,
069                             Attributes attrs,
070                             DelegationManager dm)
071         throws SAXException
072    {
073        if (!inFeature) {
074            // This is the root startElement.  Check the strand= attribute.
075
076            String strands = attrs.getValue("strand");
077            StrandedFeature.Template ft = getStrandedFeatureTemplate();
078            if (strands != null) {
079                if (strands.equals("+")) {
080                    ft.strand = StrandedFeature.POSITIVE;
081                } else if (strands.equals("-")) {
082                    ft.strand = StrandedFeature.NEGATIVE;
083                } else {
084                    ft.strand = StrandedFeature.UNKNOWN;
085                }
086            } else {
087                ft.strand = StrandedFeature.UNKNOWN;
088            }
089
090            inFeature = true;
091        }
092
093        // Pass everything on to the basic feature parser.
094
095        super.startElement(nsURI, localName, qName, attrs, dm);
096    }
097}