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

Last change on this file since 13496 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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