176356cb6ca94f4aa247352114e340e97bd2407d
[JavaForFun] /
1 package name.gumartinm.weather.information.parser;
2
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;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20
21 public class JPOSWeatherParser implements IJPOSParser {
22
23     @Override
24     public Current retrieveCurrenFromJPOS(final String jsonData)
25             throws JsonParseException, IOException {
26         final JsonFactory f = new JsonFactory();
27
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());
35         currentWeatherData
36         .setWeather(new ArrayList<name.gumartinm.weather.information.model.currentweather.Weather>());
37         currentWeatherData.setWind(new Wind());
38         final JsonParser jParser = f.createParser(jsonData);
39
40         this.getCurrentWeatherData(currentWeatherData, jParser);
41
42         return currentWeatherData;
43     }
44
45     @Override
46     public Forecast retrieveForecastFromJPOS(final String jsonData)
47             throws JsonParseException, IOException {
48         final JsonFactory f = new JsonFactory();
49
50         final Forecast forecastWeatherData = new Forecast();
51         forecastWeatherData
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);
57
58         this.getForecastWeatherData(forecastWeatherData, jParser);
59
60         return forecastWeatherData;
61     }
62
63     private void getCurrentWeatherData(final Current currentWeatherData,
64             final JsonParser jParser) throws JsonParseException, IOException {
65         if (jParser.nextToken() == JsonToken.START_OBJECT) {
66
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);
72                 }
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);
78                         }
79                         tokenNext = jParser.nextToken();
80                     }
81                 }
82                 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
83                         || (nextToken == JsonToken.VALUE_STRING)) {
84                     this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
85                 }
86             }
87         }
88     }
89
90     private void getCurrentWeatherDataObjects(final Current currentWeatherData,
91             final JsonParser jParser, final String fieldname) throws JsonParseException,
92             IOException {
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());
99                 }
100                 if ("lat".equals(namefield)) {
101                     currentWeatherData.getCoord().setLat(jParser.getDoubleValue());
102                 }
103             }
104         }
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());
111                 }
112                 if ("country".equals(namefield)) {
113                     currentWeatherData.getSys().setCountry(jParser.getValueAsString());
114                 }
115                 if ("sunrise".equals(namefield)) {
116                     currentWeatherData.getSys().setSunrise(jParser.getValueAsLong());
117                 }
118                 if ("sunset".equals(namefield)) {
119                     currentWeatherData.getSys().setSunset(jParser.getValueAsLong());
120                 }
121             }
122         }
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());
131                 }
132                 if ("main".equals(namefield)) {
133                     weather.setMain(jParser.getText());
134                 }
135                 if ("description".equals(namefield)) {
136                     weather.setDescription(jParser.getText());
137                 }
138                 if ("icon".equals(namefield)) {
139                     weather.setIcon(jParser.getText());
140                 }
141
142             }
143         }
144         if ("base".equals(fieldname)) {
145             currentWeatherData.setBase(jParser.getText());
146         }
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());
153                 }
154                 if ("temp_min".equals(namefield)) {
155                     currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue());
156                 }
157                 if ("temp_max".equals(namefield)) {
158                     currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue());
159                 }
160                 if ("pressure".equals(namefield)) {
161                     currentWeatherData.getMain().setPressure(jParser.getDoubleValue());
162                 }
163                 if ("sea_level".equals(namefield)) {
164                     currentWeatherData.getMain().setSea_level(jParser.getDoubleValue());
165                 }
166                 if ("grnd_level".equals(namefield)) {
167                     currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue());
168                 }
169                 if ("humidity".equals(namefield)) {
170                     currentWeatherData.getMain().setHumidity(jParser.getDoubleValue());
171                 }
172             }
173         }
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());
180                 }
181                 if ("deg".equals(namefield)) {
182                     currentWeatherData.getWind().setDeg(jParser.getDoubleValue());
183                 }
184             }
185         }
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());
192                 }
193             }
194         }
195         if ("dt".equals(fieldname)) {
196             currentWeatherData.setDt(jParser.getLongValue());
197         }
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());
204                 }
205             }
206         }
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());
213                 }
214             }
215         }
216         if ("id".equals(fieldname)) {
217             currentWeatherData.setId(jParser.getLongValue());
218         }
219         if ("name".equals(fieldname)) {
220             currentWeatherData.setName(jParser.getText());
221         }
222         if ("cod".equals(fieldname)) {
223             currentWeatherData.setCod(jParser.getIntValue());
224         }
225     }
226
227     private void getForecastWeatherData(final Forecast forecastWeatherData,
228             final JsonParser jParser) throws JsonParseException, IOException {
229         if (jParser.nextToken() == JsonToken.START_OBJECT) {
230
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);
236                 }
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);
242                         }
243                         tokenNext = jParser.nextToken();
244                     }
245                 }
246                 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
247                         || (nextToken == JsonToken.VALUE_STRING)) {
248                     this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
249                 }
250             }
251         }
252     }
253
254     private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
255             final JsonParser jParser, final String fieldname) throws JsonParseException,
256             IOException {
257
258         if ("cod".equals(fieldname)) {
259             final String stringCod = jParser.getText();
260             forecastWeatherData.setCod(Long.valueOf(stringCod));
261         }
262         if ("message".equals(fieldname)) {
263             forecastWeatherData.setMessage(jParser.getDoubleValue());
264         }
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
269                 // value
270                 if ("id".equals(namefield)) {
271                     forecastWeatherData.getCity().setId(jParser.getLongValue());
272                 }
273                 if ("name".equals(namefield)) {
274                     forecastWeatherData.getCity().setName(jParser.getText());
275                 }
276                 if ("coord".equals(namefield)) {
277                     if (nextToken == JsonToken.START_OBJECT) {
278                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
279                     }
280                 }
281                 if ("country".equals(namefield)) {
282                     forecastWeatherData.getCity().setCountry(jParser.getText());
283                 }
284                 if ("population".equals(namefield)) {
285                     forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
286                 }
287             }
288         }
289         if ("cnt".equals(fieldname)) {
290             forecastWeatherData.setCnt(jParser.getIntValue());
291         }
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());
298                 }
299                 if ("lat".equals(namefield)) {
300                     forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
301                 }
302             }
303         }
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
312                 // value
313                 if ("dt".equals(namefield)) {
314                     list.setDt(jParser.getLongValue());
315                 }
316                 if ("temp".equals(namefield)) {
317                     if (nextToken == JsonToken.START_OBJECT) {
318                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
319                     }
320                 }
321                 if ("pressure".equals(namefield)) {
322                     list.setPressure(jParser.getDoubleValue());
323                 }
324                 if ("humidity".equals(namefield)) {
325                     list.setHumidity(jParser.getDoubleValue());
326                 }
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,
333                                         namefield);
334                             }
335                             tokenNext = jParser.nextToken();
336                         }
337                     }
338                 }
339                 if ("speed".equals(namefield)) {
340                     list.setSpeed(jParser.getDoubleValue());
341                 }
342                 if ("deg".equals(namefield)) {
343                     list.setDeg(jParser.getDoubleValue());
344                 }
345                 if ("clouds".equals(namefield)) {
346                     list.setClouds(jParser.getDoubleValue());
347                 }
348                 if ("rain".equals(namefield)) {
349                     list.setRain(jParser.getDoubleValue());
350                 }
351             }
352         }
353         if ("temp".equals(fieldname)) {
354             final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData
355                     .getList().get(
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());
362                 }
363                 if ("min".equals(namefield)) {
364                     list.getTemp().setMin(jParser.getDoubleValue());
365                 }
366                 if ("max".equals(namefield)) {
367                     list.getTemp().setMax(jParser.getDoubleValue());
368                 }
369                 if ("night".equals(namefield)) {
370                     list.getTemp().setNight(jParser.getDoubleValue());
371                 }
372                 if ("eve".equals(namefield)) {
373                     list.getTemp().setEve(jParser.getDoubleValue());
374                 }
375                 if ("morn".equals(namefield)) {
376                     list.getTemp().setMorn(jParser.getDoubleValue());
377                 }
378             }
379         }
380         if ("weather".equals(fieldname)) {
381             final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData
382                     .getList().get(
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
388
389                 if ("id".equals(namefield)) {
390                     weather.setId(jParser.getIntValue());
391                 }
392                 if ("main".equals(namefield)) {
393                     weather.setMain(jParser.getText());
394                 }
395                 if ("description".equals(namefield)) {
396                     weather.setDescription(jParser.getText());
397                 }
398                 if ("icon".equals(namefield)) {
399                     weather.setIcon(jParser.getText());
400                 }
401             }
402             list.getWeather().add(weather);
403         }
404     }
405 }