f210e12420b517dcfc0f634cbc7b83902b36253d
[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.currentweather.Clouds;
27 import name.gumartinm.weather.information.model.currentweather.Coord;
28 import name.gumartinm.weather.information.model.currentweather.Current;
29 import name.gumartinm.weather.information.model.currentweather.Main;
30 import name.gumartinm.weather.information.model.currentweather.Rain;
31 import name.gumartinm.weather.information.model.currentweather.Snow;
32 import name.gumartinm.weather.information.model.currentweather.Sys;
33 import name.gumartinm.weather.information.model.currentweather.Weather;
34 import name.gumartinm.weather.information.model.currentweather.Wind;
35
36 public class JPOSCurrentParser {
37
38     public Current retrieveCurrenFromJPOS(final String jsonData)
39             throws JsonParseException, IOException {
40         final JsonFactory f = new JsonFactory();
41
42         final Current currentWeatherData = new Current();
43         currentWeatherData.setClouds(new Clouds());
44         currentWeatherData.setCoord(new Coord());
45         currentWeatherData.setMain(new Main());
46         currentWeatherData.setRain(new Rain());
47         currentWeatherData.setSys(new Sys());
48         currentWeatherData.setSnow(new Snow());
49         currentWeatherData
50                 .setWeather(new ArrayList<Weather>());
51         currentWeatherData.setWind(new Wind());
52         final JsonParser jParser = f.createParser(jsonData);
53
54         this.getCurrentWeatherData(currentWeatherData, jParser);
55
56         return currentWeatherData;
57     }
58
59     private void getCurrentWeatherData(final Current currentWeatherData,
60                                        final JsonParser jParser) throws JsonParseException, IOException {
61         if (jParser.nextToken() == JsonToken.START_OBJECT) {
62
63             while (jParser.nextToken() != JsonToken.END_OBJECT) {
64                 final String fieldname = jParser.getCurrentName();
65                 final JsonToken nextToken = jParser.nextToken();
66                 if (nextToken == JsonToken.START_OBJECT) {
67                     this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
68                 }
69                 if (nextToken == JsonToken.START_ARRAY) {
70                     JsonToken tokenNext = jParser.nextToken();
71                     while (tokenNext != JsonToken.END_ARRAY) {
72                         if (tokenNext == JsonToken.START_OBJECT) {
73                             this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
74                         }
75                         tokenNext = jParser.nextToken();
76                     }
77                 }
78                 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
79                         || (nextToken == JsonToken.VALUE_STRING)) {
80                     this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
81                 }
82             }
83         }
84     }
85
86     private void getCurrentWeatherDataObjects(final Current currentWeatherData,
87                                               final JsonParser jParser, final String fieldname) throws JsonParseException,
88             IOException {
89         if ("coord".equals(fieldname)) {
90             while (jParser.nextToken() != JsonToken.END_OBJECT) {
91                 final String namefield = jParser.getCurrentName();
92                 jParser.nextToken(); // move to value
93                 if ("lon".equals(namefield)) {
94                     currentWeatherData.getCoord().setLon(jParser.getDoubleValue());
95                 }
96                 if ("lat".equals(namefield)) {
97                     currentWeatherData.getCoord().setLat(jParser.getDoubleValue());
98                 }
99             }
100         }
101         if ("sys".equals(fieldname)) {
102             while (jParser.nextToken() != JsonToken.END_OBJECT) {
103                 final String namefield = jParser.getCurrentName();
104                 jParser.nextToken(); // move to value
105                 if ("message".equals(namefield)) {
106                     currentWeatherData.getSys().setMessage(jParser.getDoubleValue());
107                 }
108                 if ("country".equals(namefield)) {
109                     currentWeatherData.getSys().setCountry(jParser.getValueAsString());
110                 }
111                 if ("sunrise".equals(namefield)) {
112                     currentWeatherData.getSys().setSunrise(jParser.getValueAsLong());
113                 }
114                 if ("sunset".equals(namefield)) {
115                     currentWeatherData.getSys().setSunset(jParser.getValueAsLong());
116                 }
117             }
118         }
119         if ("weather".equals(fieldname)) {
120             final Weather weather = new Weather();
121             currentWeatherData.getWeather().add(weather);
122             while (jParser.nextToken() != JsonToken.END_OBJECT) {
123                 final String namefield = jParser.getCurrentName();
124                 jParser.nextToken(); // move to value
125                 if ("id".equals(namefield)) {
126                     weather.setId(jParser.getIntValue());
127                 }
128                 if ("main".equals(namefield)) {
129                     weather.setMain(jParser.getText());
130                 }
131                 if ("description".equals(namefield)) {
132                     weather.setDescription(jParser.getText());
133                 }
134                 if ("icon".equals(namefield)) {
135                     weather.setIcon(jParser.getText());
136                 }
137
138             }
139         }
140         if ("base".equals(fieldname)) {
141             currentWeatherData.setBase(jParser.getText());
142         }
143         if ("main".equals(fieldname)) {
144             while (jParser.nextToken() != JsonToken.END_OBJECT) {
145                 final String namefield = jParser.getCurrentName();
146                 jParser.nextToken(); // move to value
147                 if ("temp".equals(namefield)) {
148                     currentWeatherData.getMain().setTemp(jParser.getDoubleValue());
149                 }
150                 if ("temp_min".equals(namefield)) {
151                     currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue());
152                 }
153                 if ("temp_max".equals(namefield)) {
154                     currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue());
155                 }
156                 if ("pressure".equals(namefield)) {
157                     currentWeatherData.getMain().setPressure(jParser.getDoubleValue());
158                 }
159                 if ("sea_level".equals(namefield)) {
160                     currentWeatherData.getMain().setSea_level(jParser.getDoubleValue());
161                 }
162                 if ("grnd_level".equals(namefield)) {
163                     currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue());
164                 }
165                 if ("humidity".equals(namefield)) {
166                     currentWeatherData.getMain().setHumidity(jParser.getDoubleValue());
167                 }
168             }
169         }
170         if ("wind".equals(fieldname)) {
171             while (jParser.nextToken() != JsonToken.END_OBJECT) {
172                 final String namefield = jParser.getCurrentName();
173                 jParser.nextToken(); // move to value
174                 if ("speed".equals(namefield)) {
175                     currentWeatherData.getWind().setSpeed(jParser.getDoubleValue());
176                 }
177                 if ("deg".equals(namefield)) {
178                     currentWeatherData.getWind().setDeg(jParser.getDoubleValue());
179                 }
180             }
181         }
182         if ("clouds".equals(fieldname)) {
183             while (jParser.nextToken() != JsonToken.END_OBJECT) {
184                 final String namefield = jParser.getCurrentName();
185                 jParser.nextToken(); // move to value
186                 if ("all".equals(namefield)) {
187                     currentWeatherData.getClouds().setAll(jParser.getDoubleValue());
188                 }
189             }
190         }
191         if ("dt".equals(fieldname)) {
192             currentWeatherData.setDt(jParser.getLongValue());
193         }
194         if ("rain".equals(fieldname)) {
195             while (jParser.nextToken() != JsonToken.END_OBJECT) {
196                 final String namefield = jParser.getCurrentName();
197                 jParser.nextToken(); // move to value
198                 if ("3h".equals(namefield)) {
199                     currentWeatherData.getRain().set3h(jParser.getDoubleValue());
200                 }
201             }
202         }
203         if ("snow".equals(fieldname)) {
204             while (jParser.nextToken() != JsonToken.END_OBJECT) {
205                 final String namefield = jParser.getCurrentName();
206                 jParser.nextToken(); // move to value
207                 if ("3h".equals(namefield)) {
208                     currentWeatherData.getSnow().set3h(jParser.getDoubleValue());
209                 }
210             }
211         }
212         if ("id".equals(fieldname)) {
213             currentWeatherData.setId(jParser.getLongValue());
214         }
215         if ("name".equals(fieldname)) {
216             currentWeatherData.setName(jParser.getText());
217         }
218         if ("cod".equals(fieldname)) {
219             currentWeatherData.setCod(jParser.getIntValue());
220         }
221     }
222 }