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 org.biojava.bio.symbol.RangeLocation;
025import org.biojava.utils.ChangeVetoException;
026import org.biojava.utils.stax.StAXContentHandler;
027import org.xml.sax.Attributes;
028import org.xml.sax.SAXException;
029
030/**
031 * Handles the GAME <seq> element
032 *
033 * @since David Huen
034 * @since 1.2
035 */
036public class GAMESeqPropHandler 
037               extends StAXPropertyHandler 
038               implements GAMENameCallbackItf {
039  // the <seq> element supplies clone name and length.
040  // other data includes a description of the sequence.
041  // we will stuff the name as clone_name in an annotation.
042
043  // set up factory method
044  public static final StAXHandlerFactory GAME_SEQ_PROP_HANDLER_FACTORY 
045    = new StAXHandlerFactory() {
046    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
047//      System.out.println("GAMESeqPropHandler factory called.");
048      StAXContentHandler temp = new GAMESeqPropHandler(staxenv);
049//      System.out.println("GAMESeqPropHandler factory created " + temp);  
050//      System.out.println("");
051//      if (temp == null) System.out.println("GAMESeqPropHandler instantiation failed");
052//      return new GAMESeqPropHandler(staxenv);
053      return temp;
054    }
055  };
056
057  GAMESeqPropHandler(StAXFeatureHandler staxenv) {
058    // execute superclass method to setup environment
059    super(staxenv);
060//    System.out.println("GAMESeqPropHandler constructor called.");
061    setHandlerCharacteristics("seq", true);
062
063    // setup handlers
064    // <name>
065    super.addHandler(new ElementRecognizer.ByLocalName("name"),
066      GAMENamePropHandler.GAME_NAME_PROP_HANDLER_FACTORY);
067    // <description>
068    super.addHandler(new ElementRecognizer.ByLocalName("description"),
069      GAMEDescriptionPropHandler.GAME_DESCRIPTION_PROP_HANDLER_FACTORY);
070    // <residues>
071    super.addHandler(new ElementRecognizer.ByLocalName("residues"),
072      GAMEResiduesPropHandler.GAME_RESIDUES_PROP_HANDLER_FACTORY);
073//    System.out.println("GAMESeqPropHandler constructor: leaving now.");
074  }
075
076  public void NameSetStringValue(String s) {
077    if (!staxenv.featureTemplate.annotation.containsProperty("id")) {
078      // set gene name
079      try {
080       staxenv.featureTemplate.annotation.setProperty("id", s.trim());
081      }
082      catch (ChangeVetoException cve) {
083        // baulk and discard exception
084        System.err.println("GAMEGenPropHandler: change vetoed");
085      }
086    }
087  }
088
089  public void startElementHandler(
090                String nsURI,
091                String localName,
092                String qName,
093                Attributes attrs)
094         throws SAXException
095  {
096    // pick up the sequence details set them on the sequence object
097    // I will assume the length is equivalent to the coordinate range
098    String lengthString =  attrs.getValue("length");
099
100    if (lengthString != null) {
101      staxenv.featureTemplate.location = new RangeLocation(1, Integer.parseInt(lengthString));
102    }
103  }
104}
105