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