Changeset 32528 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/IODBReader.java
- Timestamp:
- 2016-07-02T03:55:03+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/IODBReader.java
r30737 r32528 1 // License: WTFPL. For details, see LICENSE file. 1 2 package iodb; 2 3 … … 8 9 import java.util.Date; 9 10 import java.util.List; 11 10 12 import javax.xml.parsers.ParserConfigurationException; 11 13 import javax.xml.parsers.SAXParserFactory; 14 12 15 import org.openstreetmap.josm.data.coor.LatLon; 13 16 import org.openstreetmap.josm.io.UTFInputStreamReader; … … 20 23 * Parses the server response. It expects XML in UTF-8 with several <offset> 21 24 * and <calibration> elements. 22 * 25 * 23 26 * @author Zverik 24 27 * @license WTFPL … … 27 30 private List<ImageryOffsetBase> offsets; 28 31 private InputSource source; 29 32 30 33 /** 31 34 * Initializes the parser. This constructor creates an input source on the input … … 34 37 * @throws IOException Thrown when something's wrong with the stream. 35 38 */ 36 public IODBReader( 39 public IODBReader(InputStream source) throws IOException { 37 40 this.source = new InputSource(UTFInputStreamReader.create(source, "UTF-8")); 38 41 this.offsets = new ArrayList<>(); … … 51 54 factory.newSAXParser().parse(source, parser); 52 55 return offsets; 53 } catch (ParserConfigurationException e56 } catch (ParserConfigurationException e) { 54 57 throw new SAXException(e); 55 58 } … … 93 96 @Override 94 97 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 95 if (!parsingOffset96 if (qName.equals("offset") || qName.equals("calibration")98 if (!parsingOffset) { 99 if (qName.equals("offset") || qName.equals("calibration")) { 97 100 parsingOffset = true; 98 101 parsingDeprecate = false; … … 100 103 fields.position = parseLatLon(attributes); 101 104 fields.id = Integer.parseInt(attributes.getValue("id")); 102 if (attributes.getValue("flagged") != null && attributes.getValue("flagged").equals("yes")105 if (attributes.getValue("flagged") != null && attributes.getValue("flagged").equals("yes")) 103 106 fields.flagged = true; 104 107 } 105 108 } else { 106 if (qName.equals("node")109 if (qName.equals("node")) { 107 110 fields.geometry.add(parseLatLon(attributes)); 108 } else if (qName.equals("imagery-position")111 } else if (qName.equals("imagery-position")) { 109 112 fields.imageryPos = parseLatLon(attributes); 110 } else if (qName.equals("imagery")113 } else if (qName.equals("imagery")) { 111 114 String minZoom = attributes.getValue("minzoom"); 112 115 String maxZoom = attributes.getValue("maxzoom"); 113 if (minZoom != null116 if (minZoom != null) 114 117 fields.minZoom = Integer.parseInt(minZoom); 115 if (maxZoom != null118 if (maxZoom != null) 116 119 fields.maxZoom = Integer.parseInt(maxZoom); 117 } else if (qName.equals("deprecated")120 } else if (qName.equals("deprecated")) { 118 121 parsingDeprecate = true; 119 122 } … … 121 124 accumulator.setLength(0); 122 125 } 123 126 124 127 @Override 125 128 public void characters(char[] ch, int start, int length) throws SAXException { 126 if (parsingOffset129 if (parsingOffset) 127 130 accumulator.append(ch, start, length); 128 131 } … … 130 133 @Override 131 134 public void endElement(String uri, String localName, String qName) throws SAXException { 132 if (parsingOffset133 if (qName.equals("author")134 if (!parsingDeprecate135 if (parsingOffset) { 136 if (qName.equals("author")) { 137 if (!parsingDeprecate) 135 138 fields.author = accumulator.toString(); 136 139 else 137 140 fields.abandonAuthor = accumulator.toString(); 138 } else if (qName.equals("description")141 } else if (qName.equals("description")) { 139 142 fields.description = accumulator.toString(); 140 } else if (qName.equals("reason") && parsingDeprecate143 } else if (qName.equals("reason") && parsingDeprecate) { 141 144 fields.abandonReason = accumulator.toString(); 142 } else if (qName.equals("date")145 } else if (qName.equals("date")) { 143 146 try { 144 if (!parsingDeprecate147 if (!parsingDeprecate) 145 148 fields.date = dateParser.parse(accumulator.toString()); 146 149 else … … 149 152 throw new SAXException(ex); 150 153 } 151 } else if (qName.equals("deprecated")154 } else if (qName.equals("deprecated")) { 152 155 parsingDeprecate = false; 153 } else if (qName.equals("imagery")156 } else if (qName.equals("imagery")) { 154 157 fields.imagery = accumulator.toString(); 155 } else if (qName.equals("offset") || qName.equals("calibration")158 } else if (qName.equals("offset") || qName.equals("calibration")) { 156 159 // store offset 157 160 try { 158 161 offsets.add(fields.constructObject()); 159 } catch (IllegalArgumentException ex162 } catch (IllegalArgumentException ex) { 160 163 // On one hand, we don't care, but this situation is one 161 164 // of those "it can never happen" cases. … … 167 170 } 168 171 } 169 172 170 173 /** 171 174 * An accumulator for parsed fields. When there's enough data, it can construct … … 191 194 * A constructor just calls {@link #clear()}. 192 195 */ 193 publicIOFields() {196 IOFields() { 194 197 clear(); 195 198 } 196 199 197 200 /** 198 201 * Clear all fields to <tt>null</tt> and <tt>-1</tt>. … … 220 223 */ 221 224 public ImageryOffsetBase constructObject() { 222 if (author == null || description == null || position == null || date == null225 if (author == null || description == null || position == null || date == null) 223 226 throw new IllegalArgumentException("Not enought arguments to build an object"); 224 227 ImageryOffsetBase result; 225 if (geometry.isEmpty()226 if (imagery == null || imageryPos == null228 if (geometry.isEmpty()) { 229 if (imagery == null || imageryPos == null) 227 230 throw new IllegalArgumentException("Both imagery and imageryPos should be specified for the offset"); 228 231 result = new ImageryOffset(imagery, imageryPos); 229 if (minZoom >= 0230 ((ImageryOffset)result).setMinZoom(minZoom); 231 if (maxZoom >= 0232 ((ImageryOffset)result).setMaxZoom(maxZoom); 232 if (minZoom >= 0) 233 ((ImageryOffset) result).setMinZoom(minZoom); 234 if (maxZoom >= 0) 235 ((ImageryOffset) result).setMaxZoom(maxZoom); 233 236 } else { 234 237 result = new CalibrationObject(geometry.toArray(new LatLon[0])); 235 238 } 236 if (id >= 0239 if (id >= 0) 237 240 result.setId(id); 238 241 result.setBasicInfo(position, author, description, date); 239 242 result.setDeprecated(abandonDate, abandonAuthor, abandonReason); 240 if (flagged243 if (flagged) 241 244 result.setFlagged(flagged); 242 245 return result;
Note:
See TracChangeset
for help on using the changeset viewer.