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.SAXException;
020
021/**
022 *  Handles the GAME <dbxref> element
023 *
024 * @author     David Huen
025 * @since      1.2
026 */
027public class GAMEPropertyHandler
028         extends StAXFeatureHandler {
029    // <dbxref> is a container for external database references.
030    // it is possible that non-unique <dbxref> occur.
031
032    // temporary cache
033    String propertyType = null;
034    String propertyValue = null;
035
036    // set up factory method
037    /**
038     *  Description of the Field
039     */
040    public final static StAXHandlerFactory GAME_PROPERTY_HANDLER_FACTORY
041             =
042        new StAXHandlerFactory() {
043            public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
044                return new GAMEPropertyHandler(staxenv);
045            }
046        };
047
048
049    /**
050     *  Constructor for the GAMEDbxrefHandler object
051     *
052     *@param  staxenv   Description of the Parameter
053     *@param  parentID  Description of the Parameter
054     */
055    GAMEPropertyHandler(StAXFeatureHandler staxenv) {
056        // setup environment
057        super(staxenv);
058
059        // setup handlers
060        // <type>
061        super.addHandler(new ElementRecognizer.ByLocalName("type"),
062            new StAXHandlerFactory() {
063                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
064                    return new TypeHandler();
065                }
066            }
067                );
068        // <value>
069        super.addHandler(new ElementRecognizer.ByLocalName("value"),
070            new StAXHandlerFactory() {
071                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
072                    return new ValueHandler();
073                }
074            }
075                );
076    }
077
078    /**
079     *  retrieves the &lt;type&gt; field.
080     */
081    private class TypeHandler extends StringElementHandlerBase {
082        protected void setStringValue(String s) {
083            propertyType = s.trim();
084        }
085    }
086
087    /**
088     *  retrieves the &lt;value&gt; field.
089     */
090    private class ValueHandler extends StringElementHandlerBase {
091        protected void setStringValue(String s) {
092            propertyValue = s.trim();
093        }
094    }
095
096    public void endElementHandler(
097            String nsURI,
098            String localName,
099            String qName,
100            StAXContentHandler contentHandler) 
101        throws SAXException 
102    {
103        // validate before going further
104        if ((propertyType == null) || (propertyValue == null)) {
105            return;
106        }
107
108        // set up field
109        try {
110            listener.addFeatureProperty(propertyType, propertyValue);
111        }
112        catch (ParseException pe) {
113            pe.printStackTrace();
114            throw new SAXException("unexpected exception while adding <property> as a feature property.");
115        }
116
117    }
118}