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

Last change on this file since 9615 was 9569, checked in by Don-vip, 8 years ago

fix #12393 - Fix parsing note hiding events (patch by ToeBee) + add non-regression unit test

  • Property svn:eol-style set to native
File size: 8.5 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;
11
12import javax.xml.parsers.ParserConfigurationException;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.notes.Note;
17import org.openstreetmap.josm.data.notes.NoteComment;
18import org.openstreetmap.josm.data.notes.NoteComment.Action;
19import org.openstreetmap.josm.data.osm.User;
20import org.openstreetmap.josm.tools.Utils;
21import org.openstreetmap.josm.tools.date.DateUtils;
22import org.xml.sax.Attributes;
23import org.xml.sax.InputSource;
24import org.xml.sax.SAXException;
25import org.xml.sax.helpers.DefaultHandler;
26
27/**
28 * Class to read Note objects from their XML representation. It can take
29 * either API style XML which starts with an "osm" tag or a planet dump
30 * style XML which starts with an "osm-notes" tag.
31 */
32public class NoteReader {
33
34 private final InputSource inputSource;
35 private List<Note> parsedNotes;
36
37 /**
38 * Notes can be represented in two XML formats. One is returned by the API
39 * while the other is used to generate the notes dump file. The parser
40 * needs to know which one it is handling.
41 */
42 private enum NoteParseMode {
43 API,
44 DUMP
45 }
46
47 /**
48 * SAX handler to read note information from its XML representation.
49 * Reads both API style and planet dump style formats.
50 */
51 private class Parser extends DefaultHandler {
52
53 private NoteParseMode parseMode;
54 private final StringBuilder buffer = new StringBuilder();
55 private Note thisNote;
56 private long commentUid;
57 private String commentUsername;
58 private Action noteAction;
59 private Date commentCreateDate;
60 private boolean commentIsNew;
61 private List<Note> notes;
62 private String commentText;
63
64 @Override
65 public void characters(char[] ch, int start, int length) throws SAXException {
66 buffer.append(ch, start, length);
67 }
68
69 @Override
70 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
71 buffer.setLength(0);
72 switch(qName) {
73 case "osm":
74 parseMode = NoteParseMode.API;
75 notes = new ArrayList<>(100);
76 return;
77 case "osm-notes":
78 parseMode = NoteParseMode.DUMP;
79 notes = new ArrayList<>(10000);
80 return;
81 }
82
83 if (parseMode == NoteParseMode.API) {
84 if ("note".equals(qName)) {
85 double lat = Double.parseDouble(attrs.getValue("lat"));
86 double lon = Double.parseDouble(attrs.getValue("lon"));
87 LatLon noteLatLon = new LatLon(lat, lon);
88 thisNote = new Note(noteLatLon);
89 }
90 return;
91 }
92
93 //The rest only applies for dump mode
94 switch(qName) {
95 case "note":
96 double lat = Double.parseDouble(attrs.getValue("lat"));
97 double lon = Double.parseDouble(attrs.getValue("lon"));
98 LatLon noteLatLon = new LatLon(lat, lon);
99 thisNote = new Note(noteLatLon);
100 thisNote.setId(Long.parseLong(attrs.getValue("id")));
101 String closedTimeStr = attrs.getValue("closed_at");
102 if (closedTimeStr == null) { //no closed_at means the note is still open
103 thisNote.setState(Note.State.open);
104 } else {
105 thisNote.setState(Note.State.closed);
106 thisNote.setClosedAt(DateUtils.fromString(closedTimeStr));
107 }
108 thisNote.setCreatedAt(DateUtils.fromString(attrs.getValue("created_at")));
109 break;
110 case "comment":
111 String uidStr = attrs.getValue("uid");
112 if (uidStr == null) {
113 commentUid = 0;
114 } else {
115 commentUid = Long.parseLong(uidStr);
116 }
117 commentUsername = attrs.getValue("user");
118 noteAction = Action.valueOf(attrs.getValue("action"));
119 commentCreateDate = DateUtils.fromString(attrs.getValue("timestamp"));
120 String isNew = attrs.getValue("is_new");
121 if (isNew == null) {
122 commentIsNew = false;
123 } else {
124 commentIsNew = Boolean.parseBoolean(isNew);
125 }
126 break;
127 }
128 }
129
130 @Override
131 public void endElement(String namespaceURI, String localName, String qName) {
132 if (notes != null && "note".equals(qName)) {
133 notes.add(thisNote);
134 }
135 if ("comment".equals(qName)) {
136 User commentUser = User.createOsmUser(commentUid, commentUsername);
137 if (commentUid == 0) {
138 commentUser = User.getAnonymous();
139 }
140 if (parseMode == NoteParseMode.API) {
141 commentIsNew = false;
142 }
143 if (parseMode == NoteParseMode.DUMP) {
144 commentText = buffer.toString();
145 }
146 thisNote.addComment(new NoteComment(commentCreateDate, commentUser, commentText, noteAction, commentIsNew));
147 commentUid = 0;
148 commentUsername = null;
149 commentCreateDate = null;
150 commentIsNew = false;
151 commentText = null;
152 }
153 if (parseMode == NoteParseMode.DUMP) {
154 return;
155 }
156
157 //the rest only applies to API mode
158 switch (qName) {
159 case "id":
160 thisNote.setId(Long.parseLong(buffer.toString()));
161 break;
162 case "status":
163 thisNote.setState(Note.State.valueOf(buffer.toString()));
164 break;
165 case "date_created":
166 thisNote.setCreatedAt(DateUtils.fromString(buffer.toString()));
167 break;
168 case "date_closed":
169 thisNote.setClosedAt(DateUtils.fromString(buffer.toString()));
170 break;
171 case "date":
172 commentCreateDate = DateUtils.fromString(buffer.toString());
173 break;
174 case "user":
175 commentUsername = buffer.toString();
176 break;
177 case "uid":
178 commentUid = Long.parseLong(buffer.toString());
179 break;
180 case "text":
181 commentText = buffer.toString();
182 buffer.setLength(0);
183 break;
184 case "action":
185 noteAction = Action.valueOf(buffer.toString());
186 break;
187 case "note": //nothing to do for comment or note, already handled above
188 case "comment":
189 break;
190 }
191 }
192
193 @Override
194 public void endDocument() throws SAXException {
195 parsedNotes = notes;
196 }
197 }
198
199 /**
200 * Initializes the reader with a given InputStream
201 * @param source - InputStream containing Notes XML
202 * @throws IOException if any I/O error occurs
203 */
204 public NoteReader(InputStream source) throws IOException {
205 this.inputSource = new InputSource(source);
206 }
207
208 /**
209 * Initializes the reader with a string as a source
210 * @param source UTF-8 string containing Notes XML to parse
211 * @throws IOException if any I/O error occurs
212 */
213 public NoteReader(String source) throws IOException {
214 this.inputSource = new InputSource(new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
215 }
216
217 /**
218 * Parses the InputStream given to the constructor and returns
219 * the resulting Note objects
220 * @return List of Notes parsed from the input data
221 * @throws SAXException if any SAX parsing error occurs
222 * @throws IOException if any I/O error occurs
223 */
224 public List<Note> parse() throws SAXException, IOException {
225 DefaultHandler parser = new Parser();
226 try {
227 Utils.parseSafeSAX(inputSource, parser);
228 } catch (ParserConfigurationException e) {
229 Main.error(e); // broken SAXException chaining
230 throw new SAXException(e);
231 }
232 return parsedNotes;
233 }
234}
Note: See TracBrowser for help on using the repository browser.