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