2 * BioJava development code
4 * This code may be freely distributed and modified under the
5 * terms of the GNU Lesser General Public Licence. This should
6 * be distributed with the code. If you do not have a copy,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on Mar 15, 2006
23 package org.biojava.dasobert.das2.io;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.biojava.dasobert.das2.Das2Capability;
29 import org.biojava.dasobert.das2.Das2CapabilityImpl;
30 import org.biojava.dasobert.das2.Das2Source;
31 import org.biojava.dasobert.das2.Das2SourceImpl;
32 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
33 import org.biojava.dasobert.dasregistry.DasSource;
34 import org.xml.sax.Attributes;
35 import org.xml.sax.helpers.DefaultHandler;
37 /** a parser for the DAS2 sources response
39 * @author Andreas Prlic
43 public class DAS2SourceHandler extends DefaultHandler{
46 Das2Source currentSource;
51 public static final String LABELPROPERTY = "label";
53 public DAS2SourceHandler() {
56 sources = new ArrayList();
57 currentSource = new Das2SourceImpl();
58 coordinates = new ArrayList();
59 capabilities = new ArrayList();
60 labels = new ArrayList();
63 private void startSource (String uri, String name, String qName, Attributes atts){
65 String id = atts.getValue("uri");
66 String title = atts.getValue("title");
67 String doc_ref = atts.getValue("doc_href");
68 String description = atts.getValue("description");
71 currentSource.setId(id);
72 currentSource.setNickname(title);
73 currentSource.setHelperurl(doc_ref);
74 currentSource.setDescription(description);
78 private DasCoordinateSystem getCoordinateSystem(String uri, String name, String qname, Attributes atts){
79 // e.g. uri="http://das.sanger.ac.uk/dasregistry/coordsys/CS_LOCAL6"
80 // source="Protein Sequence" authority="UniProt" test_range="P06213" />
81 DasCoordinateSystem dcs = new DasCoordinateSystem();
82 String id = atts.getValue("uri");
85 String source = atts.getValue("source");
86 dcs.setCategory(source);
88 String authority = atts.getValue("authority");
89 dcs.setName(authority);
91 String test_range = atts.getValue("test_range");
92 dcs.setTestCode(test_range);
95 String taxidstr = atts.getValue("taxid");
96 int taxid = Integer.parseInt(taxidstr);
97 dcs.setNCBITaxId(taxid);
98 } catch (Exception e){}
100 String version = atts.getValue("version");
101 if ( version != null)
102 dcs.setVersion(version);
107 public void startElement (String uri, String name, String qName, Attributes atts){
108 //System.out.println("new element "+qName);
110 if (qName.equals("SOURCE")) {
111 //System.out.println("new Source " + atts.getValue(uri));
112 currentSource = new Das2SourceImpl();
113 coordinates = new ArrayList();
114 capabilities = new ArrayList();
116 startSource(uri,name, qName, atts);
118 } else if ( qName.equals("MAINTAINER")){
119 String email = atts.getValue("email");
120 currentSource.setAdminemail(email);
121 } else if ( qName.equals("COORDINATES")){
122 DasCoordinateSystem dcs = getCoordinateSystem(uri,name,qName,atts);
123 coordinates.add(dcs);
125 } else if ( qName.equals("CAPABILITY")){
126 Das2Capability cap = getCapability(uri,name,qName,atts);
127 capabilities.add(cap);
128 } else if (qName.equals("PROPERTY")) {
129 addProperty(uri,name,qName,atts);
133 private Das2Capability getCapability(String uri, String name, String qName, Attributes atts){
134 // e.g <CAPABILITY type="features" query_id="http://das.biopackages.net/das/genome/yeast/S228C/feature" />
135 Das2Capability cap = new Das2CapabilityImpl();
137 String type = atts.getValue("type");
138 cap.setCapability(type);
139 String query_uri = atts.getValue("query_uri");
140 cap.setQueryUri(query_uri);
145 private void addProperty(String uri, String name, String qName, Attributes atts){
146 String pname = atts.getValue("name");
147 String label = atts.getValue("value");
148 if ( pname.equals(LABELPROPERTY) )
152 public void startDocument(){
153 sources = new ArrayList();
154 coordinates = new ArrayList();
155 capabilities = new ArrayList();
158 public void endElement(String uri, String name, String qName) {
159 if ( qName.equals("SOURCE")) {
160 currentSource.setDas2Capabilities((Das2Capability[])capabilities.toArray(new Das2Capability[capabilities.size()]));
161 //System.out.println("got coordinates " + coordinates.size());
162 currentSource.setCoordinateSystem((DasCoordinateSystem[])coordinates.toArray(new DasCoordinateSystem[coordinates.size()]));
164 currentSource.setLabels((String[])labels.toArray(new String[labels.size()]));
167 //System.out.println("Das2SourceHandler endElement name " + name + " uri " + uri + " qName " + qName);
168 //System.out.println("Das2SourceHandler adding to source: " + currentSource.getId());
169 sources.add(currentSource);
170 currentSource = new Das2SourceImpl();
174 public DasSource[] getSources(){
175 //System.out.println("Das2SourceHandler: source size: " + sources.size());
176 return (DasSource[])sources.toArray(new DasSource[sources.size()]);