~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.gps.naming.google/src/com/bdaum/zoom/gps/naming/google/internal/GoogleGeoCodeParser.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.bdaum.zoom.gps.naming.google.internal;
 
2
 
 
3
import java.io.InputStream;
 
4
import java.text.ParseException;
 
5
 
 
6
import javax.xml.parsers.ParserConfigurationException;
 
7
 
 
8
import org.eclipse.osgi.util.NLS;
 
9
import org.xml.sax.Attributes;
 
10
import org.xml.sax.SAXException;
 
11
import org.xml.sax.helpers.DefaultHandler;
 
12
 
 
13
import com.bdaum.zoom.gps.geonames.AbstractParser;
 
14
import com.bdaum.zoom.gps.geonames.WebServiceException;
 
15
import com.bdaum.zoom.ui.gps.WaypointArea;
 
16
 
 
17
@SuppressWarnings("restriction")
 
18
public class GoogleGeoCodeParser extends AbstractParser {
 
19
        private static final String RESULT = "result"; //$NON-NLS-1$
 
20
        private static final String ADDRESS = "formatted_address"; //$NON-NLS-1$
 
21
        private static final String LNG = "lng"; //$NON-NLS-1$
 
22
        private static final String LAT = "lat"; //$NON-NLS-1$
 
23
        private static final String LOCATION = "location"; //$NON-NLS-1$
 
24
        private static final String GEOMETRY = "geometry"; //$NON-NLS-1$
 
25
        private static final String VIEWPORT = "viewport"; //$NON-NLS-1$
 
26
        private static final String SOUTHWEST = "southwest"; //$NON-NLS-1$
 
27
        private static final String NORTHEAST = "northeast"; //$NON-NLS-1$
 
28
        private static final String STATUS = "status";//$NON-NLS-1$
 
29
        private static final String STATUS_OK = "OK"; //$NON-NLS-1$
 
30
        private static final String STATUS_QUERYLIMIT = "OVER_QUERY_LIMIT"; //$NON-NLS-1$
 
31
        protected boolean northeast;
 
32
 
 
33
        public GoogleGeoCodeParser(InputStream in)
 
34
                        throws ParserConfigurationException, SAXException {
 
35
                super(in);
 
36
        }
 
37
 
 
38
        @Override
 
39
        protected DefaultHandler getHandler() {
 
40
                DefaultHandler handler = new DefaultHandler() {
 
41
                        private boolean geo = false;
 
42
                        private WaypointArea waypoint;
 
43
                        private boolean geometry;
 
44
                        private boolean location;
 
45
                        private boolean viewport;
 
46
                        private boolean southwest;
 
47
 
 
48
                        @Override
 
49
                        public void startElement(String namespaceURI, String localName,
 
50
                                        String qName, Attributes atts) throws SAXException {
 
51
                                if (RESULT.equals(qName)) {
 
52
                                        geo = true;
 
53
                                        waypoint = null;
 
54
                                } else if (geo) {
 
55
                                        if (GEOMETRY.equals(qName))
 
56
                                                geometry = true;
 
57
                                        else if (geometry && LOCATION.equals(qName))
 
58
                                                location = true;
 
59
                                        else if (geometry && VIEWPORT.equals(qName))
 
60
                                                viewport = true;
 
61
                                        else if (viewport && SOUTHWEST.equals(qName))
 
62
                                                southwest = true;
 
63
                                        else if (viewport && NORTHEAST.equals(qName))
 
64
                                                northeast = true;
 
65
                                }
 
66
                                text.setLength(0);
 
67
                        }
 
68
 
 
69
                        @Override
 
70
                        public void characters(char[] ch, int start, int length) {
 
71
                                text.append(ch, start, length);
 
72
                        }
 
73
 
 
74
                        @Override
 
75
                        public void endElement(String uri, String localName, String qName)
 
76
                                        throws SAXException {
 
77
                                if (geo) {
 
78
                                        String s = text.toString();
 
79
                                        if (STATUS.equals(qName) && !STATUS_OK.equals(s)) {
 
80
                                                if (STATUS_QUERYLIMIT.equals(s))
 
81
                                                        throw new WebServiceException(
 
82
                                                                        NLS.bind(
 
83
                                                                                        Messages.GooglePlaceParser_google_web_service_exception,
 
84
                                                                                        text), new WebServiceException(s));
 
85
                                                throw new WebServiceException(
 
86
                                                                NLS.bind(
 
87
                                                                                Messages.GooglePlaceParser_google_web_service_exception,
 
88
                                                                                s));
 
89
                                        } else if (ADDRESS.equals(qName)) {
 
90
                                                getWaypoint().setName(s);
 
91
                                        }
 
92
                                        if (LAT.equals(qName)) {
 
93
                                                try {
 
94
                                                        double lat = getDouble(text.toString());
 
95
                                                        if (location)
 
96
                                                                getWaypoint().setLat(lat);
 
97
                                                        else if (southwest)
 
98
                                                                getWaypoint().setSWlat(lat);
 
99
                                                        else if (northeast)
 
100
                                                                getWaypoint().setNElat(lat);
 
101
                                                } catch (ParseException e) {
 
102
                                                        throw new SAXException(e);
 
103
                                                }
 
104
                                        } else if (LNG.equals(qName)) {
 
105
                                                try {
 
106
                                                        double lon = getDouble(text.toString());
 
107
                                                        if (location)
 
108
                                                                getWaypoint().setLon(lon);
 
109
                                                        else if (southwest)
 
110
                                                                getWaypoint().setSWlon(lon);
 
111
                                                        else if (northeast)
 
112
                                                                getWaypoint().setNElon(lon);
 
113
                                                } catch (ParseException e) {
 
114
                                                        throw new SAXException(e);
 
115
                                                }
 
116
                                        }
 
117
                                        if (location && LOCATION.equals(qName))
 
118
                                                location = false;
 
119
                                        if (viewport) {
 
120
                                                if (VIEWPORT.equals(qName))
 
121
                                                        viewport = false;
 
122
                                                else if (SOUTHWEST.equals(qName))
 
123
                                                        southwest = false;
 
124
                                                else if (NORTHEAST.equals(qName))
 
125
                                                        northeast = false;
 
126
                                        }
 
127
                                }
 
128
                        }
 
129
 
 
130
                        public WaypointArea getWaypoint() {
 
131
                                if (waypoint == null) {
 
132
                                        waypoint = new WaypointArea();
 
133
                                        pnts.add(waypoint);
 
134
                                }
 
135
                                return waypoint;
 
136
                        }
 
137
                };
 
138
                return handler;
 
139
        }
 
140
 
 
141
}