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.io.ParseException;
016import org.biojava.bio.seq.io.game.ElementRecognizer;
017import org.biojava.utils.stax.StAXContentHandler;
018import org.biojava.utils.stax.StringElementHandlerBase;
019import org.xml.sax.Attributes;
020import org.xml.sax.SAXException;
021
022/**
023 *  Handles the GAME <seq> element
024 *
025 * @author     David Huen
026 * @since      1.2
027 */
028public class GAMESeqHandler
029         extends StAXFeatureHandler {
030    // the <seq> element supplies clone name and length.
031    // other data includes a description of the sequence.
032    // <seq> does not necessarily have an accompanying <residues>
033    // seq appears to be near terminal in that it doesn't contain
034    // complex structures with their own unique_ids within it.
035    // this means that I can safely omit the duplicate entry without
036    // risking that some nested element within it goes wrong.
037
038    //database columns
039    private String seqId;
040
041    private int seqLength = 0;
042//    private byte[] buffer = null;
043//    private boolean hasResidues = false;
044//    private boolean nonUniqueEntry = false;
045
046    // set up factory method
047    /**
048     *  Description of the Field
049     */
050    public final static StAXHandlerFactory GAME_SEQ_HANDLER_FACTORY
051             =
052        new StAXHandlerFactory() {
053            public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
054                return new GAMESeqHandler(staxenv);
055            }
056        };
057
058
059    /**
060     *  Constructor for the GAMESeqHandler object
061     *
062     *@param  staxenv   Description of the Parameter
063     *@param  parentID  Description of the Parameter
064     */
065    GAMESeqHandler(StAXFeatureHandler staxenv) {
066        // stash environment
067        super(staxenv);
068
069        // setup handlers
070        // <name>
071        super.addHandler(new ElementRecognizer.ByLocalName("name"),
072            new StAXHandlerFactory() {
073                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
074                    return new NameHandler();
075                }
076            }
077                );
078
079        // <description>
080        super.addHandler(new ElementRecognizer.ByLocalName("description"),
081            new StAXHandlerFactory() {
082                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
083                    return new DescriptionHandler();
084                }
085            }
086                );
087        // <residues>
088//        super.addHandler(new ElementRecognizer.ByLocalName("residues"),
089//                GAMEResiduesHandler.GAME_RESIDUES_HANDLER_FACTORY);
090    }
091
092
093    /**
094     *  Description of the Class
095     *
096     *@author     david
097     *@created    19 January 2002
098     */
099    private class NameHandler extends StringElementHandlerBase {
100        /**
101         *  Sets the stringValue attribute of the NameHandler object
102         *
103         *@param  s  The new stringValue value
104         */
105        protected void setStringValue(String s) {
106        }
107    }
108
109
110    /**
111     *  Description of the Class
112     *
113     *@author     david
114     *@created    19 January 2002
115     */
116    private class DescriptionHandler extends StringElementHandlerBase {
117        /**
118         *  Sets the stringValue attribute of the DescriptionHandler object
119         *
120         *@param  s  The new stringValue value
121         */
122        protected void setStringValue(String s) {
123        }
124    }
125
126
127    /**
128     *  Gets the sequenceLength attribute of the GAMESeqHandler object
129     *
130     *@return    The sequenceLength value
131     */
132    public int getSequenceLength() {
133        return seqLength;
134    }
135
136
137//    /**
138//     *  Description of the Method
139//     *
140//     *@param  buffer  Description of the Parameter
141//     */
142//    public void returnSequenceBuffer(byte[] buffer) {
143//        this.buffer = buffer;
144//    }
145
146
147    public void startElementHandler(
148            String nsURI,
149            String localName,
150            String qName,
151            Attributes attrs)
152             throws SAXException 
153    {
154        // pick up attributes
155        seqId = attrs.getValue("id").trim();        
156        String length = attrs.getValue("length").trim();        
157
158        // return results
159        try {
160            listener.setName(seqId);
161            listener.addSequenceProperty("length", length);
162        }
163        catch (ParseException pe) {
164            throw new SAXException("could not set sequence properties.");
165        }
166    }
167
168
169    public void endElementHandler(
170            String nsURI,
171            String localName,
172            String qName,
173            StAXContentHandler staxHandler)
174             throws SAXException {
175    }
176}