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.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 |
|
---|
10 | import javax.xml.xpath.XPath;
|
---|
11 | import javax.xml.xpath.XPathConstants;
|
---|
12 | import javax.xml.xpath.XPathExpression;
|
---|
13 | import javax.xml.xpath.XPathExpressionException;
|
---|
14 | import javax.xml.xpath.XPathFactory;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
17 | import org.openstreetmap.josm.gui.io.importexport.FileImporter;
|
---|
18 | import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
23 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 | import org.w3c.dom.Element;
|
---|
26 | import org.w3c.dom.Node;
|
---|
27 | import org.w3c.dom.NodeList;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Session importer for {@link OsmDataLayer}.
|
---|
31 | * @since 4685
|
---|
32 | */
|
---|
33 | public class OsmDataSessionImporter implements SessionLayerImporter {
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
37 | checkMetaVersion(elem);
|
---|
38 | final String fileStr = extractFileName(elem, support);
|
---|
39 | final File file = new File(fileStr);
|
---|
40 | for (FileImporter importer : ExtensionFileFilter.getImporters()) {
|
---|
41 | if (importer instanceof OsmImporter && importer.acceptFile(file)) {
|
---|
42 | return importData((OsmImporter) importer, support, fileStr, progressMonitor);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | // Fall back to the default OsmImporter, just in case. It will probably fail.
|
---|
46 | return importData(new OsmImporter(), support, fileStr, progressMonitor);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Checks that element defines the expected version number.
|
---|
51 | * @param elem element to check
|
---|
52 | * @throws IllegalDataException if version is not the expected one
|
---|
53 | * @since 15377
|
---|
54 | */
|
---|
55 | public static void checkMetaVersion(Element elem) throws IllegalDataException {
|
---|
56 | String version = elem.getAttribute("version");
|
---|
57 | if (!"0.1".equals(version)) {
|
---|
58 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Extract file name from element.
|
---|
64 | * @param elem element to parse
|
---|
65 | * @param support import/export support
|
---|
66 | * @return file name, if present
|
---|
67 | * @throws IllegalDataException if file name missing or empty
|
---|
68 | * @since 15377
|
---|
69 | */
|
---|
70 | public static String extractFileName(Element elem, ImportSupport support) throws IllegalDataException {
|
---|
71 | try {
|
---|
72 | // see #23427: try first to avoid possibly very slow XPath call
|
---|
73 | NodeList x = elem.getElementsByTagName("file");
|
---|
74 | if (x.getLength() > 0 && x.item(0).getNodeType() == Node.ELEMENT_NODE) {
|
---|
75 | String fileStr = x.item(0).getTextContent();
|
---|
76 | if (!Utils.isEmpty(fileStr))
|
---|
77 | return fileStr;
|
---|
78 | }
|
---|
79 | XPathFactory xPathFactory = XPathFactory.newInstance();
|
---|
80 | XPath xpath = xPathFactory.newXPath();
|
---|
81 | XPathExpression fileExp = xpath.compile("file/text()");
|
---|
82 | String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
|
---|
83 | if (Utils.isEmpty(fileStr)) {
|
---|
84 | throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
|
---|
85 | }
|
---|
86 | return fileStr;
|
---|
87 | } catch (XPathExpressionException e) {
|
---|
88 | throw new IllegalDataException(e);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Import data as a new layer.
|
---|
94 | * @param osmImporter OSM importer
|
---|
95 | * @param support import/export support
|
---|
96 | * @param fileStr file name to import
|
---|
97 | * @param progressMonitor progress monitor
|
---|
98 | * @return new layer
|
---|
99 | * @throws IOException in case of I/O error
|
---|
100 | * @throws IllegalDataException in case of illegal data
|
---|
101 | * @since 15377
|
---|
102 | */
|
---|
103 | public static Layer importData(OsmImporter osmImporter, ImportSupport support, String fileStr, ProgressMonitor progressMonitor)
|
---|
104 | throws IOException, IllegalDataException {
|
---|
105 | try (InputStream in = support.getInputStream(fileStr)) {
|
---|
106 | OsmImporter.OsmImporterData importData = osmImporter.loadLayer(
|
---|
107 | in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
|
---|
108 |
|
---|
109 | support.addPostLayersTask(importData.getPostLayerTask());
|
---|
110 | return importData.getLayer();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|