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