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);
70 System.err.println("Exception for url:"+url);
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 = SAXParserFactory.newInstance();
90 spfactory.setValidating(false);
92 SAXParser saxParser = null;
96 saxParser = spfactory.newSAXParser();
97 } catch (ParserConfigurationException e)
103 String vali = System.getProperty("XMLVALIDATION");
105 boolean validation = false;
108 if (vali.equals("true"))
114 XMLReader xmlreader = saxParser.getXMLReader();
116 // XMLReader xmlreader = XMLReaderFactory.createXMLReader();
119 xmlreader.setFeature("http://xml.org/sax/features/validation",
121 } catch (SAXException e)
123 // logger.log(Level.FINE,"Cannot set validation " + validation);
130 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
132 } catch (SAXNotRecognizedException e)
135 // logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
139 DAS2SourceHandler cont_handle = new DAS2SourceHandler();
141 xmlreader.setContentHandler(cont_handle);
142 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
143 InputSource insource = new InputSource();
144 insource.setByteStream(stream);
146 xmlreader.parse(insource);
147 sources = cont_handle.getSources();
149 } catch (Exception e)
157 public Exception getLoggedException()
159 return loggedException;
162 public static void main(String[] args)
164 String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
165 DasSourceReaderImpl reader = new DasSourceReaderImpl();
168 URL u = new URL(url);
169 DasSource[] sources = reader.readDasSource(u);
170 for (int i = 0; i < sources.length; i++)
172 DasSource ds = sources[i];
173 System.out.println(ds.toString());
176 } catch (Exception e)