source: josm/trunk/src/org/openstreetmap/josm/io/OsmIdReader.java@ 729

Last change on this file since 729 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.InputStreamReader;
9import java.io.Reader;
10import java.util.HashMap;
11import java.util.Map;
12
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.parsers.SAXParserFactory;
15
16import org.xml.sax.Attributes;
17import org.xml.sax.InputSource;
18import org.xml.sax.SAXException;
19import org.xml.sax.helpers.DefaultHandler;
20
21/**
22 * Read only the ids and classes of an stream.
23 *
24 * @author Imi
25 */
26public class OsmIdReader extends DefaultHandler {
27
28 private boolean cancel;
29 Map<Long, String> entries = new HashMap<Long, String>();
30 private Reader in;
31
32 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
33 if (qName.equals("node") || qName.equals("way") || qName.equals("relation")) {
34 try {
35 entries.put(Long.valueOf(atts.getValue("id")), qName);
36 } catch (Exception e) {
37 e.printStackTrace();
38 throw new SAXException(tr("Error during parse."));
39 }
40 }
41 }
42
43 public Map<Long, String> parseIds(InputStream in) throws IOException, SAXException {
44 this.in = new InputStreamReader(in, "UTF-8");
45 try {
46 SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(this.in), this);
47 } catch (ParserConfigurationException e) {
48 if (!cancel) {
49 e.printStackTrace(); // broken SAXException chaining
50 throw new SAXException(e);
51 }
52 } catch (SAXException e) {
53 if (!cancel)
54 throw e;
55 }
56 return entries;
57 }
58
59 public void cancel() {
60 cancel = true;
61 if (in != null)
62 try {in.close();} catch (IOException e) {}
63 }
64}
Note: See TracBrowser for help on using the repository browser.