1
package com.bdaum.zoom.gps.naming.google.internal;
3
import java.io.InputStream;
4
import java.text.ParseException;
6
import javax.xml.parsers.ParserConfigurationException;
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;
13
import com.bdaum.zoom.gps.geonames.AbstractParser;
14
import com.bdaum.zoom.gps.geonames.WebServiceException;
15
import com.bdaum.zoom.ui.gps.WaypointArea;
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;
33
public GoogleGeoCodeParser(InputStream in)
34
throws ParserConfigurationException, SAXException {
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;
49
public void startElement(String namespaceURI, String localName,
50
String qName, Attributes atts) throws SAXException {
51
if (RESULT.equals(qName)) {
55
if (GEOMETRY.equals(qName))
57
else if (geometry && LOCATION.equals(qName))
59
else if (geometry && VIEWPORT.equals(qName))
61
else if (viewport && SOUTHWEST.equals(qName))
63
else if (viewport && NORTHEAST.equals(qName))
70
public void characters(char[] ch, int start, int length) {
71
text.append(ch, start, length);
75
public void endElement(String uri, String localName, String qName)
78
String s = text.toString();
79
if (STATUS.equals(qName) && !STATUS_OK.equals(s)) {
80
if (STATUS_QUERYLIMIT.equals(s))
81
throw new WebServiceException(
83
Messages.GooglePlaceParser_google_web_service_exception,
84
text), new WebServiceException(s));
85
throw new WebServiceException(
87
Messages.GooglePlaceParser_google_web_service_exception,
89
} else if (ADDRESS.equals(qName)) {
90
getWaypoint().setName(s);
92
if (LAT.equals(qName)) {
94
double lat = getDouble(text.toString());
96
getWaypoint().setLat(lat);
98
getWaypoint().setSWlat(lat);
100
getWaypoint().setNElat(lat);
101
} catch (ParseException e) {
102
throw new SAXException(e);
104
} else if (LNG.equals(qName)) {
106
double lon = getDouble(text.toString());
108
getWaypoint().setLon(lon);
110
getWaypoint().setSWlon(lon);
112
getWaypoint().setNElon(lon);
113
} catch (ParseException e) {
114
throw new SAXException(e);
117
if (location && LOCATION.equals(qName))
120
if (VIEWPORT.equals(qName))
122
else if (SOUTHWEST.equals(qName))
124
else if (NORTHEAST.equals(qName))
130
public WaypointArea getWaypoint() {
131
if (waypoint == null) {
132
waypoint = new WaypointArea();