5801fc2a16fc87eb31e3e3e823f50c6448d27b7c
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package name.gumartinm.weather.information.parser;
17
18 import com.fasterxml.jackson.core.JsonFactory;
19 import com.fasterxml.jackson.core.JsonParseException;
20 import com.fasterxml.jackson.core.JsonParser;
21 import com.fasterxml.jackson.core.JsonToken;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25
26 import name.gumartinm.weather.information.model.forecastweather.City;
27 import name.gumartinm.weather.information.model.forecastweather.Coord;
28 import name.gumartinm.weather.information.model.forecastweather.Forecast;
29 import name.gumartinm.weather.information.model.forecastweather.List;
30 import name.gumartinm.weather.information.model.forecastweather.Temp;
31 import name.gumartinm.weather.information.model.forecastweather.Weather;
32
33 public class JPOSForecastParser {
34
35     public Forecast retrieveForecastFromJPOS(final String jsonData)
36             throws JsonParseException, IOException {
37         final JsonFactory f = new JsonFactory();
38
39         final Forecast forecastWeatherData = new Forecast();
40         forecastWeatherData
41                 .setList(new ArrayList<List>(15));
42         final City city = new City();
43         city.setCoord(new Coord());
44         forecastWeatherData.setCity(city);
45         final JsonParser jParser = f.createParser(jsonData);
46
47         this.getForecastWeatherData(forecastWeatherData, jParser);
48
49         return forecastWeatherData;
50     }
51
52     private void getForecastWeatherData(final Forecast forecastWeatherData,
53                                         final JsonParser jParser) throws JsonParseException, IOException {
54         if (jParser.nextToken() == JsonToken.START_OBJECT) {
55
56             while (jParser.nextToken() != JsonToken.END_OBJECT) {
57                 final String fieldname = jParser.getCurrentName();
58                 final JsonToken nextToken = jParser.nextToken();
59                 if (nextToken == JsonToken.START_OBJECT) {
60                     this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
61                 }
62                 if (nextToken == JsonToken.START_ARRAY) {
63                     JsonToken tokenNext = jParser.nextToken();
64                     while (tokenNext != JsonToken.END_ARRAY) {
65                         if (tokenNext == JsonToken.START_OBJECT) {
66                             this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
67                         }
68                         tokenNext = jParser.nextToken();
69                     }
70                 }
71                 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
72                         || (nextToken == JsonToken.VALUE_STRING)) {
73                     this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
74                 }
75             }
76         }
77     }
78
79     private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
80                                                final JsonParser jParser, final String fieldname) throws JsonParseException,
81             IOException {
82
83         if ("cod".equals(fieldname)) {
84             final String stringCod = jParser.getText();
85             forecastWeatherData.setCod(Long.valueOf(stringCod));
86         }
87         if ("message".equals(fieldname)) {
88             forecastWeatherData.setMessage(jParser.getDoubleValue());
89         }
90         if ("city".equals(fieldname)) {
91             while (jParser.nextToken() != JsonToken.END_OBJECT) {
92                 final String namefield = jParser.getCurrentName();
93                 final JsonToken nextToken = jParser.nextToken(); // move to
94                 // value
95                 if ("id".equals(namefield)) {
96                     forecastWeatherData.getCity().setId(jParser.getLongValue());
97                 }
98                 if ("name".equals(namefield)) {
99                     forecastWeatherData.getCity().setName(jParser.getText());
100                 }
101                 if ("coord".equals(namefield)) {
102                     if (nextToken == JsonToken.START_OBJECT) {
103                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
104                     }
105                 }
106                 if ("country".equals(namefield)) {
107                     forecastWeatherData.getCity().setCountry(jParser.getText());
108                 }
109                 if ("population".equals(namefield)) {
110                     forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
111                 }
112             }
113         }
114         if ("cnt".equals(fieldname)) {
115             forecastWeatherData.setCnt(jParser.getIntValue());
116         }
117         if ("coord".equals(fieldname)) {
118             while (jParser.nextToken() != JsonToken.END_OBJECT) {
119                 final String namefield = jParser.getCurrentName();
120                 jParser.nextToken(); // move to value
121                 if ("lon".equals(namefield)) {
122                     forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
123                 }
124                 if ("lat".equals(namefield)) {
125                     forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
126                 }
127             }
128         }
129         if ("list".equals(fieldname)) {
130             final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List();
131             list.setTemp(new Temp());
132             list.setWeather(new ArrayList<Weather>());
133             forecastWeatherData.getList().add(list);
134             while (jParser.nextToken() != JsonToken.END_OBJECT) {
135                 final String namefield = jParser.getCurrentName();
136                 final JsonToken nextToken = jParser.nextToken(); // move to
137                 // value
138                 if ("dt".equals(namefield)) {
139                     list.setDt(jParser.getLongValue());
140                 }
141                 if ("temp".equals(namefield)) {
142                     if (nextToken == JsonToken.START_OBJECT) {
143                         this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
144                     }
145                 }
146                 if ("pressure".equals(namefield)) {
147                     list.setPressure(jParser.getDoubleValue());
148                 }
149                 if ("humidity".equals(namefield)) {
150                     list.setHumidity(jParser.getDoubleValue());
151                 }
152                 if ("weather".equals(namefield)) {
153                     if (nextToken == JsonToken.START_ARRAY) {
154                         JsonToken tokenNext = jParser.nextToken();
155                         while (tokenNext != JsonToken.END_ARRAY) {
156                             if (tokenNext == JsonToken.START_OBJECT) {
157                                 this.getForecastWeatherDataObjects(forecastWeatherData, jParser,
158                                         namefield);
159                             }
160                             tokenNext = jParser.nextToken();
161                         }
162                     }
163                 }
164                 if ("speed".equals(namefield)) {
165                     list.setSpeed(jParser.getDoubleValue());
166                 }
167                 if ("deg".equals(namefield)) {
168                     list.setDeg(jParser.getDoubleValue());
169                 }
170                 if ("clouds".equals(namefield)) {
171                     list.setClouds(jParser.getDoubleValue());
172                 }
173                 if ("rain".equals(namefield)) {
174                     list.setRain(jParser.getDoubleValue());
175                 }
176             }
177         }
178         if ("temp".equals(fieldname)) {
179             final name.gumartinm.weather.information.model.forecastweather.List list =
180                     forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
181             while (jParser.nextToken() != JsonToken.END_OBJECT) {
182                 final String namefield = jParser.getCurrentName();
183                 jParser.nextToken(); // move to value
184                 if ("day".equals(namefield)) {
185                     list.getTemp().setDay(jParser.getDoubleValue());
186                 }
187                 if ("min".equals(namefield)) {
188                     list.getTemp().setMin(jParser.getDoubleValue());
189                 }
190                 if ("max".equals(namefield)) {
191                     list.getTemp().setMax(jParser.getDoubleValue());
192                 }
193                 if ("night".equals(namefield)) {
194                     list.getTemp().setNight(jParser.getDoubleValue());
195                 }
196                 if ("eve".equals(namefield)) {
197                     list.getTemp().setEve(jParser.getDoubleValue());
198                 }
199                 if ("morn".equals(namefield)) {
200                     list.getTemp().setMorn(jParser.getDoubleValue());
201                 }
202             }
203         }
204         if ("weather".equals(fieldname)) {
205             final name.gumartinm.weather.information.model.forecastweather.List list =
206                     forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
207             final Weather weather = new Weather();
208             while (jParser.nextToken() != JsonToken.END_OBJECT) {
209                 final String namefield = jParser.getCurrentName();
210                 jParser.nextToken(); // move to value
211
212                 if ("id".equals(namefield)) {
213                     weather.setId(jParser.getIntValue());
214                 }
215                 if ("main".equals(namefield)) {
216                     weather.setMain(jParser.getText());
217                 }
218                 if ("description".equals(namefield)) {
219                     weather.setDescription(jParser.getText());
220                 }
221                 if ("icon".equals(namefield)) {
222                     weather.setIcon(jParser.getText());
223                 }
224             }
225             list.getWeather().add(weather);
226         }
227     }
228 }