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.das.AlignmentThread;
35 import org.biojava.dasobert.das.DAS_FeatureRetrieve;
36 import org.biojava.dasobert.dasregistry.DasSource;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.SAXNotRecognizedException;
40 import org.xml.sax.XMLReader;
42 public class DasSourceReaderImpl implements DasSourceReader {
44 Exception loggedException;
46 public DasSourceReaderImpl() {
48 loggedException = null;
50 // open the stream to a server and then parse the result ...
53 private InputStream open(URL url)
54 throws java.io.IOException, java.net.ConnectException
56 InputStream inStream = null;
59 HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
61 inStream = huc.getInputStream();
68 public DasSource[] readDasSource(URL url){
69 DasSource[] sources = new DasSource[0];
72 InputStream stream = open(url);
74 sources = readDasSource(stream);
75 } catch (Exception e){
82 /** read a DAS2 sources response and return a list of DAS sources.
85 public DasSource[] readDasSource(InputStream stream) {
87 DasSource[] sources = new DasSource[0];
90 SAXParserFactory spfactory =
91 SAXParserFactory.newInstance();
93 spfactory.setValidating(false);
95 SAXParser saxParser = null ;
99 spfactory.newSAXParser();
100 } catch (ParserConfigurationException e) {
105 String vali = System.getProperty("XMLVALIDATION");
107 boolean validation = false ;
109 if ( vali.equals("true") )
113 XMLReader xmlreader = saxParser.getXMLReader();
115 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
117 xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
118 } catch (SAXException e) {
119 //logger.log(Level.FINE,"Cannot set validation " + validation);
123 xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
124 } catch (SAXNotRecognizedException e){
126 //logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
130 DAS2SourceHandler cont_handle = new DAS2SourceHandler() ;
132 xmlreader.setContentHandler(cont_handle);
133 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
134 InputSource insource = new InputSource() ;
135 insource.setByteStream(stream);
138 xmlreader.parse(insource);
139 sources = cont_handle.getSources();
143 } catch (Exception e) {
150 public Exception getLoggedException(){
151 return loggedException;
154 public static void main (String[] args){
155 String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
156 DasSourceReaderImpl reader = new DasSourceReaderImpl();
158 URL u = new URL(url);
159 DasSource[] sources = reader.readDasSource(u);
160 for (int i=0; i< sources.length;i++){
161 DasSource ds = sources[i];
162 System.out.println(ds.toString());
165 } catch (Exception e){