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.List;
025import java.util.Vector;
026
027import org.biojava.utils.ChangeVetoException;
028import org.biojava.utils.stax.StAXContentHandler;
029import org.biojava.utils.stax.StringElementHandlerBase;
030import org.xml.sax.SAXException;
031
032/**
033 * Deals with database crossreferences
034 *
035 * @author David Huen
036 */
037public class GAMEDbxrefPropHandler 
038               extends StAXPropertyHandler
039{
040  // set up factory method
041  public static final StAXHandlerFactory GAME_DBXREF_PROP_HANDLER_FACTORY
042    = new StAXHandlerFactory() {
043    public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
044      return new GAMEDbxrefPropHandler(staxenv);
045    }
046  };
047
048  // the key values in a <db_xref>
049  private String XrefDb = null;
050  private String DbXrefId = null;
051
052  public class DbXrefElement
053  {
054    private String XrefDb;
055    private String DbXrefId;
056
057    private DbXrefElement(String XrefDb, String DbXrefId)
058    {
059      this.XrefDb = XrefDb;
060      this.DbXrefId = DbXrefId;
061    }
062
063    public String getXrefDb()
064    {
065      return XrefDb;
066    }
067
068    public String getDbXrefId()
069    {
070      return DbXrefId;
071    }
072  }
073
074  private class XrefDbHandler extends StringElementHandlerBase
075  {
076    protected void setStringValue(String s)
077    {
078      XrefDb = s.trim();
079    }
080  }
081
082  private class DbXrefIdHandler extends StringElementHandlerBase
083  {
084    protected void setStringValue(String s)
085    {
086      DbXrefId = s.trim();
087    }
088  }
089
090  GAMEDbxrefPropHandler(StAXFeatureHandler staxenv) {
091    // execute superclass method to setup environment
092    super(staxenv);
093
094    setHandlerCharacteristics("dbxref", true); 
095
096    // setup handlers
097    super.addHandler(new ElementRecognizer.ByLocalName("xref_db"),
098      new StAXHandlerFactory() {
099           public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
100             return new XrefDbHandler(); }
101      }
102    );
103        
104 
105    super.addHandler(new ElementRecognizer.ByLocalName("db_xref_id"),
106      new StAXHandlerFactory() {
107           public StAXContentHandler getHandler(StAXFeatureHandler staxenv) {
108             return new DbXrefIdHandler(); }
109      }
110    );
111   
112  }
113
114/*
115  public void setXrefDb(String s)
116  {
117    XrefDb = s;
118  }
119
120  public void setDbXrefId(String s)
121  {
122    DbXrefId = s;
123  }
124*/
125
126/*
127  public void startElementHandler(
128                String nsURI,
129                String localName,
130                String qName,
131                Attributes attrs)
132         throws SAXException
133  {
134//    System.out.println("GAMEDbxrefPropHandler.startElementHandler entered");
135  }
136*/
137
138/**
139 * when exiting, put the DbXrefElement into the annotation bundle
140 */
141  public void endElementHandler(
142                String nsURI,
143                String localName,
144                String qName,
145                StAXContentHandler handler)
146                throws SAXException
147  {
148    if (XrefDb == null || DbXrefId == null) {
149      // malformed <db_xref>
150      throw new SAXException("Malformed <db_xref> ");
151    }
152
153    // create the dbxref List if need be.
154    try {
155
156      if (!staxenv.featureTemplate.annotation.containsProperty("dbxref_list")) {
157        // create the necessary list element
158        staxenv.featureTemplate.annotation.setProperty("dbxref_list", new Vector());
159      }
160
161      // stash away the data in it
162      List dbxrefList = (List) staxenv.featureTemplate.annotation.getProperty("dbxref_list");
163//      System.out.println("DbXrefElement set up with " + XrefDb + " " + DbXrefId);
164      dbxrefList.add(new DbXrefElement(XrefDb, DbXrefId));
165    }
166    catch (ChangeVetoException cve) {
167      System.err.println("GAMEDbxrefPropHandler: change vetoed.");
168    }
169  }
170}