Changeset 14101 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2018-08-07T00:24:36+02:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/NoteReader.java
r13901 r14101 11 11 import java.util.Locale; 12 12 import java.util.Optional; 13 import java.util.function.Function; 13 14 14 15 import javax.xml.parsers.ParserConfigurationException; … … 85 86 if (parseMode == NoteParseMode.API) { 86 87 if ("note".equals(qName)) { 87 double lat = Double.parseDouble(attrs.getValue("lat")); 88 double lon = Double.parseDouble(attrs.getValue("lon")); 89 LatLon noteLatLon = new LatLon(lat, lon); 90 thisNote = new Note(noteLatLon); 88 thisNote = parseNoteBasic(attrs); 91 89 } 92 90 return; … … 96 94 switch(qName) { 97 95 case "note": 98 double lat = Double.parseDouble(attrs.getValue("lat")); 99 double lon = Double.parseDouble(attrs.getValue("lon")); 100 LatLon noteLatLon = new LatLon(lat, lon); 101 thisNote = new Note(noteLatLon); 102 thisNote.setId(Long.parseLong(attrs.getValue("id"))); 103 String closedTimeStr = attrs.getValue("closed_at"); 104 if (closedTimeStr == null) { //no closed_at means the note is still open 105 thisNote.setState(Note.State.OPEN); 106 } else { 107 thisNote.setState(Note.State.CLOSED); 108 thisNote.setClosedAt(DateUtils.fromString(closedTimeStr)); 109 } 110 thisNote.setCreatedAt(DateUtils.fromString(attrs.getValue("created_at"))); 96 thisNote = parseNoteFull(attrs); 111 97 break; 112 98 case "comment": … … 188 174 parsedNotes = notes; 189 175 } 176 } 177 178 static LatLon parseLatLon(Function<String, String> attrs) { 179 double lat = Double.parseDouble(attrs.apply("lat")); 180 double lon = Double.parseDouble(attrs.apply("lon")); 181 return new LatLon(lat, lon); 182 } 183 184 static Note parseNoteBasic(Attributes attrs) { 185 return parseNoteBasic(attrs::getValue); 186 } 187 188 static Note parseNoteBasic(Function<String, String> attrs) { 189 return new Note(parseLatLon(attrs)); 190 } 191 192 static Note parseNoteFull(Attributes attrs) { 193 return parseNoteFull(attrs::getValue); 194 } 195 196 static Note parseNoteFull(Function<String, String> attrs) { 197 Note note = parseNoteBasic(attrs); 198 String id = attrs.apply("id"); 199 if (id != null) { 200 note.setId(Long.parseLong(id)); 201 } 202 String closedTimeStr = attrs.apply("closed_at"); 203 if (closedTimeStr == null) { //no closed_at means the note is still open 204 note.setState(Note.State.OPEN); 205 } else { 206 note.setState(Note.State.CLOSED); 207 note.setClosedAt(DateUtils.fromString(closedTimeStr)); 208 } 209 String createdAt = attrs.apply("created_at"); 210 if (createdAt != null) { 211 note.setCreatedAt(DateUtils.fromString(createdAt)); 212 } 213 return note; 190 214 } 191 215 -
trunk/src/org/openstreetmap/josm/io/OsmChangeReader.java
r8415 r14101 10 10 import javax.xml.stream.XMLStreamException; 11 11 12 import org.openstreetmap.josm.data.coor.LatLon; 12 13 import org.openstreetmap.josm.data.osm.DataSet; 14 import org.openstreetmap.josm.data.osm.NoteData; 13 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 16 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 17 import org.openstreetmap.josm.tools.Pair; 15 18 16 19 /** … … 23 26 */ 24 27 private static final String[] ACTIONS = {"create", "modify", "delete"}; 28 29 protected final NoteData noteData = new NoteData(); 25 30 26 31 /** … … 80 85 p = parseRelation(); 81 86 break; 87 case "note": 88 parseNote(); 89 break; 82 90 default: 83 91 parseUnknown(); … … 96 104 } 97 105 106 private void parseNote() throws XMLStreamException { 107 LatLon location = NoteReader.parseLatLon(s -> parser.getAttributeValue(null, s)); 108 String text = null; 109 while (parser.hasNext()) { 110 int event = parser.next(); 111 if (event == XMLStreamConstants.START_ELEMENT) { 112 switch (parser.getLocalName()) { 113 case "comment": 114 text = parser.getAttributeValue(null, "text"); 115 break; 116 default: 117 parseUnknown(); 118 } 119 } else if (event == XMLStreamConstants.END_ELEMENT) { 120 break; 121 } 122 } 123 if (location != null && text != null) { 124 noteData.createNote(location, text); 125 } 126 } 127 128 /** 129 * Replies the parsed notes data. 130 * @return the parsed notes data 131 * @since 14101 132 */ 133 public final NoteData getNoteData() { 134 return noteData; 135 } 136 98 137 /** 99 138 * Parse the given input source and return the dataset. … … 110 149 return new OsmChangeReader().doParseDataSet(source, progressMonitor); 111 150 } 151 152 /** 153 * Parse the given input source and return the dataset and notes, if any (OsmAnd extends the osmChange format by adding notes). 154 * 155 * @param source the source input stream. Must not be <code>null</code>. 156 * @param progressMonitor the progress monitor. If <code>null</code>, 157 * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed 158 * 159 * @return the dataset with the parsed data 160 * @throws IllegalDataException if the an error was found while parsing the data from the source 161 * @throws IllegalArgumentException if source is <code>null</code> 162 * @since 14101 163 */ 164 public static Pair<DataSet, NoteData> parseDataSetAndNotes(InputStream source, ProgressMonitor progressMonitor) 165 throws IllegalDataException { 166 OsmChangeReader osmChangeReader = new OsmChangeReader(); 167 osmChangeReader.doParseDataSet(source, progressMonitor); 168 return new Pair<>(osmChangeReader.getDataSet(), osmChangeReader.getNoteData()); 169 } 112 170 }
Note:
See TracChangeset
for help on using the changeset viewer.