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.game12;
023
024import org.biojava.bio.seq.io.ParseException;
025import org.biojava.bio.seq.io.game.ElementRecognizer;
026import org.biojava.utils.stax.StAXContentHandler;
027import org.biojava.utils.stax.StringElementHandlerBase;
028import org.xml.sax.SAXException;
029
030/**
031 * Handles the GAME <aspect> element
032 *
033 * @author David Huen
034 * @since 1.8
035 */
036public class GAMEAspectHandler extends StAXFeatureHandler {
037    // this class has <function> and <dbxref> child elements.
038    private String aspectDbxrefDb = null;
039    private String aspectDbxrefId = null;
040    private String function = null;
041
042    // set up factory method
043    public static final StAXHandlerFactory GAME_ASPECT_HANDLER_FACTORY 
044      = new StAXHandlerFactory() {
045      public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
046        return new GAMEAspectHandler(staxenv);
047      }
048    };
049
050    private class DbxrefHandler extends GAMEDbxrefHandler
051    {
052        private DbxrefHandler(StAXFeatureHandler staxenv)
053        {
054            super(staxenv);
055        }
056
057        public void endElementHandler(
058                String nsURI,
059                String localName,
060                String qName,
061                StAXContentHandler contentHandler)
062            throws SAXException
063        {
064            // validate before going further
065            super.endElementHandler(nsURI, localName, qName, contentHandler);
066
067            // stash away result until processing complete
068            aspectDbxrefDb = db_xref_db;
069            aspectDbxrefId = db_xref_id;
070        }
071    }
072
073    private class FunctionHandler extends StringElementHandlerBase {
074        /**
075         *  Sets the stringValue attribute of the NameHandler object
076         *
077         *@param  s  The new stringValue value
078         */
079        protected void setStringValue(String s) {
080            function = s.trim();
081        }
082    }
083
084    GAMEAspectHandler(StAXFeatureHandler staxenv) {
085        // execute superclass method to setup environment
086        super(staxenv);
087
088        // setup handlers
089        // <function>
090        super.addHandler(new ElementRecognizer.ByLocalName("function"),
091            new StAXHandlerFactory() {
092                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
093                    return new FunctionHandler();
094                }
095            }
096        );
097        // <dbxref>
098        super.addHandler(new ElementRecognizer.ByLocalName("dbxref"),
099            new StAXHandlerFactory() {
100                public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
101                    return new DbxrefHandler(staxenv);
102                }
103            }
104        );
105    }
106
107    public void endElementHandler(
108            String nsURI,
109            String localName,
110            String qName,
111            StAXContentHandler contentHandler)
112        throws SAXException
113    {
114        if ((aspectDbxrefDb == null) || (aspectDbxrefId == null) || (function == null)) return;
115
116        try {
117            listener.addFeatureProperty("aspect:" + function, "dbxref:" + aspectDbxrefDb + "//" + aspectDbxrefId);
118        }
119        catch (ParseException pe) {
120            pe.printStackTrace();
121            throw new SAXException("unexpected exception while adding <aspect> as a feature property.");
122        }
123    }
124}
125