7dd4d468c63de5405451354bfd4153840d2b8972
[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
8 import java.io.IOException;
9 import java.util.ArrayList;
10
11 import name.gumartinm.weather.information.model.forecastweather.City;
12 import name.gumartinm.weather.information.model.forecastweather.Coord;
13 import name.gumartinm.weather.information.model.forecastweather.Forecast;
14 import name.gumartinm.weather.information.model.forecastweather.List;
15 import name.gumartinm.weather.information.model.forecastweather.Temp;
16 import name.gumartinm.weather.information.model.forecastweather.Weather;
17
18 public class JPOSForecastParser {
19
20     public Forecast retrieveForecastFromJPOS(final String jsonData)
21             throws JsonParseException, IOException {
22         final JsonFactory f = new JsonFactory();
23
24         final Forecast forecastWeatherData = new Forecast();
25         forecastWeatherData
26                 .setList(new ArrayList<List>(15));
27         final City city = new City();
28         city.setCoord(new Coord());
29         forecastWeatherData.setCity(city);
30         final JsonParser jParser = f.createParser(jsonData);
31
32         this.getForecastWeatherData(forecastWeatherData, jParser);
33
34         return forecastWeatherData;
35     }
36
37     private void getForecastWeatherData(final Forecast forecastWeatherData,
38                                         final JsonParser jParser) throws JsonParseException, IOException {
39         if (jParser.nextToken() == JsonToken.START_OBJECT) {
40
41             while (jParser.nextToken() != JsonToken.END_OBJECT) {
42                 final String fieldname = jParser.getCurrentName();
43                 final JsonToken nextToken = jParser.nextToken();
44                 if (nextToken == JsonToken.START_OBJECT) {
45                     this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
46                 }
47                 if (nextToken == JsonToken.START_ARRAY) {
48                     JsonToken tokenNext = jParser.nextToken();
49                     while (tokenNext != JsonToken.END_ARRAY) {
50                         if (tokenNext == JsonToken.START_OBJECT) {
51                             this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
52                         }
53                         tokenNext = jParser.nextToken();
54                     }
55                 }
56                 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
57                         || (nextToken == JsonToken.VALUE_STRING)) {
58                     this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
59                 }
60             }
61         }
62     }
63
64     private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
65                                                final JsonParser jParser, final String fieldname) throws JsonParseException,
66             IOException {
67
68         if ("cod".equals(fieldname)) {
69             final String stringCod = jParser.getText();
70             forecastWeatherData.setCod(Long.valueOf(stringCod));
71         }
72         if ("message".equals(fieldname)) {
73             forecastWeatherData.setMessage(jParser.getDoubleValue());
74         }
75         if ("city".equals(fieldname)) {
76             while (jParser.nextToken() != JsonToken.END_OBJECT) {
77                 final String namefield = jParser.getCurrentName();
78                 final JsonToken nextToken = jParser.nextToken(); // move to
79                 // value
80                 if ("id".equals(namefield)) {
81                     forecastWeatherData.getCity().setId(jParser.getLongValue());
82                 }
83                 if ("name".equals(namefield)) {
84                     forecastWeatherData.getCity().setName(jParser.getText());
85                 }
86                 if ("coord".equals(namefield)) {
87                     if (nextToken == JsonToken.START_OBJECT) {
88                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
89                     }
90                 }
91                 if ("country".equals(namefield)) {
92                     forecastWeatherData.getCity().setCountry(jParser.getText());
93                 }
94                 if ("population".equals(namefield)) {
95                     forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
96                 }
97             }
98         }
99         if ("cnt".equals(fieldname)) {
100             forecastWeatherData.setCnt(jParser.getIntValue());
101         }
102         if ("coord".equals(fieldname)) {
103             while (jParser.nextToken() != JsonToken.END_OBJECT) {
104                 final String namefield = jParser.getCurrentName();
105                 jParser.nextToken(); // move to value
106                 if ("lon".equals(namefield)) {
107                     forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
108                 }
109                 if ("lat".equals(namefield)) {
110                     forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
111                 }
112             }
113         }
114         if ("list".equals(fieldname)) {
115             final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List();
116             list.setTemp(new Temp());
117             list.setWeather(new ArrayList<Weather>());
118             forecastWeatherData.getList().add(list);
119             while (jParser.nextToken() != JsonToken.END_OBJECT) {
120                 final String namefield = jParser.getCurrentName();
121                 final JsonToken nextToken = jParser.nextToken(); // move to
122                 // value
123                 if ("dt".equals(namefield)) {
124                     list.setDt(jParser.getLongValue());
125                 }
126                 if ("temp".equals(namefield)) {
127                     if (nextToken == JsonToken.START_OBJECT) {
128                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
129                     }
130                 }
131                 if ("pressure".equals(namefield)) {
132                     list.setPressure(jParser.getDoubleValue());
133                 }
134                 if ("humidity".equals(namefield)) {
135                     list.setHumidity(jParser.getDoubleValue());
136                 }
137                 if ("weather".equals(namefield)) {
138                     if (nextToken == JsonToken.START_ARRAY) {
139                         JsonToken tokenNext = jParser.nextToken();
140                         while (tokenNext != JsonToken.END_ARRAY) {
141                             if (tokenNext == JsonToken.START_OBJECT) {
142                                 this.getForecastWeatherDataObjects(forecastWeatherData, jParser,
143                                         namefield);
144                             }
145                             tokenNext = jParser.nextToken();
146                         }
147                     }
148                 }
149                 if ("speed".equals(namefield)) {
150                     list.setSpeed(jParser.getDoubleValue());
151                 }
152                 if ("deg".equals(namefield)) {
153                     list.setDeg(jParser.getDoubleValue());
154                 }
155                 if ("clouds".equals(namefield)) {
156                     list.setClouds(jParser.getDoubleValue());
157                 }
158                 if ("rain".equals(namefield)) {
159                     list.setRain(jParser.getDoubleValue());
160                 }
161             }
162         }
163         if ("temp".equals(fieldname)) {
164             final name.gumartinm.weather.information.model.forecastweather.List list =
165                     forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
166             while (jParser.nextToken() != JsonToken.END_OBJECT) {
167                 final String namefield = jParser.getCurrentName();
168                 jParser.nextToken(); // move to value
169                 if ("day".equals(namefield)) {
170                     list.getTemp().setDay(jParser.getDoubleValue());
171                 }
172                 if ("min".equals(namefield)) {
173                     list.getTemp().setMin(jParser.getDoubleValue());
174                 }
175                 if ("max".equals(namefield)) {
176                     list.getTemp().setMax(jParser.getDoubleValue());
177                 }
178                 if ("night".equals(namefield)) {
179                     list.getTemp().setNight(jParser.getDoubleValue());
180                 }
181                 if ("eve".equals(namefield)) {
182                     list.getTemp().setEve(jParser.getDoubleValue());
183                 }
184                 if ("morn".equals(namefield)) {
185                     list.getTemp().setMorn(jParser.getDoubleValue());
186                 }
187             }
188         }
189         if ("weather".equals(fieldname)) {
190             final name.gumartinm.weather.information.model.forecastweather.List list =
191                     forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
192             final Weather weather = new Weather();
193             while (jParser.nextToken() != JsonToken.END_OBJECT) {
194                 final String namefield = jParser.getCurrentName();
195                 jParser.nextToken(); // move to value
196
197                 if ("id".equals(namefield)) {
198                     weather.setId(jParser.getIntValue());
199                 }
200                 if ("main".equals(namefield)) {
201                     weather.setMain(jParser.getText());
202                 }
203                 if ("description".equals(namefield)) {
204                     weather.setDescription(jParser.getText());
205                 }
206                 if ("icon".equals(namefield)) {
207                     weather.setIcon(jParser.getText());
208                 }
209             }
210             list.getWeather().add(weather);
211         }
212     }
213 }