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