merge from 2_4_Release branch
[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       e.printStackTrace();
71       loggedException = e;
72     }
73     return sources;
74   }
75
76   /**
77    * read a DAS2 sources response and return a list of DAS sources.
78    * 
79    */
80   public DasSource[] readDasSource(InputStream stream)
81   {
82
83     DasSource[] sources = new DasSource[0];
84
85     try
86     {
87       SAXParserFactory spfactory = SAXParserFactory.newInstance();
88
89       spfactory.setValidating(false);
90
91       SAXParser saxParser = null;
92
93       try
94       {
95         saxParser = spfactory.newSAXParser();
96       } catch (ParserConfigurationException e)
97       {
98         e.printStackTrace();
99         loggedException = e;
100       }
101
102       String vali = System.getProperty("XMLVALIDATION");
103
104       boolean validation = false;
105       if (vali != null)
106       {
107         if (vali.equals("true"))
108         {
109           validation = true;
110         }
111       }
112
113       XMLReader xmlreader = saxParser.getXMLReader();
114
115       // XMLReader xmlreader = XMLReaderFactory.createXMLReader();
116       try
117       {
118         xmlreader.setFeature("http://xml.org/sax/features/validation",
119                 validation);
120       } catch (SAXException e)
121       {
122         // logger.log(Level.FINE,"Cannot set validation " + validation);
123       }
124
125       try
126       {
127         xmlreader
128                 .setFeature(
129                         "http://apache.org/xml/features/nonvalidating/load-external-dtd",
130                         validation);
131       } catch (SAXNotRecognizedException e)
132       {
133         e.printStackTrace();
134         // logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
135
136       }
137
138       DAS2SourceHandler cont_handle = new DAS2SourceHandler();
139
140       xmlreader.setContentHandler(cont_handle);
141       xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
142       InputSource insource = new InputSource();
143       insource.setByteStream(stream);
144
145       xmlreader.parse(insource);
146       sources = cont_handle.getSources();
147
148     } catch (Exception e)
149     {
150       e.printStackTrace();
151       loggedException = e;
152     }
153     return sources;
154   }
155
156   public Exception getLoggedException()
157   {
158     return loggedException;
159   }
160
161   public static void main(String[] args)
162   {
163     String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
164     DasSourceReaderImpl reader = new DasSourceReaderImpl();
165     try
166     {
167       URL u = new URL(url);
168       DasSource[] sources = reader.readDasSource(u);
169       for (int i = 0; i < sources.length; i++)
170       {
171         DasSource ds = sources[i];
172         System.out.println(ds.toString());
173       }
174
175     } catch (Exception e)
176     {
177       e.printStackTrace();
178     }
179
180   }
181
182 }