source: josm/branch/0.5/src/org/openstreetmap/josm/io/OsmIdReader.java@ 329

Last change on this file since 329 was 329, checked in by framm, 17 years ago

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

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")) {
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.