KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > weather > beans > NWSInformation


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.beans;
32
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.w3c.dom.DOMException JavaDoc;
37
38 import java.text.MessageFormat JavaDoc;
39
40 /**
41  * NWSInformation
42  *
43  * @author David Czarnecki
44  * @author Mark Lussier
45  * @version $Id: NWSInformation.java,v 1.1 2006/03/26 16:27:30 czarneckid Exp $
46  * @since blojsom 3.0
47  */

48 public class NWSInformation implements WeatherInformation {
49
50     public static final String JavaDoc NWS_URL_FORMAT = "http://www.nws.noaa.gov/data/current_obs/{0}.xml";
51
52     public static final String JavaDoc TAG_CREDIT = "credit";
53     public static final String JavaDoc TAG_CREDIT_URL = "credit_URL";
54     public static final String JavaDoc TAG_LOCATION = "location";
55     public static final String JavaDoc TAG_OBSERVATION = "observation_time";
56     public static final String JavaDoc TAG_WEATHER = "weather";
57     public static final String JavaDoc TAG_TEMP_STRING = "temperature_string";
58     public static final String JavaDoc TAG_TEMP_F = "temp_f";
59     public static final String JavaDoc TAG_TEMP_C = "temp_c";
60     public static final String JavaDoc TAG_HUMIDITY = "relative_humidity";
61     public static final String JavaDoc TAG_WIND_STRING = "wind_string";
62     public static final String JavaDoc TAG_WIND_DIRECTION = "wind_dir";
63     public static final String JavaDoc TAG_WIND_DEGREES = "wind_degrees";
64     public static final String JavaDoc TAG_WIND_MPH = "wind_mph";
65     public static final String JavaDoc TAG_WIND_GUST_MPH = "wind_gust_mph";
66     public static final String JavaDoc TAG_STATION = "station_id";
67     public static final String JavaDoc TAG_VISIBILITY = "visibility_mi";
68     public static final String JavaDoc TAG_HISTORY = "two_day_history_url";
69
70     private String JavaDoc _temperatureF = "-0 F";
71     private String JavaDoc _temperatureC = "-0 C";
72     private String JavaDoc _stationCode = "";
73     private String JavaDoc _location = "a";
74     private String JavaDoc _visibility = "";
75     private String JavaDoc _wind = "";
76     private String JavaDoc _history = "#";
77     private Document JavaDoc _document;
78
79     /**
80      * Public Constructor
81      *
82      * @param stationCode The Provider Station Id
83      */

84     public NWSInformation(String JavaDoc stationCode) {
85         _stationCode = stationCode;
86     }
87
88     /**
89      * Parse an XML document containing weather related information
90      *
91      * @param document XML document with weather information
92      */

93     public void parseDocument(Document JavaDoc document) {
94         _document = document;
95
96         _temperatureC = getValueOfTag(TAG_TEMP_C);
97         _temperatureF = getValueOfTag(TAG_TEMP_F);
98         _stationCode = getValueOfTag(TAG_STATION);
99         _location = getValueOfTag(TAG_LOCATION);
100         _visibility = getValueOfTag(TAG_VISIBILITY);
101         _wind = getValueOfTag(TAG_WIND_STRING);
102         _history = getValueOfTag(TAG_HISTORY);
103     }
104
105     /**
106      * Get Contents of a Tag in the Weather Data
107      *
108      * @param tag The Tag Name
109      * @return Value of the Element as a String
110      */

111     private String JavaDoc getValueOfTag(String JavaDoc tag) {
112         String JavaDoc result = null;
113         try {
114             NodeList JavaDoc nodeList = _document.getElementsByTagName(tag);
115             if (nodeList != null) {
116                 Node JavaDoc tempNode = nodeList.item(0);
117                 Node JavaDoc value = tempNode.getFirstChild();
118                 if (value != null) {
119                     result = value.getNodeValue();
120                 }
121             }
122         } catch (DOMException JavaDoc e) {
123             result = null;
124         }
125
126         return result;
127     }
128
129     /**
130      * Get the Location of the Weather Station
131      *
132      * @return The Weather Station name as a String
133      */

134     public String JavaDoc getLocation() {
135         return _location;
136     }
137
138     /**
139      * Get the Station Id
140      *
141      * @return The Station Id as a String
142      */

143     public String JavaDoc getStationCode() {
144         return _stationCode;
145     }
146
147     /**
148      * Get the current temperature as Farenheit
149      *
150      * @return A String containing the current temperature in Farenheit
151      */

152     public String JavaDoc getFahrenheit() {
153         return _temperatureF + " F";
154     }
155
156     /**
157      * Get the current temperate as Celcius
158      *
159      * @return A String containing the current temperature as Celcius
160      */

161     public String JavaDoc getCelcius() {
162         return _temperatureC + " C";
163     }
164
165     /**
166      * Get the current Visibility
167      *
168      * @return The current visbility as a String
169      */

170     public String JavaDoc getVisibility() {
171         return _visibility;
172     }
173
174     /**
175      * Get the current Wind conditions
176      *
177      * @return The current wind conditions as a String
178      */

179     public String JavaDoc getWind() {
180         return _wind;
181     }
182
183     /**
184      * Get the URL containing a link to weather history information
185      *
186      * @return URL for weather history information
187      */

188     public String JavaDoc getHistoryUrl() {
189         return _history;
190     }
191
192     /**
193      * Gets the URL required to fetch this resource
194      *
195      * @return The resource location as a String
196      */

197     public String JavaDoc getProviderUrl() {
198         return MessageFormat.format(NWS_URL_FORMAT, new Object JavaDoc[]{_stationCode});
199     }
200
201     /**
202      * Get the value for a given tag from the parsed XML weather information
203      *
204      * @param tag Tag to retrieve
205      * @return Value of tag or <code>null</code> if the tag is not present
206      * @since blojsom 2.24
207      */

208     public String JavaDoc getValueForTag(String JavaDoc tag) {
209         return getValueOfTag(tag);
210     }
211 }
212
Popular Tags