Formatting
[jalview.git] / src / org / biojava / dasobert / das2 / io / DasSourceReaderImpl.java
1 /*
2  *                  BioJava development code
3  *
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,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  *
20  * Created on Feb 24, 2006
21  *
22  */
23 package org.biojava.dasobert.das2.io;
24
25 import java.io.*;
26 import java.net.*;
27 import javax.xml.parsers.*;
28
29 import org.biojava.dasobert.das.*;
30 import org.biojava.dasobert.dasregistry.*;
31 import org.xml.sax.*;
32
33 public class DasSourceReaderImpl
34     implements DasSourceReader
35 {
36
37   Exception loggedException;
38
39   public DasSourceReaderImpl()
40   {
41     super();
42     loggedException = null;
43
44     // open the stream to a server and then parse the result ...
45   }
46
47   private InputStream open(URL url)
48       throws java.io.IOException, java.net.ConnectException
49   {
50     InputStream inStream = null;
51
52     HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
53
54     inStream = huc.getInputStream();
55
56     return inStream;
57
58   }
59
60   public DasSource[] readDasSource(URL url)
61   {
62     DasSource[] sources = new DasSource[0];
63
64     try
65     {
66       InputStream stream = open(url);
67
68       sources = readDasSource(stream);
69     }
70     catch (Exception e)
71     {
72       e.printStackTrace();
73       loggedException = e;
74     }
75     return sources;
76   }
77
78   /** read a DAS2 sources response and return a list of DAS sources.
79    *
80    */
81   public DasSource[] readDasSource(InputStream stream)
82   {
83
84     DasSource[] sources = new DasSource[0];
85
86     try
87     {
88       SAXParserFactory spfactory =
89           SAXParserFactory.newInstance();
90
91       spfactory.setValidating(false);
92
93       SAXParser saxParser = null;
94
95       try
96       {
97         saxParser =
98             spfactory.newSAXParser();
99       }
100       catch (ParserConfigurationException e)
101       {
102         e.printStackTrace();
103         loggedException = e;
104       }
105
106       String vali = System.getProperty("XMLVALIDATION");
107
108       boolean validation = false;
109       if (vali != null)
110       {
111         if (vali.equals("true"))
112         {
113           validation = true;
114         }
115       }
116
117       XMLReader xmlreader = saxParser.getXMLReader();
118
119       //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
120       try
121       {
122         xmlreader.setFeature("http://xml.org/sax/features/validation",
123                              validation);
124       }
125       catch (SAXException e)
126       {
127         //logger.log(Level.FINE,"Cannot set validation " + validation);
128       }
129
130       try
131       {
132         xmlreader.setFeature(
133             "http://apache.org/xml/features/nonvalidating/load-external-dtd",
134             validation);
135       }
136       catch (SAXNotRecognizedException e)
137       {
138         e.printStackTrace();
139         //logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
140
141       }
142
143       DAS2SourceHandler cont_handle = new DAS2SourceHandler();
144
145       xmlreader.setContentHandler(cont_handle);
146       xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
147       InputSource insource = new InputSource();
148       insource.setByteStream(stream);
149
150       xmlreader.parse(insource);
151       sources = cont_handle.getSources();
152
153     }
154     catch (Exception e)
155     {
156       e.printStackTrace();
157       loggedException = e;
158     }
159     return sources;
160   }
161
162   public Exception getLoggedException()
163   {
164     return loggedException;
165   }
166
167   public static void main(String[] args)
168   {
169     String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
170     DasSourceReaderImpl reader = new DasSourceReaderImpl();
171     try
172     {
173       URL u = new URL(url);
174       DasSource[] sources = reader.readDasSource(u);
175       for (int i = 0; i < sources.length; i++)
176       {
177         DasSource ds = sources[i];
178         System.out.println(ds.toString());
179       }
180
181     }
182     catch (Exception e)
183     {
184       e.printStackTrace();
185     }
186
187   }
188
189 }