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;
27 import javax.xml.parsers.*;
29 import org.biojava.dasobert.das.*;
30 import org.biojava.dasobert.dasregistry.*;
33 public class DasSourceReaderImpl
34 implements DasSourceReader
37 Exception loggedException;
39 public DasSourceReaderImpl()
42 loggedException = null;
44 // open the stream to a server and then parse the result ...
47 private InputStream open(URL url)
48 throws java.io.IOException, java.net.ConnectException
50 InputStream inStream = null;
52 HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
54 inStream = huc.getInputStream();
60 public DasSource[] readDasSource(URL url)
62 DasSource[] sources = new DasSource[0];
66 InputStream stream = open(url);
68 sources = readDasSource(stream);
78 /** read a DAS2 sources response and return a list of DAS sources.
81 public DasSource[] readDasSource(InputStream stream)
84 DasSource[] sources = new DasSource[0];
88 SAXParserFactory spfactory =
89 SAXParserFactory.newInstance();
91 spfactory.setValidating(false);
93 SAXParser saxParser = null;
98 spfactory.newSAXParser();
100 catch (ParserConfigurationException e)
106 String vali = System.getProperty("XMLVALIDATION");
108 boolean validation = false;
111 if (vali.equals("true"))
117 XMLReader xmlreader = saxParser.getXMLReader();
119 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
122 xmlreader.setFeature("http://xml.org/sax/features/validation",
125 catch (SAXException e)
127 //logger.log(Level.FINE,"Cannot set validation " + validation);
132 xmlreader.setFeature(
133 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
136 catch (SAXNotRecognizedException e)
139 //logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
143 DAS2SourceHandler cont_handle = new DAS2SourceHandler();
145 xmlreader.setContentHandler(cont_handle);
146 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
147 InputSource insource = new InputSource();
148 insource.setByteStream(stream);
150 xmlreader.parse(insource);
151 sources = cont_handle.getSources();
162 public Exception getLoggedException()
164 return loggedException;
167 public static void main(String[] args)
169 String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
170 DasSourceReaderImpl reader = new DasSourceReaderImpl();
173 URL u = new URL(url);
174 DasSource[] sources = reader.readDasSource(u);
175 for (int i = 0; i < sources.length; i++)
177 DasSource ds = sources[i];
178 System.out.println(ds.toString());