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;
50 public DAS2SourceHandler() {
53 sources = new ArrayList();
54 currentSource = new Das2SourceImpl();
55 coordinates = new ArrayList();
56 capabilities = new ArrayList();
59 private void startSource (String uri, String name, String qName, Attributes atts){
61 String id = atts.getValue("uri");
62 String title = atts.getValue("title");
63 String doc_ref = atts.getValue("doc_href");
64 String description = atts.getValue("description");
67 currentSource.setId(id);
68 currentSource.setNickname(title);
69 currentSource.setHelperurl(doc_ref);
70 currentSource.setDescription(description);
74 private DasCoordinateSystem getCoordinateSystem(String uri, String name, String qname, Attributes atts){
75 // e.g. uri="http://das.sanger.ac.uk/dasregistry/coordsys/CS_LOCAL6"
76 // source="Protein Sequence" authority="UniProt" test_range="P06213" />
77 DasCoordinateSystem dcs = new DasCoordinateSystem();
78 String id = atts.getValue("uri");
81 String source = atts.getValue("source");
82 dcs.setCategory(source);
84 String authority = atts.getValue("authority");
85 dcs.setName(authority);
87 String test_range = atts.getValue("test_range");
88 dcs.setTestCode(test_range);
91 String taxidstr = atts.getValue("taxid");
92 int taxid = Integer.parseInt(taxidstr);
93 dcs.setNCBITaxId(taxid);
94 } catch (Exception e){}
96 String version = atts.getValue("version");
98 dcs.setVersion(version);
103 public void startElement (String uri, String name, String qName, Attributes atts){
104 //System.out.println("new element "+qName);
106 if (qName.equals("SOURCE")) {
107 //System.out.println("new Source " + atts.getValue(uri));
108 currentSource = new Das2SourceImpl();
109 coordinates = new ArrayList();
110 capabilities = new ArrayList();
112 startSource(uri,name, qName, atts);
114 } else if ( qName.equals("MAINTAINER")){
115 String email = atts.getValue("email");
116 currentSource.setAdminemail(email);
117 } else if ( qName.equals("COORDINATES")){
118 DasCoordinateSystem dcs = getCoordinateSystem(uri,name,qName,atts);
119 coordinates.add(dcs);
121 } else if ( qName.equals("CAPABILITY")){
122 Das2Capability cap = getCapability(uri,name,qName,atts);
123 capabilities.add(cap);
126 //TODO: add support for "labels"
130 private Das2Capability getCapability(String uri, String name, String qName, Attributes atts){
131 // e.g <CAPABILITY type="features" query_id="http://das.biopackages.net/das/genome/yeast/S228C/feature" />
132 Das2Capability cap = new Das2CapabilityImpl();
134 String type = atts.getValue("type");
135 cap.setCapability(type);
136 String query_uri = atts.getValue("query_uri");
137 cap.setQueryUri(query_uri);
142 public void startDocument(){
143 sources = new ArrayList();
144 coordinates = new ArrayList();
145 capabilities = new ArrayList();
148 public void endElement(String uri, String name, String qName) {
149 if ( qName.equals("SOURCE")) {
150 currentSource.setDas2Capabilities((Das2Capability[])capabilities.toArray(new Das2Capability[capabilities.size()]));
151 //System.out.println("got coordinates " + coordinates.size());
152 currentSource.setCoordinateSystem((DasCoordinateSystem[])coordinates.toArray(new DasCoordinateSystem[coordinates.size()]));
153 //System.out.println("Das2SourceHandler endElement name " + name + " uri " + uri + " qName " + qName);
154 //System.out.println("Das2SourceHandler adding to source: " + currentSource.getId());
155 sources.add(currentSource);
156 currentSource = new Das2SourceImpl();
160 public DasSource[] getSources(){
161 //System.out.println("Das2SourceHandler: source size: " + sources.size());
162 return (DasSource[])sources.toArray(new DasSource[sources.size()]);