source: josm/trunk/src/org/openstreetmap/josm/io/NoteReader.java@ 14180

Last change on this file since 14180 was 14101, checked in by Don-vip, 6 years ago

see #8765, fix #11086 - Support notes in osmChange (.osc) files created by OsmAnd

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.ByteArrayInputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.nio.charset.StandardCharsets;
8import java.util.ArrayList;
9import java.util.Date;
10import java.util.List;
11import java.util.Locale;
12import java.util.Optional;
13import java.util.function.Function;
14
15import javax.xml.parsers.ParserConfigurationException;
16
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.notes.Note;
19import org.openstreetmap.josm.data.notes.NoteComment;
20import org.openstreetmap.josm.data.notes.NoteComment.Action;
21import org.openstreetmap.josm.data.osm.User;
22import org.openstreetmap.josm.tools.Logging;
23import org.openstreetmap.josm.tools.XmlUtils;
24import org.openstreetmap.josm.tools.date.DateUtils;
25import org.xml.sax.Attributes;
26import org.xml.sax.InputSource;
27import org.xml.sax.SAXException;
28import org.xml.sax.helpers.DefaultHandler;
29
30/**
31 * Class to read Note objects from their XML representation. It can take
32 * either API style XML which starts with an "osm" tag or a planet dump
33 * style XML which starts with an "osm-notes" tag.
34 */
35public class NoteReader {
36
37 private final InputSource inputSource;
38 private List<Note> parsedNotes;
39
40 /**
41 * Notes can be represented in two XML formats. One is returned by the API
42 * while the other is used to generate the notes dump file. The parser
43 * needs to know which one it is handling.
44 */
45 private enum NoteParseMode {
46 API,
47 DUMP
48 }
49
50 /**
51 * SAX handler to read note information from its XML representation.
52 * Reads both API style and planet dump style formats.
53 */
54 private class Parser extends DefaultHandler {
55
56 private NoteParseMode parseMode;
57 private final StringBuilder buffer = new StringBuilder();
58 private Note thisNote;
59 private long commentUid;
60 private String commentUsername;
61 private Action noteAction;
62 private Date commentCreateDate;
63 private boolean commentIsNew;
64 private List<Note> notes;
65 private String commentText;
66
67 @Override
68 public void characters(char[] ch, int start, int length) throws SAXException {
69 buffer.append(ch, start, length);
70 }
71
72 @Override
73 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
74 buffer.setLength(0);
75 switch(qName) {
76 case "osm":
77 parseMode = NoteParseMode.API;
78 notes = new ArrayList<>(100);
79 return;
80 case "osm-notes":
81 parseMode = NoteParseMode.DUMP;
82 notes = new ArrayList<>(10_000);
83 return;
84 }
85
86 if (parseMode == NoteParseMode.API) {
87 if ("note".equals(qName)) {
88 thisNote = parseNoteBasic(attrs);
89 }
90 return;
91 }
92
93 //The rest only applies for dump mode
94 switch(qName) {
95 case "note":
96 thisNote = parseNoteFull(attrs);
97 break;
98 case "comment":
99 commentUid = Long.parseLong(Optional.ofNullable(attrs.getValue("uid")).orElse("0"));
100 commentUsername = attrs.getValue("user");
101 noteAction = Action.valueOf(attrs.getValue("action").toUpperCase(Locale.ENGLISH));
102 commentCreateDate = DateUtils.fromString(attrs.getValue("timestamp"));
103 commentIsNew = Boolean.parseBoolean(Optional.ofNullable(attrs.getValue("is_new")).orElse("false"));
104 break;
105 default: // Do nothing
106 }
107 }
108
109 @Override
110 public void endElement(String namespaceURI, String localName, String qName) {
111 if (notes != null && "note".equals(qName)) {
112 notes.add(thisNote);
113 }
114 if ("comment".equals(qName)) {
115 User commentUser = User.createOsmUser(commentUid, commentUsername);
116 if (commentUid == 0) {
117 commentUser = User.getAnonymous();
118 }
119 if (parseMode == NoteParseMode.API) {
120 commentIsNew = false;
121 }
122 if (parseMode == NoteParseMode.DUMP) {
123 commentText = buffer.toString();
124 }
125 thisNote.addComment(new NoteComment(commentCreateDate, commentUser, commentText, noteAction, commentIsNew));
126 commentUid = 0;
127 commentUsername = null;
128 commentCreateDate = null;
129 commentIsNew = false;
130 commentText = null;
131 }
132 if (parseMode == NoteParseMode.DUMP) {
133 return;
134 }
135
136 //the rest only applies to API mode
137 switch (qName) {
138 case "id":
139 thisNote.setId(Long.parseLong(buffer.toString()));
140 break;
141 case "status":
142 thisNote.setState(Note.State.valueOf(buffer.toString().toUpperCase(Locale.ENGLISH)));
143 break;
144 case "date_created":
145 thisNote.setCreatedAt(DateUtils.fromString(buffer.toString()));
146 break;
147 case "date_closed":
148 thisNote.setClosedAt(DateUtils.fromString(buffer.toString()));
149 break;
150 case "date":
151 commentCreateDate = DateUtils.fromString(buffer.toString());
152 break;
153 case "user":
154 commentUsername = buffer.toString();
155 break;
156 case "uid":
157 commentUid = Long.parseLong(buffer.toString());
158 break;
159 case "text":
160 commentText = buffer.toString();
161 buffer.setLength(0);
162 break;
163 case "action":
164 noteAction = Action.valueOf(buffer.toString().toUpperCase(Locale.ENGLISH));
165 break;
166 case "note": //nothing to do for comment or note, already handled above
167 case "comment":
168 break;
169 }
170 }
171
172 @Override
173 public void endDocument() throws SAXException {
174 parsedNotes = notes;
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;
214 }
215
216 /**
217 * Initializes the reader with a given InputStream
218 * @param source - InputStream containing Notes XML
219 */
220 public NoteReader(InputStream source) {
221 this.inputSource = new InputSource(source);
222 }
223
224 /**
225 * Initializes the reader with a string as a source
226 * @param source UTF-8 string containing Notes XML to parse
227 */
228 public NoteReader(String source) {
229 this.inputSource = new InputSource(new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
230 }
231
232 /**
233 * Parses the InputStream given to the constructor and returns
234 * the resulting Note objects
235 * @return List of Notes parsed from the input data
236 * @throws SAXException if any SAX parsing error occurs
237 * @throws IOException if any I/O error occurs
238 */
239 public List<Note> parse() throws SAXException, IOException {
240 DefaultHandler parser = new Parser();
241 try {
242 XmlUtils.parseSafeSAX(inputSource, parser);
243 } catch (ParserConfigurationException e) {
244 Logging.error(e); // broken SAXException chaining
245 throw new SAXException(e);
246 }
247 return parsedNotes;
248 }
249}
Note: See TracBrowser for help on using the repository browser.