001/**
002 *
003 * Copyright 2015-2017 Ishan Khanna, Fernando Ramirez
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.geoloc.provider;
018
019import java.io.IOException;
020import java.net.URISyntaxException;
021import java.text.ParseException;
022
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.jivesoftware.smack.util.ParserUtils;
025
026import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
027
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031public class GeoLocationProvider extends ExtensionElementProvider<GeoLocation> {
032
033    @Override
034    public GeoLocation parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
035                    ParseException, URISyntaxException {
036
037        GeoLocation.Builder builder = GeoLocation.builder();
038
039        outerloop: while (true) {
040            int event = parser.next();
041            switch (event) {
042            case XmlPullParser.START_TAG:
043                String name = parser.getName();
044                switch (name) {
045                case "accuracy":
046                    builder.setAccuracy(ParserUtils.getDoubleFromNextText(parser));
047                    break;
048                case "alt":
049                    builder.setAlt(ParserUtils.getDoubleFromNextText(parser));
050                    break;
051                case "altaccuracy":
052                    builder.setAltAccuracy(ParserUtils.getDoubleFromNextText(parser));
053                    break;
054                case "area":
055                    builder.setArea(parser.nextText());
056                    break;
057                case "bearing":
058                    builder.setBearing(ParserUtils.getDoubleFromNextText(parser));
059                    break;
060                case "building":
061                    builder.setBuilding(parser.nextText());
062                    break;
063                case "country":
064                    builder.setCountry(parser.nextText());
065                    break;
066                case "countrycode":
067                    builder.setCountryCode(parser.nextText());
068                    break;
069                case "datum":
070                    builder.setDatum(parser.nextText());
071                    break;
072                case "description":
073                    builder.setDescription(parser.nextText());
074                    break;
075                case "error":
076                    builder.setError(ParserUtils.getDoubleFromNextText(parser));
077                    break;
078                case "floor":
079                    builder.setFloor(parser.nextText());
080                    break;
081                case "lat":
082                    builder.setLat(ParserUtils.getDoubleFromNextText(parser));
083                    break;
084                case "locality":
085                    builder.setLocality(parser.nextText());
086                    break;
087                case "lon":
088                    builder.setLon(ParserUtils.getDoubleFromNextText(parser));
089                    break;
090                case "postalcode":
091                    builder.setPostalcode(parser.nextText());
092                    break;
093                case "region":
094                    builder.setRegion(parser.nextText());
095                    break;
096                case "room":
097                    builder.setRoom(parser.nextText());
098                    break;
099                case "speed":
100                    builder.setSpeed(ParserUtils.getDoubleFromNextText(parser));
101                    break;
102                case "street":
103                    builder.setStreet(parser.nextText());
104                    break;
105                case "text":
106                    builder.setText(parser.nextText());
107                    break;
108                case "timestamp":
109                    builder.setTimestamp(ParserUtils.getDateFromNextText(parser));
110                    break;
111                case "tzo":
112                    builder.setTzo(parser.nextText());
113                    break;
114                case "uri":
115                    builder.setUri(ParserUtils.getUriFromNextText(parser));
116                    break;
117                }
118                break;
119            case XmlPullParser.END_TAG:
120                if (parser.getDepth() == initialDepth) {
121                    break outerloop;
122                }
123            }
124        }
125
126        return builder.build();
127    }
128
129}