Dasobert updates, labels and prefix changes
[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     List labels;
50
51     public static final String LABELPROPERTY = "label";
52
53     public DAS2SourceHandler() {
54         super();
55         
56         sources       = new ArrayList();
57         currentSource = new Das2SourceImpl();
58         coordinates   = new ArrayList();
59         capabilities  = new ArrayList();
60         labels        = new ArrayList();
61     }
62     
63     private void startSource (String uri, String name, String qName, Attributes atts){
64         
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");
69
70            
71         currentSource.setId(id);
72         currentSource.setNickname(title);
73         currentSource.setHelperurl(doc_ref);
74         currentSource.setDescription(description);
75         
76     }
77     
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");
83         dcs.setUniqueId(id);
84         
85         String source = atts.getValue("source");
86         dcs.setCategory(source);
87         
88         String authority = atts.getValue("authority");
89         dcs.setName(authority);
90         
91         String test_range = atts.getValue("test_range");
92         dcs.setTestCode(test_range);
93         
94         try {
95             String taxidstr = atts.getValue("taxid");
96             int taxid = Integer.parseInt(taxidstr);
97             dcs.setNCBITaxId(taxid);
98         } catch (Exception e){}
99         
100         String version = atts.getValue("version");
101         if ( version != null)
102             dcs.setVersion(version);
103         
104         return dcs;
105     }
106     
107     public void startElement (String uri, String name, String qName, Attributes atts){
108         //System.out.println("new element "+qName);
109         
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();
115             
116             startSource(uri,name, qName, atts);
117             
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);
124             
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);
130         }                
131     }
132     
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();
136         
137         String type = atts.getValue("type");
138         cap.setCapability(type);
139         String query_uri = atts.getValue("query_uri");
140         cap.setQueryUri(query_uri);
141         return cap;
142         
143     }
144     
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) )
149             labels.add(label);
150     }
151     
152     public void startDocument(){
153         sources = new ArrayList();
154         coordinates = new ArrayList();
155         capabilities = new ArrayList();
156     }
157     
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()]));
163
164             currentSource.setLabels((String[])labels.toArray(new String[labels.size()]));
165             labels.clear();
166
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();
171         }               
172     }
173     
174     public DasSource[] getSources(){    
175         //System.out.println("Das2SourceHandler: source size: " + sources.size());
176         return (DasSource[])sources.toArray(new DasSource[sources.size()]);
177     }
178     
179     
180
181 }