1 package name.gumartinm.weather.information.parser;
3 import com.fasterxml.jackson.core.JsonFactory;
4 import com.fasterxml.jackson.core.JsonParseException;
5 import com.fasterxml.jackson.core.JsonParser;
6 import com.fasterxml.jackson.core.JsonToken;
7 import name.gumartinm.weather.information.model.currentweather.Clouds;
8 import name.gumartinm.weather.information.model.currentweather.Current;
9 import name.gumartinm.weather.information.model.currentweather.Main;
10 import name.gumartinm.weather.information.model.currentweather.Rain;
11 import name.gumartinm.weather.information.model.currentweather.Snow;
12 import name.gumartinm.weather.information.model.currentweather.Sys;
13 import name.gumartinm.weather.information.model.currentweather.Wind;
14 import name.gumartinm.weather.information.model.forecastweather.City;
15 import name.gumartinm.weather.information.model.forecastweather.Forecast;
16 import name.gumartinm.weather.information.model.forecastweather.Temp;
18 import java.io.IOException;
19 import java.util.ArrayList;
21 public class JPOSWeatherParser implements IJPOSParser {
24 public Current retrieveCurrenFromJPOS(final String jsonData)
25 throws JsonParseException, IOException {
26 final JsonFactory f = new JsonFactory();
28 final Current currentWeatherData = new Current();
29 currentWeatherData.setClouds(new Clouds());
30 currentWeatherData.setCoord(new name.gumartinm.weather.information.model.currentweather.Coord());
31 currentWeatherData.setMain(new Main());
32 currentWeatherData.setRain(new Rain());
33 currentWeatherData.setSys(new Sys());
34 currentWeatherData.setSnow(new Snow());
36 .setWeather(new ArrayList<name.gumartinm.weather.information.model.currentweather.Weather>());
37 currentWeatherData.setWind(new Wind());
38 final JsonParser jParser = f.createParser(jsonData);
40 this.getCurrentWeatherData(currentWeatherData, jParser);
42 return currentWeatherData;
46 public Forecast retrieveForecastFromJPOS(final String jsonData)
47 throws JsonParseException, IOException {
48 final JsonFactory f = new JsonFactory();
50 final Forecast forecastWeatherData = new Forecast();
52 .setList(new ArrayList<name.gumartinm.weather.information.model.forecastweather.List>(15));
53 final City city = new City();
54 city.setCoord(new name.gumartinm.weather.information.model.forecastweather.Coord());
55 forecastWeatherData.setCity(city);
56 final JsonParser jParser = f.createParser(jsonData);
58 this.getForecastWeatherData(forecastWeatherData, jParser);
60 return forecastWeatherData;
63 private void getCurrentWeatherData(final Current currentWeatherData,
64 final JsonParser jParser) throws JsonParseException, IOException {
65 if (jParser.nextToken() == JsonToken.START_OBJECT) {
67 while (jParser.nextToken() != JsonToken.END_OBJECT) {
68 final String fieldname = jParser.getCurrentName();
69 final JsonToken nextToken = jParser.nextToken();
70 if (nextToken == JsonToken.START_OBJECT) {
71 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
73 if (nextToken == JsonToken.START_ARRAY) {
74 JsonToken tokenNext = jParser.nextToken();
75 while (tokenNext != JsonToken.END_ARRAY) {
76 if (tokenNext == JsonToken.START_OBJECT) {
77 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
79 tokenNext = jParser.nextToken();
82 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
83 || (nextToken == JsonToken.VALUE_STRING)) {
84 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
90 private void getCurrentWeatherDataObjects(final Current currentWeatherData,
91 final JsonParser jParser, final String fieldname) throws JsonParseException,
93 if ("coord".equals(fieldname)) {
94 while (jParser.nextToken() != JsonToken.END_OBJECT) {
95 final String namefield = jParser.getCurrentName();
96 jParser.nextToken(); // move to value
97 if ("lon".equals(namefield)) {
98 currentWeatherData.getCoord().setLon(jParser.getDoubleValue());
100 if ("lat".equals(namefield)) {
101 currentWeatherData.getCoord().setLat(jParser.getDoubleValue());
105 if ("sys".equals(fieldname)) {
106 while (jParser.nextToken() != JsonToken.END_OBJECT) {
107 final String namefield = jParser.getCurrentName();
108 jParser.nextToken(); // move to value
109 if ("message".equals(namefield)) {
110 currentWeatherData.getSys().setMessage(jParser.getDoubleValue());
112 if ("country".equals(namefield)) {
113 currentWeatherData.getSys().setCountry(jParser.getValueAsString());
115 if ("sunrise".equals(namefield)) {
116 currentWeatherData.getSys().setSunrise(jParser.getValueAsLong());
118 if ("sunset".equals(namefield)) {
119 currentWeatherData.getSys().setSunset(jParser.getValueAsLong());
123 if ("weather".equals(fieldname)) {
124 final name.gumartinm.weather.information.model.currentweather.Weather weather = new name.gumartinm.weather.information.model.currentweather.Weather();
125 currentWeatherData.getWeather().add(weather);
126 while (jParser.nextToken() != JsonToken.END_OBJECT) {
127 final String namefield = jParser.getCurrentName();
128 jParser.nextToken(); // move to value
129 if ("id".equals(namefield)) {
130 weather.setId(jParser.getIntValue());
132 if ("main".equals(namefield)) {
133 weather.setMain(jParser.getText());
135 if ("description".equals(namefield)) {
136 weather.setDescription(jParser.getText());
138 if ("icon".equals(namefield)) {
139 weather.setIcon(jParser.getText());
144 if ("base".equals(fieldname)) {
145 currentWeatherData.setBase(jParser.getText());
147 if ("main".equals(fieldname)) {
148 while (jParser.nextToken() != JsonToken.END_OBJECT) {
149 final String namefield = jParser.getCurrentName();
150 jParser.nextToken(); // move to value
151 if ("temp".equals(namefield)) {
152 currentWeatherData.getMain().setTemp(jParser.getDoubleValue());
154 if ("temp_min".equals(namefield)) {
155 currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue());
157 if ("temp_max".equals(namefield)) {
158 currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue());
160 if ("pressure".equals(namefield)) {
161 currentWeatherData.getMain().setPressure(jParser.getDoubleValue());
163 if ("sea_level".equals(namefield)) {
164 currentWeatherData.getMain().setSea_level(jParser.getDoubleValue());
166 if ("grnd_level".equals(namefield)) {
167 currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue());
169 if ("humidity".equals(namefield)) {
170 currentWeatherData.getMain().setHumidity(jParser.getDoubleValue());
174 if ("wind".equals(fieldname)) {
175 while (jParser.nextToken() != JsonToken.END_OBJECT) {
176 final String namefield = jParser.getCurrentName();
177 jParser.nextToken(); // move to value
178 if ("speed".equals(namefield)) {
179 currentWeatherData.getWind().setSpeed(jParser.getDoubleValue());
181 if ("deg".equals(namefield)) {
182 currentWeatherData.getWind().setDeg(jParser.getDoubleValue());
186 if ("clouds".equals(fieldname)) {
187 while (jParser.nextToken() != JsonToken.END_OBJECT) {
188 final String namefield = jParser.getCurrentName();
189 jParser.nextToken(); // move to value
190 if ("all".equals(namefield)) {
191 currentWeatherData.getClouds().setAll(jParser.getDoubleValue());
195 if ("dt".equals(fieldname)) {
196 currentWeatherData.setDt(jParser.getLongValue());
198 if ("rain".equals(fieldname)) {
199 while (jParser.nextToken() != JsonToken.END_OBJECT) {
200 final String namefield = jParser.getCurrentName();
201 jParser.nextToken(); // move to value
202 if ("3h".equals(namefield)) {
203 currentWeatherData.getRain().set3h(jParser.getDoubleValue());
207 if ("snow".equals(fieldname)) {
208 while (jParser.nextToken() != JsonToken.END_OBJECT) {
209 final String namefield = jParser.getCurrentName();
210 jParser.nextToken(); // move to value
211 if ("3h".equals(namefield)) {
212 currentWeatherData.getSnow().set3h(jParser.getDoubleValue());
216 if ("id".equals(fieldname)) {
217 currentWeatherData.setId(jParser.getLongValue());
219 if ("name".equals(fieldname)) {
220 currentWeatherData.setName(jParser.getText());
222 if ("cod".equals(fieldname)) {
223 currentWeatherData.setCod(jParser.getIntValue());
227 private void getForecastWeatherData(final Forecast forecastWeatherData,
228 final JsonParser jParser) throws JsonParseException, IOException {
229 if (jParser.nextToken() == JsonToken.START_OBJECT) {
231 while (jParser.nextToken() != JsonToken.END_OBJECT) {
232 final String fieldname = jParser.getCurrentName();
233 final JsonToken nextToken = jParser.nextToken();
234 if (nextToken == JsonToken.START_OBJECT) {
235 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
237 if (nextToken == JsonToken.START_ARRAY) {
238 JsonToken tokenNext = jParser.nextToken();
239 while (tokenNext != JsonToken.END_ARRAY) {
240 if (tokenNext == JsonToken.START_OBJECT) {
241 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
243 tokenNext = jParser.nextToken();
246 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
247 || (nextToken == JsonToken.VALUE_STRING)) {
248 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
254 private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
255 final JsonParser jParser, final String fieldname) throws JsonParseException,
258 if ("cod".equals(fieldname)) {
259 final String stringCod = jParser.getText();
260 forecastWeatherData.setCod(Long.valueOf(stringCod));
262 if ("message".equals(fieldname)) {
263 forecastWeatherData.setMessage(jParser.getDoubleValue());
265 if ("city".equals(fieldname)) {
266 while (jParser.nextToken() != JsonToken.END_OBJECT) {
267 final String namefield = jParser.getCurrentName();
268 final JsonToken nextToken = jParser.nextToken(); // move to
270 if ("id".equals(namefield)) {
271 forecastWeatherData.getCity().setId(jParser.getLongValue());
273 if ("name".equals(namefield)) {
274 forecastWeatherData.getCity().setName(jParser.getText());
276 if ("coord".equals(namefield)) {
277 if (nextToken == JsonToken.START_OBJECT) {
278 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
281 if ("country".equals(namefield)) {
282 forecastWeatherData.getCity().setCountry(jParser.getText());
284 if ("population".equals(namefield)) {
285 forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
289 if ("cnt".equals(fieldname)) {
290 forecastWeatherData.setCnt(jParser.getIntValue());
292 if ("coord".equals(fieldname)) {
293 while (jParser.nextToken() != JsonToken.END_OBJECT) {
294 final String namefield = jParser.getCurrentName();
295 jParser.nextToken(); // move to value
296 if ("lon".equals(namefield)) {
297 forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
299 if ("lat".equals(namefield)) {
300 forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
304 if ("list".equals(fieldname)) {
305 final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List();
306 list.setTemp(new Temp());
307 list.setWeather(new ArrayList<name.gumartinm.weather.information.model.forecastweather.Weather>());
308 forecastWeatherData.getList().add(list);
309 while (jParser.nextToken() != JsonToken.END_OBJECT) {
310 final String namefield = jParser.getCurrentName();
311 final JsonToken nextToken = jParser.nextToken(); // move to
313 if ("dt".equals(namefield)) {
314 list.setDt(jParser.getLongValue());
316 if ("temp".equals(namefield)) {
317 if (nextToken == JsonToken.START_OBJECT) {
318 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
321 if ("pressure".equals(namefield)) {
322 list.setPressure(jParser.getDoubleValue());
324 if ("humidity".equals(namefield)) {
325 list.setHumidity(jParser.getDoubleValue());
327 if ("weather".equals(namefield)) {
328 if (nextToken == JsonToken.START_ARRAY) {
329 JsonToken tokenNext = jParser.nextToken();
330 while (tokenNext != JsonToken.END_ARRAY) {
331 if (tokenNext == JsonToken.START_OBJECT) {
332 this.getForecastWeatherDataObjects(forecastWeatherData, jParser,
335 tokenNext = jParser.nextToken();
339 if ("speed".equals(namefield)) {
340 list.setSpeed(jParser.getDoubleValue());
342 if ("deg".equals(namefield)) {
343 list.setDeg(jParser.getDoubleValue());
345 if ("clouds".equals(namefield)) {
346 list.setClouds(jParser.getDoubleValue());
348 if ("rain".equals(namefield)) {
349 list.setRain(jParser.getDoubleValue());
353 if ("temp".equals(fieldname)) {
354 final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData
356 (forecastWeatherData.getList().size() - 1));
357 while (jParser.nextToken() != JsonToken.END_OBJECT) {
358 final String namefield = jParser.getCurrentName();
359 jParser.nextToken(); // move to value
360 if ("day".equals(namefield)) {
361 list.getTemp().setDay(jParser.getDoubleValue());
363 if ("min".equals(namefield)) {
364 list.getTemp().setMin(jParser.getDoubleValue());
366 if ("max".equals(namefield)) {
367 list.getTemp().setMax(jParser.getDoubleValue());
369 if ("night".equals(namefield)) {
370 list.getTemp().setNight(jParser.getDoubleValue());
372 if ("eve".equals(namefield)) {
373 list.getTemp().setEve(jParser.getDoubleValue());
375 if ("morn".equals(namefield)) {
376 list.getTemp().setMorn(jParser.getDoubleValue());
380 if ("weather".equals(fieldname)) {
381 final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData
383 (forecastWeatherData.getList().size() - 1));
384 final name.gumartinm.weather.information.model.forecastweather.Weather weather = new name.gumartinm.weather.information.model.forecastweather.Weather();
385 while (jParser.nextToken() != JsonToken.END_OBJECT) {
386 final String namefield = jParser.getCurrentName();
387 jParser.nextToken(); // move to value
389 if ("id".equals(namefield)) {
390 weather.setId(jParser.getIntValue());
392 if ("main".equals(namefield)) {
393 weather.setMain(jParser.getText());
395 if ("description".equals(namefield)) {
396 weather.setDescription(jParser.getText());
398 if ("icon".equals(namefield)) {
399 weather.setIcon(jParser.getText());
402 list.getWeather().add(weather);