| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.io; |
| 3 | |
| 4 | import java.io.IOException; |
| 5 | import java.io.InputStream; |
| 6 | import java.text.ParseException; |
| 7 | import java.text.SimpleDateFormat; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.Date; |
| 10 | import java.util.List; |
| 11 | import java.util.Locale; |
| 12 | |
| 13 | import javax.xml.parsers.ParserConfigurationException; |
| 14 | import javax.xml.parsers.SAXParserFactory; |
| 15 | |
| 16 | import org.openstreetmap.josm.Main; |
| 17 | import org.openstreetmap.josm.data.coor.LatLon; |
| 18 | import org.openstreetmap.josm.data.notes.Note; |
| 19 | import org.openstreetmap.josm.data.notes.NoteComment; |
| 20 | import org.openstreetmap.josm.data.notes.NoteComment.Action; |
| 21 | import org.openstreetmap.josm.data.osm.User; |
| 22 | import org.xml.sax.Attributes; |
| 23 | import org.xml.sax.InputSource; |
| 24 | import org.xml.sax.SAXException; |
| 25 | import org.xml.sax.helpers.DefaultHandler; |
| 26 | |
| 27 | /** |
| 28 | * Class to read Note objects from their XML representation |
| 29 | */ |
| 30 | public class NoteReader { |
| 31 | |
| 32 | private InputSource inputSource; |
| 33 | private List<Note> parsedNotes; |
| 34 | private NoteParseMode parseMode; |
| 35 | |
| 36 | /** |
| 37 | * Notes can be represented in two XML formats. One is returned by the API |
| 38 | * while the other is used to generate the notes dump file. The parser |
| 39 | * needs to know which one it is handling. |
| 40 | */ |
| 41 | public enum NoteParseMode {API, DUMP} |
| 42 | |
| 43 | /** |
| 44 | * Parser for the notes dump file format. |
| 45 | * It is completely different from the API XML format. |
| 46 | */ |
| 47 | private class DumpParser extends DefaultHandler { |
| 48 | private StringBuffer buffer = new StringBuffer(); |
| 49 | private final SimpleDateFormat ISO8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.ENGLISH); |
| 50 | |
| 51 | private List<Note> notes = new ArrayList<Note>(100000); |
| 52 | private Note thisNote; |
| 53 | |
| 54 | private Date commentCreateDate; |
| 55 | private String commentUsername; |
| 56 | private long commentUid; |
| 57 | private Action noteAction; |
| 58 | private Boolean commentIsNew; |
| 59 | |
| 60 | @Override |
| 61 | public void characters(char[] ch, int start, int length) throws SAXException { |
| 62 | buffer.append(ch, start, length); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 67 | switch (qName) { |
| 68 | case "note": |
| 69 | notes.add(thisNote); |
| 70 | break; |
| 71 | case "comment": |
| 72 | User commentUser = User.createOsmUser(commentUid, commentUsername); |
| 73 | thisNote.addComment(new NoteComment(commentCreateDate, commentUser, buffer.toString(), noteAction, commentIsNew)); |
| 74 | commentUid = 0; |
| 75 | commentUsername = null; |
| 76 | commentCreateDate = null; |
| 77 | commentIsNew = null; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 84 | buffer.setLength(0); |
| 85 | switch(qName) { |
| 86 | case "note": |
| 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 | thisNote.setId(Long.parseLong(attrs.getValue("id"))); |
| 92 | String closedTimeStr = attrs.getValue("closed_at"); |
| 93 | if(closedTimeStr == null) { //no closed_at means the note is still open |
| 94 | thisNote.setState(Note.State.open); |
| 95 | } else { |
| 96 | thisNote.setState(Note.State.closed); |
| 97 | thisNote.setClosedAt(parseDate(ISO8601_FORMAT, closedTimeStr)); |
| 98 | } |
| 99 | thisNote.setCreatedAt(parseDate(ISO8601_FORMAT, attrs.getValue("created_at"))); |
| 100 | break; |
| 101 | case "comment": |
| 102 | String uidStr = attrs.getValue("uid"); |
| 103 | if(uidStr == null) { |
| 104 | commentUid = 0; |
| 105 | } else { |
| 106 | commentUid = Long.parseLong(uidStr); |
| 107 | } |
| 108 | commentUsername = attrs.getValue("user"); |
| 109 | noteAction = Action.valueOf(attrs.getValue("action")); |
| 110 | commentCreateDate = parseDate(ISO8601_FORMAT, attrs.getValue("timestamp")); |
| 111 | String isNew = attrs.getValue("is_new"); |
| 112 | if(isNew == null) { |
| 113 | commentIsNew = false; |
| 114 | } else { |
| 115 | commentIsNew = Boolean.valueOf(isNew); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void endDocument() throws SAXException { |
| 123 | Main.info("parsed notes: " + notes.size()); |
| 124 | parsedNotes = notes; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private class ApiParser extends DefaultHandler { |
| 129 | |
| 130 | private StringBuffer accumulator = new StringBuffer(); |
| 131 | private final SimpleDateFormat NOTE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.ENGLISH); |
| 132 | |
| 133 | private List<Note> notes = new ArrayList<Note>(); |
| 134 | private Note thisNote; |
| 135 | |
| 136 | private Date commentCreateDate; |
| 137 | private String commentUsername; |
| 138 | private long commentUid; |
| 139 | private String commentText; |
| 140 | private Action commentAction; |
| 141 | |
| 142 | @Override |
| 143 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { |
| 144 | accumulator.setLength(0); |
| 145 | if ("note".equals(qName)) { |
| 146 | double lat = Double.parseDouble(atts.getValue("lat")); |
| 147 | double lon = Double.parseDouble(atts.getValue("lon")); |
| 148 | LatLon noteLatLon = new LatLon(lat, lon); |
| 149 | thisNote = new Note(noteLatLon); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void characters(char[] ch, int start, int length) { |
| 155 | accumulator.append(ch, start, length); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void endElement(String namespaceURI, String localName, String qName) { |
| 160 | switch (qName) { |
| 161 | case "id": |
| 162 | thisNote.setId(Long.parseLong(accumulator.toString())); |
| 163 | break; |
| 164 | case "status": |
| 165 | thisNote.setState(Note.State.valueOf(accumulator.toString())); |
| 166 | break; |
| 167 | case "date_created": |
| 168 | thisNote.setCreatedAt(parseDate(NOTE_DATE_FORMAT, accumulator.toString())); |
| 169 | break; |
| 170 | case "note": |
| 171 | notes.add(thisNote); |
| 172 | break; |
| 173 | case "date": |
| 174 | commentCreateDate = parseDate(NOTE_DATE_FORMAT, accumulator.toString()); |
| 175 | break; |
| 176 | case "user": |
| 177 | commentUsername = accumulator.toString(); |
| 178 | break; |
| 179 | case "uid": |
| 180 | commentUid = Long.parseLong(accumulator.toString()); |
| 181 | break; |
| 182 | case "text": |
| 183 | commentText = accumulator.toString(); |
| 184 | break; |
| 185 | case "comment": |
| 186 | User commentUser = User.createOsmUser(commentUid, commentUsername); |
| 187 | thisNote.addComment(new NoteComment(commentCreateDate, commentUser, commentText, commentAction, false)); |
| 188 | commentUid = 0; |
| 189 | commentUsername = null; |
| 190 | commentCreateDate = null; |
| 191 | commentText = null; |
| 192 | break; |
| 193 | case "action": |
| 194 | commentAction = Action.valueOf(accumulator.toString()); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void endDocument() throws SAXException { |
| 201 | Main.info("parsed notes: " + notes.size()); |
| 202 | parsedNotes = notes; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Convenience method to handle the date parsing try/catch. Will return null if |
| 208 | * there is a parsing exception. This means whatever generated this XML is in error |
| 209 | * and there isn't anything we can do about it. |
| 210 | * @param dateStr - String to parse |
| 211 | * @return Parsed date, null if parsing fails |
| 212 | */ |
| 213 | private Date parseDate(SimpleDateFormat sdf, String dateStr) { |
| 214 | try { |
| 215 | return sdf.parse(dateStr); |
| 216 | } catch(ParseException e) { |
| 217 | Main.error("error parsing date in note parser"); |
| 218 | return null; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Initializes the reader with a given InputStream |
| 224 | * @param source - InputStream containing Notes XML |
| 225 | * @param parseMode - Indicate if we are parsing API or dump file style XML |
| 226 | * @throws IOException |
| 227 | */ |
| 228 | public NoteReader(InputStream source, NoteParseMode parseMode) throws IOException { |
| 229 | this.inputSource = new InputSource(source); |
| 230 | this.parseMode = parseMode; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Parses the InputStream given to the constructor and returns |
| 235 | * the resulting Note objects |
| 236 | * @return List of Notes parsed from the input data |
| 237 | * @throws SAXException |
| 238 | * @throws IOException |
| 239 | */ |
| 240 | public List<Note> parse() throws SAXException, IOException { |
| 241 | DefaultHandler parser; |
| 242 | if(parseMode == NoteParseMode.DUMP) { |
| 243 | parser = new DumpParser(); |
| 244 | } else { |
| 245 | parser = new ApiParser(); |
| 246 | } |
| 247 | try { |
| 248 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 249 | factory.setNamespaceAware(true); |
| 250 | factory.newSAXParser().parse(inputSource, parser); |
| 251 | } catch (ParserConfigurationException e) { |
| 252 | Main.error(e); // broken SAXException chaining |
| 253 | throw new SAXException(e); |
| 254 | } |
| 255 | return parsedNotes; |
| 256 | } |
| 257 | } |