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.game;
023
024import java.util.ListIterator;
025
026import org.biojava.bio.seq.io.ParseException;
027import org.biojava.utils.stax.DelegationManager;
028import org.biojava.utils.stax.StAXContentHandler;
029import org.biojava.utils.stax.StringElementHandlerBase;
030import org.xml.sax.Attributes;
031import org.xml.sax.SAXException;
032
033/**
034 * StAX handler for the GAME <name> element.
035 * derived from Thomas Down's PropDetailHandler
036 *
037 * @author David Huen
038 * @author Thomas Down
039 * @since 1.8
040 */
041
042public class GAMENamePropHandler extends StringElementHandlerBase {
043    public static final StAXHandlerFactory GAME_NAME_PROP_HANDLER_FACTORY = new StAXHandlerFactory() {
044            public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
045//                System.out.println ("GAMENamePropHandler factory called.");
046                return new GAMENamePropHandler(staxenv);
047            }
048        } ;
049
050  private StAXFeatureHandler staxenv;
051  // this class is not derived from StAXPropertyHandler and doesn't inherit
052  // any of the handlerStack maintenance code.  However it does offer
053  // context either so it can be omitted from the stack.
054  public GAMENamePropHandler(StAXFeatureHandler staxenv) {
055    super();
056    this.staxenv = staxenv;
057  }
058
059  public void startElement(String nsURI,
060                                    String localName,
061                                    String qName,
062                                    Attributes attrs,
063                                    DelegationManager dm)
064         throws SAXException
065  {
066//     System.out.println("GAMENamePropHandler.startElement entered.");
067     super.startElement(nsURI, localName, qName, attrs, dm);
068//     System.out.println("GAMENamePropHandler.startElement left.");
069  }
070
071  protected void setStringValue(String s)
072        throws SAXException
073  {
074    int currLevel = staxenv.getLevel();
075//    System.out.println("GAMENamePropHandler.setStringValue entered. currlevel: " + currLevel);
076
077    if (currLevel >=1) {
078      // search down stack for callback handler
079      ListIterator li = staxenv.getHandlerStackIterator(currLevel);
080//    System.out.println("GAMENamePropHandler.setStringValue entered. got ListIterator");      
081      while (li.hasPrevious()) {
082        Object ob = li.previous();
083//    System.out.println("GAMENamePropHandler.setStringValue entered. got stack object");      
084        if (ob instanceof GAMENameCallbackItf) {
085          // we have a nesting handler, use it
086//    System.out.println("GAMENamePropHandler.setStringValue calling back");              
087          ((GAMENameCallbackItf) ob).NameSetStringValue(s);
088          return;
089        }
090      }
091    }
092    //  default is to just set the name property to the string.
093    try {
094      staxenv.getFeatureListener().addFeatureProperty("name", s);
095    }
096    catch (ParseException pe) {
097      throw new SAXException(pe);
098    }
099  }
100}