1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.io.Reader;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.xml.parsers.ParserConfigurationException;
|
---|
14 | import javax.xml.parsers.SAXParserFactory;
|
---|
15 |
|
---|
16 | import org.xml.sax.Attributes;
|
---|
17 | import org.xml.sax.InputSource;
|
---|
18 | import org.xml.sax.SAXException;
|
---|
19 | import org.xml.sax.helpers.DefaultHandler;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Read only the ids and classes of an stream.
|
---|
23 | *
|
---|
24 | * @author Imi
|
---|
25 | */
|
---|
26 | public 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 | }
|
---|