Formatting
[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.*;
26
27 import org.biojava.dasobert.das2.*;
28 import org.biojava.dasobert.dasregistry.*;
29 import org.xml.sax.*;
30 import org.xml.sax.helpers.*;
31
32 /** a parser for the DAS2 sources response
33  *
34  * @author Andreas Prlic
35  * @since 6:53:45 PM
36  * @version %I% %G%
37  */
38 public class DAS2SourceHandler
39     extends DefaultHandler
40 {
41
42   List sources;
43   Das2Source currentSource;
44   List coordinates;
45   List capabilities;
46   List labels;
47
48   public static final String LABELPROPERTY = "label";
49
50   public DAS2SourceHandler()
51   {
52     super();
53
54     sources = new ArrayList();
55     currentSource = new Das2SourceImpl();
56     coordinates = new ArrayList();
57     capabilities = new ArrayList();
58     labels = new ArrayList();
59   }
60
61   private void startSource(String uri, String name, String qName,
62                            Attributes atts)
63   {
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     currentSource.setId(id);
71     currentSource.setNickname(title);
72     currentSource.setHelperurl(doc_ref);
73     currentSource.setDescription(description);
74
75   }
76
77   private DasCoordinateSystem getCoordinateSystem(String uri, String name,
78                                                   String qname, Attributes atts)
79   {
80     // e.g. uri="http://das.sanger.ac.uk/dasregistry/coordsys/CS_LOCAL6"
81     // source="Protein Sequence" authority="UniProt" test_range="P06213" />
82     DasCoordinateSystem dcs = new DasCoordinateSystem();
83     String id = atts.getValue("uri");
84     dcs.setUniqueId(id);
85
86     String source = atts.getValue("source");
87     dcs.setCategory(source);
88
89     String authority = atts.getValue("authority");
90     dcs.setName(authority);
91
92     String test_range = atts.getValue("test_range");
93     dcs.setTestCode(test_range);
94
95     try
96     {
97       String taxidstr = atts.getValue("taxid");
98       int taxid = Integer.parseInt(taxidstr);
99       dcs.setNCBITaxId(taxid);
100     }
101     catch (Exception e)
102     {}
103
104     String version = atts.getValue("version");
105     if (version != null)
106     {
107       dcs.setVersion(version);
108     }
109
110     return dcs;
111   }
112
113   public void startElement(String uri, String name, String qName,
114                            Attributes atts)
115   {
116     //System.out.println("new element "+qName);
117
118     if (qName.equals("SOURCE"))
119     {
120       //System.out.println("new Source " + atts.getValue(uri));
121       currentSource = new Das2SourceImpl();
122       coordinates = new ArrayList();
123       capabilities = new ArrayList();
124
125       startSource(uri, name, qName, atts);
126
127     }
128     else if (qName.equals("MAINTAINER"))
129     {
130       String email = atts.getValue("email");
131       currentSource.setAdminemail(email);
132     }
133     else if (qName.equals("COORDINATES"))
134     {
135       DasCoordinateSystem dcs = getCoordinateSystem(uri, name, qName, atts);
136       coordinates.add(dcs);
137
138     }
139     else if (qName.equals("CAPABILITY"))
140     {
141       Das2Capability cap = getCapability(uri, name, qName, atts);
142       capabilities.add(cap);
143     }
144     else if (qName.equals("PROPERTY"))
145     {
146       addProperty(uri, name, qName, atts);
147     }
148   }
149
150   private Das2Capability getCapability(String uri, String name, String qName,
151                                        Attributes atts)
152   {
153     // e.g <CAPABILITY type="features" query_id="http://das.biopackages.net/das/genome/yeast/S228C/feature" />
154     Das2Capability cap = new Das2CapabilityImpl();
155
156     String type = atts.getValue("type");
157     cap.setCapability(type);
158     String query_uri = atts.getValue("query_uri");
159     cap.setQueryUri(query_uri);
160     return cap;
161
162   }
163
164   private void addProperty(String uri, String name, String qName,
165                            Attributes atts)
166   {
167     String pname = atts.getValue("name");
168     String label = atts.getValue("value");
169     if (pname.equals(LABELPROPERTY))
170     {
171       labels.add(label);
172     }
173   }
174
175   public void startDocument()
176   {
177     sources = new ArrayList();
178     coordinates = new ArrayList();
179     capabilities = new ArrayList();
180   }
181
182   public void endElement(String uri, String name, String qName)
183   {
184     if (qName.equals("SOURCE"))
185     {
186       currentSource.setDas2Capabilities( (Das2Capability[]) capabilities.
187                                         toArray(new Das2Capability[capabilities.
188                                                 size()]));
189       //System.out.println("got coordinates " + coordinates.size());
190       currentSource.setCoordinateSystem( (DasCoordinateSystem[]) coordinates.
191                                         toArray(new DasCoordinateSystem[
192                                                 coordinates.size()]));
193
194       currentSource.setLabels( (String[]) labels.toArray(new String[labels.size()]));
195       labels.clear();
196
197       //System.out.println("Das2SourceHandler endElement name " + name + " uri " + uri + " qName " + qName);
198       //System.out.println("Das2SourceHandler adding to source: " + currentSource.getId());
199       sources.add(currentSource);
200       currentSource = new Das2SourceImpl();
201     }
202   }
203
204   public DasSource[] getSources()
205   {
206     //System.out.println("Das2SourceHandler: source size: " + sources.size());
207     return (DasSource[]) sources.toArray(new DasSource[sources.size()]);
208   }
209
210 }