(JAL-1016) noted position where race condition occurs
[jalview.git] / src / org / biojava / dasobert / das2 / io / DasSourceReaderImpl.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 Feb 24, 2006
21  *
22  */
23 package org.biojava.dasobert.das2.io;
24
25 import java.io.*;
26 import java.net.*;
27 import javax.xml.parsers.*;
28
29 import org.biojava.dasobert.das.*;
30 import org.biojava.dasobert.dasregistry.*;
31 import org.xml.sax.*;
32
33 public class DasSourceReaderImpl implements DasSourceReader
34 {
35
36   Exception loggedException;
37
38   public DasSourceReaderImpl()
39   {
40     super();
41     loggedException = null;
42
43     // open the stream to a server and then parse the result ...
44   }
45
46   private InputStream open(URL url) throws java.io.IOException,
47           java.net.ConnectException
48   {
49     InputStream inStream = null;
50
51     HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
52
53     inStream = huc.getInputStream();
54
55     return inStream;
56
57   }
58
59   public DasSource[] readDasSource(URL url)
60   {
61     DasSource[] sources = new DasSource[0];
62
63     try
64     {
65       InputStream stream = open(url);
66
67       sources = readDasSource(stream);
68     } catch (Exception e)
69     {
70       System.err.println("Exception for url:"+url);
71       e.printStackTrace();
72       loggedException = e;
73     }
74     return sources;
75   }
76
77   /**
78    * read a DAS2 sources response and return a list of DAS sources.
79    * 
80    */
81   public DasSource[] readDasSource(InputStream stream)
82   {
83
84     DasSource[] sources = new DasSource[0];
85
86     try
87     {
88       SAXParserFactory spfactory = SAXParserFactory.newInstance();
89
90       spfactory.setValidating(false);
91
92       SAXParser saxParser = null;
93
94       try
95       {
96         saxParser = spfactory.newSAXParser();
97       } catch (ParserConfigurationException e)
98       {
99         e.printStackTrace();
100         loggedException = e;
101       }
102
103       String vali = System.getProperty("XMLVALIDATION");
104
105       boolean validation = false;
106       if (vali != null)
107       {
108         if (vali.equals("true"))
109         {
110           validation = true;
111         }
112       }
113
114       XMLReader xmlreader = saxParser.getXMLReader();
115
116       // XMLReader xmlreader = XMLReaderFactory.createXMLReader();
117       try
118       {
119         xmlreader.setFeature("http://xml.org/sax/features/validation",
120                 validation);
121       } catch (SAXException e)
122       {
123         // logger.log(Level.FINE,"Cannot set validation " + validation);
124       }
125
126       try
127       {
128         xmlreader
129                 .setFeature(
130                         "http://apache.org/xml/features/nonvalidating/load-external-dtd",
131                         validation);
132       } catch (SAXNotRecognizedException e)
133       {
134         e.printStackTrace();
135         // logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
136
137       }
138
139       DAS2SourceHandler cont_handle = new DAS2SourceHandler();
140
141       xmlreader.setContentHandler(cont_handle);
142       xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
143       InputSource insource = new InputSource();
144       insource.setByteStream(stream);
145
146       xmlreader.parse(insource);
147       sources = cont_handle.getSources();
148
149     } catch (Exception e)
150     {
151       e.printStackTrace();
152       loggedException = e;
153     }
154     return sources;
155   }
156
157   public Exception getLoggedException()
158   {
159     return loggedException;
160   }
161
162   public static void main(String[] args)
163   {
164     String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
165     DasSourceReaderImpl reader = new DasSourceReaderImpl();
166     try
167     {
168       URL u = new URL(url);
169       DasSource[] sources = reader.readDasSource(u);
170       for (int i = 0; i < sources.length; i++)
171       {
172         DasSource ds = sources[i];
173         System.out.println(ds.toString());
174       }
175
176     } catch (Exception e)
177     {
178       e.printStackTrace();
179     }
180
181   }
182
183 }