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