new files
[jalview.git] / src / org / biojava / dasobert / das2 / io / DAS2SourceHandler.java
1 /*
2  *                  BioJava development code
3  *
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,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  * 
20  * Created on Mar 15, 2006
21  *
22  */
23 package org.biojava.dasobert.das2.io;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
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;
36
37 /** a parser for the DAS2 sources response
38  * 
39  * @author Andreas Prlic
40  * @since 6:53:45 PM
41  * @version %I% %G%
42  */
43 public class DAS2SourceHandler extends DefaultHandler{
44
45     List sources;
46     Das2Source currentSource;
47     List coordinates;
48     List capabilities;
49     
50     public DAS2SourceHandler() {
51         super();
52         
53         sources = new ArrayList();
54         currentSource = new Das2SourceImpl();
55         coordinates = new ArrayList();
56         capabilities = new ArrayList();
57     }
58     
59     private void startSource (String uri, String name, String qName, Attributes atts){
60         
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");
65
66            
67         currentSource.setId(id);
68         currentSource.setNickname(title);
69         currentSource.setHelperurl(doc_ref);
70         currentSource.setDescription(description);
71         
72     }
73     
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");
79         dcs.setUniqueId(id);
80         
81         String source = atts.getValue("source");
82         dcs.setCategory(source);
83         
84         String authority = atts.getValue("authority");
85         dcs.setName(authority);
86         
87         String test_range = atts.getValue("test_range");
88         dcs.setTestCode(test_range);
89         
90         try {
91             String taxidstr = atts.getValue("taxid");
92             int taxid = Integer.parseInt(taxidstr);
93             dcs.setNCBITaxId(taxid);
94         } catch (Exception e){}
95         
96         String version = atts.getValue("version");
97         if ( version != null)
98             dcs.setVersion(version);
99         
100         return dcs;
101     }
102     
103     public void startElement (String uri, String name, String qName, Attributes atts){
104         //System.out.println("new element "+qName);
105         
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();
111             
112             startSource(uri,name, qName, atts);
113             
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);
120             
121         } else if ( qName.equals("CAPABILITY")){
122             Das2Capability cap = getCapability(uri,name,qName,atts);
123             capabilities.add(cap);
124         }
125         
126         //TODO: add support for "labels"
127         
128     }
129     
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();
133         
134         String type = atts.getValue("type");
135         cap.setCapability(type);
136         String query_uri = atts.getValue("query_uri");
137         cap.setQueryUri(query_uri);
138         return cap;
139         
140     }
141     
142     public void startDocument(){
143         sources = new ArrayList();
144         coordinates = new ArrayList();
145         capabilities = new ArrayList();
146     }
147     
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();
157         }
158     }
159     
160     public DasSource[] getSources(){    
161         //System.out.println("Das2SourceHandler: source size: " + sources.size());
162         return (DasSource[])sources.toArray(new DasSource[sources.size()]);
163     }
164     
165     
166
167 }