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 implements DasSourceReader
36 Exception loggedException;
38 public DasSourceReaderImpl()
41 loggedException = null;
43 // open the stream to a server and then parse the result ...
46 private InputStream open(URL url) throws java.io.IOException,
47 java.net.ConnectException
49 InputStream inStream = null;
51 HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
53 inStream = huc.getInputStream();
59 public DasSource[] readDasSource(URL url)
61 DasSource[] sources = new DasSource[0];
65 InputStream stream = open(url);
67 sources = readDasSource(stream);
77 * read a DAS2 sources response and return a list of DAS sources.
80 public DasSource[] readDasSource(InputStream stream)
83 DasSource[] sources = new DasSource[0];
87 SAXParserFactory spfactory = SAXParserFactory.newInstance();
89 spfactory.setValidating(false);
91 SAXParser saxParser = null;
95 saxParser = spfactory.newSAXParser();
96 } catch (ParserConfigurationException e)
102 String vali = System.getProperty("XMLVALIDATION");
104 boolean validation = false;
107 if (vali.equals("true"))
113 XMLReader xmlreader = saxParser.getXMLReader();
115 // XMLReader xmlreader = XMLReaderFactory.createXMLReader();
118 xmlreader.setFeature("http://xml.org/sax/features/validation",
120 } catch (SAXException e)
122 // logger.log(Level.FINE,"Cannot set validation " + validation);
129 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
131 } catch (SAXNotRecognizedException e)
134 // logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
138 DAS2SourceHandler cont_handle = new DAS2SourceHandler();
140 xmlreader.setContentHandler(cont_handle);
141 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
142 InputSource insource = new InputSource();
143 insource.setByteStream(stream);
145 xmlreader.parse(insource);
146 sources = cont_handle.getSources();
148 } catch (Exception e)
156 public Exception getLoggedException()
158 return loggedException;
161 public static void main(String[] args)
163 String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
164 DasSourceReaderImpl reader = new DasSourceReaderImpl();
167 URL u = new URL(url);
168 DasSource[] sources = reader.readDasSource(u);
169 for (int i = 0; i < sources.length; i++)
171 DasSource ds = sources[i];
172 System.out.println(ds.toString());
175 } catch (Exception e)