1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 |
|
---|
9 | import javax.xml.xpath.XPath;
|
---|
10 | import javax.xml.xpath.XPathConstants;
|
---|
11 | import javax.xml.xpath.XPathExpression;
|
---|
12 | import javax.xml.xpath.XPathExpressionException;
|
---|
13 | import javax.xml.xpath.XPathFactory;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
18 | import org.openstreetmap.josm.io.OsmImporter;
|
---|
19 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
---|
20 | import org.w3c.dom.Element;
|
---|
21 |
|
---|
22 | public class OsmDataSessionImporter implements SessionLayerImporter {
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
26 | String version = elem.getAttribute("version");
|
---|
27 | if (!"0.1".equals(version)) {
|
---|
28 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
|
---|
29 | }
|
---|
30 | try {
|
---|
31 | XPathFactory xPathFactory = XPathFactory.newInstance();
|
---|
32 | XPath xpath = xPathFactory.newXPath();
|
---|
33 | XPathExpression fileExp = xpath.compile("file/text()");
|
---|
34 | String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
|
---|
35 | if (fileStr == null || fileStr.isEmpty()) {
|
---|
36 | throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
|
---|
37 | }
|
---|
38 |
|
---|
39 | OsmImporter importer = new OsmImporter();
|
---|
40 | try (InputStream in = support.getInputStream(fileStr)) {
|
---|
41 | OsmImporter.OsmImporterData importData = importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(),
|
---|
42 | progressMonitor);
|
---|
43 |
|
---|
44 | support.addPostLayersTask(importData.getPostLayerTask());
|
---|
45 | return importData.getLayer();
|
---|
46 | }
|
---|
47 | } catch (XPathExpressionException e) {
|
---|
48 | throw new IllegalDataException(e);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|