| 1 31 package org.blojsom.plugin.weather.beans; 32 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.w3c.dom.DOMException ; 37 38 import java.text.MessageFormat ; 39 40 48 public class NWSInformation implements WeatherInformation { 49 50 public static final String NWS_URL_FORMAT = "http://www.nws.noaa.gov/data/current_obs/{0}.xml"; 51 52 public static final String TAG_CREDIT = "credit"; 53 public static final String TAG_CREDIT_URL = "credit_URL"; 54 public static final String TAG_LOCATION = "location"; 55 public static final String TAG_OBSERVATION = "observation_time"; 56 public static final String TAG_WEATHER = "weather"; 57 public static final String TAG_TEMP_STRING = "temperature_string"; 58 public static final String TAG_TEMP_F = "temp_f"; 59 public static final String TAG_TEMP_C = "temp_c"; 60 public static final String TAG_HUMIDITY = "relative_humidity"; 61 public static final String TAG_WIND_STRING = "wind_string"; 62 public static final String TAG_WIND_DIRECTION = "wind_dir"; 63 public static final String TAG_WIND_DEGREES = "wind_degrees"; 64 public static final String TAG_WIND_MPH = "wind_mph"; 65 public static final String TAG_WIND_GUST_MPH = "wind_gust_mph"; 66 public static final String TAG_STATION = "station_id"; 67 public static final String TAG_VISIBILITY = "visibility_mi"; 68 public static final String TAG_HISTORY = "two_day_history_url"; 69 70 private String _temperatureF = "-0 F"; 71 private String _temperatureC = "-0 C"; 72 private String _stationCode = ""; 73 private String _location = "a"; 74 private String _visibility = ""; 75 private String _wind = ""; 76 private String _history = "#"; 77 private Document _document; 78 79 84 public NWSInformation(String stationCode) { 85 _stationCode = stationCode; 86 } 87 88 93 public void parseDocument(Document 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 111 private String getValueOfTag(String tag) { 112 String result = null; 113 try { 114 NodeList nodeList = _document.getElementsByTagName(tag); 115 if (nodeList != null) { 116 Node tempNode = nodeList.item(0); 117 Node value = tempNode.getFirstChild(); 118 if (value != null) { 119 result = value.getNodeValue(); 120 } 121 } 122 } catch (DOMException e) { 123 result = null; 124 } 125 126 return result; 127 } 128 129 134 public String getLocation() { 135 return _location; 136 } 137 138 143 public String getStationCode() { 144 return _stationCode; 145 } 146 147 152 public String getFahrenheit() { 153 return _temperatureF + " F"; 154 } 155 156 161 public String getCelcius() { 162 return _temperatureC + " C"; 163 } 164 165 170 public String getVisibility() { 171 return _visibility; 172 } 173 174 179 public String getWind() { 180 return _wind; 181 } 182 183 188 public String getHistoryUrl() { 189 return _history; 190 } 191 192 197 public String getProviderUrl() { 198 return MessageFormat.format(NWS_URL_FORMAT, new Object []{_stationCode}); 199 } 200 201 208 public String getValueForTag(String tag) { 209 return getValueOfTag(tag); 210 } 211 } 212 | Popular Tags |