1 | package iodb;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.text.ParseException;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.List;
|
---|
10 | import javax.xml.parsers.ParserConfigurationException;
|
---|
11 | import javax.xml.parsers.SAXParserFactory;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.io.UTFInputStreamReader;
|
---|
17 | import org.xml.sax.Attributes;
|
---|
18 | import org.xml.sax.InputSource;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 | import org.xml.sax.helpers.DefaultHandler;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Parses the message from server. It expects XML in UTF-8 with several <offset> elements.
|
---|
24 | *
|
---|
25 | * @author zverik
|
---|
26 | */
|
---|
27 | public class IODBReader {
|
---|
28 | private List<ImageryOffsetBase> offsets;
|
---|
29 | private InputSource source;
|
---|
30 |
|
---|
31 | private class Parser extends DefaultHandler {
|
---|
32 | private StringBuffer accumulator = new StringBuffer();
|
---|
33 | private IOFields fields;
|
---|
34 | private boolean parsingOffset;
|
---|
35 | private SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void startDocument() throws SAXException {
|
---|
39 | fields = new IOFields();
|
---|
40 | offsets.clear();
|
---|
41 | parsingOffset = false;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private LatLon parseLatLon(Attributes atts) {
|
---|
45 | return new LatLon(
|
---|
46 | Double.parseDouble(atts.getValue("lat")),
|
---|
47 | Double.parseDouble(atts.getValue("lon")));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
---|
52 | if( !parsingOffset ) {
|
---|
53 | if( qName.equals("offset") || qName.equals("calibration-object") ) {
|
---|
54 | parsingOffset = true;
|
---|
55 | fields.clear();
|
---|
56 | fields.position = parseLatLon(attributes);
|
---|
57 | }
|
---|
58 | } else {
|
---|
59 | if( qName.equals("object") ) {
|
---|
60 | fields.isNode = attributes.getValue("type").equals("node");
|
---|
61 | } else if( qName.equals("imagery-position") ) {
|
---|
62 | fields.imageryPos = parseLatLon(attributes);
|
---|
63 | } else if( qName.equals("imagery") ) {
|
---|
64 | String minZoom = attributes.getValue("minzoom");
|
---|
65 | String maxZoom = attributes.getValue("maxzoom");
|
---|
66 | if( minZoom != null )
|
---|
67 | fields.minZoom = Integer.parseInt(minZoom);
|
---|
68 | if( maxZoom != null )
|
---|
69 | fields.maxZoom = Integer.parseInt(maxZoom);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | accumulator.setLength(0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public void characters(char[] ch, int start, int length) throws SAXException {
|
---|
77 | if( parsingOffset )
|
---|
78 | accumulator.append(ch, start, length);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
83 | if( parsingOffset ) {
|
---|
84 | if( qName.equals("author") ) {
|
---|
85 | fields.author = accumulator.toString();
|
---|
86 | } else if( qName.equals("description") ) {
|
---|
87 | fields.description = accumulator.toString();
|
---|
88 | } else if( qName.equals("date") ) {
|
---|
89 | try {
|
---|
90 | fields.date = dateParser.parse(accumulator.toString());
|
---|
91 | } catch (ParseException ex) {
|
---|
92 | throw new SAXException(ex);
|
---|
93 | }
|
---|
94 | } else if( qName.equals("deprecated") ) {
|
---|
95 | try {
|
---|
96 | fields.abandonDate = dateParser.parse(accumulator.toString());
|
---|
97 | } catch (ParseException ex) {
|
---|
98 | throw new SAXException(ex);
|
---|
99 | }
|
---|
100 | } else if( qName.equals("imagery") ) {
|
---|
101 | fields.imagery = accumulator.toString();
|
---|
102 | } else if( qName.equals("object") ) {
|
---|
103 | fields.objectId = Integer.parseInt(accumulator.toString());
|
---|
104 | } else if( qName.equals("last-user") ) {
|
---|
105 | fields.lastUserId = Integer.parseInt(accumulator.toString());
|
---|
106 | } else if( qName.equals("offset") || qName.equals("calibration-object") ) {
|
---|
107 | // store offset
|
---|
108 | try {
|
---|
109 | offsets.add(fields.constructObject());
|
---|
110 | } catch( IllegalArgumentException ex ) {
|
---|
111 | System.err.println(ex.getMessage());
|
---|
112 | }
|
---|
113 | parsingOffset = false;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | public IODBReader( InputStream source ) throws IOException {
|
---|
121 | this.source = new InputSource(UTFInputStreamReader.create(source, "UTF-8"));
|
---|
122 | this.offsets = new ArrayList<ImageryOffsetBase>();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public List<ImageryOffsetBase> parse() throws SAXException, IOException {
|
---|
126 | Parser parser = new Parser();
|
---|
127 | try {
|
---|
128 | SAXParserFactory factory = SAXParserFactory.newInstance();
|
---|
129 | factory.newSAXParser().parse(source, parser);
|
---|
130 | return offsets;
|
---|
131 | } catch (ParserConfigurationException e) {
|
---|
132 | e.printStackTrace();
|
---|
133 | throw new SAXException(e);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private class IOFields {
|
---|
138 | public LatLon position;
|
---|
139 | public Date date;
|
---|
140 | public String author;
|
---|
141 | public String description;
|
---|
142 | public Date abandonDate;
|
---|
143 | public LatLon imageryPos;
|
---|
144 | public String imagery;
|
---|
145 | public int minZoom, maxZoom;
|
---|
146 | public boolean isNode;
|
---|
147 | public long objectId;
|
---|
148 | public long lastUserId;
|
---|
149 |
|
---|
150 | public IOFields() {
|
---|
151 | clear();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void clear() {
|
---|
155 | position = null;
|
---|
156 | date = null;
|
---|
157 | author = null;
|
---|
158 | description = null;
|
---|
159 | abandonDate = null;
|
---|
160 | imageryPos = null;
|
---|
161 | imagery = null;
|
---|
162 | minZoom = -1;
|
---|
163 | maxZoom = -1;
|
---|
164 | isNode = false;
|
---|
165 | objectId = -1;
|
---|
166 | lastUserId = -1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public ImageryOffsetBase constructObject() {
|
---|
170 | if( author == null || description == null || position == null || date == null )
|
---|
171 | throw new IllegalArgumentException("Not enought arguments to build an object");
|
---|
172 | if( objectId < 0 ) {
|
---|
173 | if( imagery == null || imageryPos == null )
|
---|
174 | throw new IllegalArgumentException("Both imagery and imageryPos should be sepcified for the offset");
|
---|
175 | ImageryOffset result = new ImageryOffset(imagery, imageryPos);
|
---|
176 | if( minZoom >= 0 )
|
---|
177 | result.setMinZoom(minZoom);
|
---|
178 | if( maxZoom >= 0 )
|
---|
179 | result.setMaxZoom(maxZoom);
|
---|
180 | result.setBasicInfo(position, author, description, date);
|
---|
181 | result.setAbandonDate(abandonDate);
|
---|
182 | return result;
|
---|
183 | } else {
|
---|
184 | OsmPrimitive p = isNode ? new Node(objectId) : new Way(objectId);
|
---|
185 | CalibrationObject result = new CalibrationObject(p, lastUserId);
|
---|
186 | result.setBasicInfo(position, author, description, date);
|
---|
187 | result.setAbandonDate(abandonDate);
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|