KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > weather > WeatherFetcher


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.weather;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.plugin.weather.beans.WeatherInformation;
36 import org.blojsom.util.BlojsomConstants;
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import javax.xml.parsers.DocumentBuilder JavaDoc;
42 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
43 import javax.xml.parsers.ParserConfigurationException JavaDoc;
44 import java.io.*;
45 import java.net.HttpURLConnection JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.net.URLConnection JavaDoc;
48 import java.util.zip.GZIPInputStream JavaDoc;
49
50 /**
51  * WeatherFetcher
52  *
53  * @author David Czarnecki
54  * @author Mark Lussier
55  * @version $Id: WeatherFetcher.java,v 1.2 2006/03/26 18:45:32 czarneckid Exp $
56  * @since blojsom 3.0
57  */

58 public class WeatherFetcher {
59
60     private Log _logger = LogFactory.getLog(WeatherFetcher.class);
61
62     private DocumentBuilder JavaDoc _documentBuilder;
63
64     /**
65      * Construct a new instance of the <code>WeatherFetcher</code> class
66      */

67     public WeatherFetcher() {
68         DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
69         documentBuilderFactory.setValidating(false);
70         documentBuilderFactory.setIgnoringElementContentWhitespace(true);
71         documentBuilderFactory.setIgnoringComments(true);
72         documentBuilderFactory.setCoalescing(true);
73         documentBuilderFactory.setNamespaceAware(false);
74
75         try {
76             _documentBuilder = documentBuilderFactory.newDocumentBuilder();
77         } catch (ParserConfigurationException JavaDoc e) {
78             if (_logger.isErrorEnabled()) {
79                 _logger.error(e);
80             }
81         }
82     }
83
84     /**
85      * Retrieve the {@link WeatherInformation} from a site containing an XML feed of weather information
86      *
87      * @param provider {@link WeatherInformation}
88      * @return {@link WeatherInformation} populated with data
89      * @throws IllegalArgumentException If there is an error parsing weather information
90      * @throws IOException If there is an error parsing weather information
91      */

92     public WeatherInformation retrieveForecast(WeatherInformation provider) throws IllegalArgumentException JavaDoc, IOException {
93         URL JavaDoc forecastUrl = new URL JavaDoc(provider.getProviderUrl());
94         URLConnection JavaDoc connection = forecastUrl.openConnection();
95
96         if (!(connection instanceof HttpURLConnection JavaDoc)) {
97             throw new IllegalArgumentException JavaDoc(forecastUrl.toExternalForm() + " is not a valid HTTP Url");
98         }
99
100         HttpURLConnection JavaDoc httpConnection = (HttpURLConnection JavaDoc) connection;
101         httpConnection.setRequestProperty("Accept-Encoding", WeatherConstants.GZIP);
102         httpConnection.setRequestProperty("User-Agent", WeatherConstants.BLOJSOM_WEATHER_USER_AGENT);
103
104         httpConnection.connect();
105         InputStream JavaDoc is = connection.getInputStream();
106         BufferedInputStream bis;
107
108         if (WeatherConstants.GZIP.equalsIgnoreCase(httpConnection.getContentEncoding())) {
109             bis = new BufferedInputStream(new GZIPInputStream JavaDoc(is));
110         } else {
111             bis = new BufferedInputStream(is);
112         }
113
114         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
115         BufferedReader br = new BufferedReader(new InputStreamReader(bis));
116
117         String JavaDoc input;
118         while ((input = br.readLine()) != null) {
119             buffer.append(input).append(BlojsomConstants.LINE_SEPARATOR);
120         }
121
122         try {
123             Document JavaDoc document = _documentBuilder.parse(new InputSource JavaDoc(new StringReader(buffer.toString())));
124             provider.parseDocument(document);
125         } catch (SAXException JavaDoc e) {
126             if (_logger.isErrorEnabled()) {
127                 _logger.error(e);
128             }
129         }
130
131         bis.close();
132
133         return provider;
134     }
135 }
136
Popular Tags