2 * BioJava development code
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,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on Feb 24, 2006
23 package org.biojava.dasobert.das2.io;
25 import java.io.InputStream;
26 import java.net.HttpURLConnection;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
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;
41 public class DasSourceReaderImpl implements DasSourceReader {
43 Exception loggedException;
45 public DasSourceReaderImpl() {
47 loggedException = null;
49 // open the stream to a server and then parse the result ...
52 private InputStream open(URL url)
53 throws java.io.IOException, java.net.ConnectException
55 InputStream inStream = null;
58 HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
60 inStream = huc.getInputStream();
67 public DasSource[] readDasSource(URL url){
68 DasSource[] sources = new DasSource[0];
71 InputStream stream = open(url);
73 sources = readDasSource(stream);
74 } catch (Exception e){
81 /** read a DAS2 sources response and return a list of DAS sources.
84 public DasSource[] readDasSource(InputStream stream) {
86 DasSource[] sources = new DasSource[0];
89 SAXParserFactory spfactory =
90 SAXParserFactory.newInstance();
92 spfactory.setValidating(false);
94 SAXParser saxParser = null ;
98 spfactory.newSAXParser();
99 } catch (ParserConfigurationException e) {
104 String vali = System.getProperty("XMLVALIDATION");
106 boolean validation = false ;
108 if ( vali.equals("true") )
112 XMLReader xmlreader = saxParser.getXMLReader();
114 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
116 xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
117 } catch (SAXException e) {
118 //logger.log(Level.FINE,"Cannot set validation " + validation);
122 xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
123 } catch (SAXNotRecognizedException e){
125 //logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
129 DAS2SourceHandler cont_handle = new DAS2SourceHandler() ;
131 xmlreader.setContentHandler(cont_handle);
132 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
133 InputSource insource = new InputSource() ;
134 insource.setByteStream(stream);
137 xmlreader.parse(insource);
138 sources = cont_handle.getSources();
142 } catch (Exception e) {
149 public Exception getLoggedException(){
150 return loggedException;
153 public static void main (String[] args){
154 String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
155 DasSourceReaderImpl reader = new DasSourceReaderImpl();
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());
164 } catch (Exception e){