| 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.io.importexport.OsmImporter;
|
|---|
| 16 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 19 | import org.openstreetmap.josm.io.IllegalDataException;
|
|---|
| 20 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
|---|
| 21 | import org.w3c.dom.Element;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Session importer for {@link OsmDataLayer}.
|
|---|
| 25 | * @since 4685
|
|---|
| 26 | */
|
|---|
| 27 | public class OsmDataSessionImporter implements SessionLayerImporter {
|
|---|
| 28 |
|
|---|
| 29 | @Override
|
|---|
| 30 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
|---|
| 31 | String version = elem.getAttribute("version");
|
|---|
| 32 | if (!"0.1".equals(version)) {
|
|---|
| 33 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
|
|---|
| 34 | }
|
|---|
| 35 | try {
|
|---|
| 36 | XPathFactory xPathFactory = XPathFactory.newInstance();
|
|---|
| 37 | XPath xpath = xPathFactory.newXPath();
|
|---|
| 38 | XPathExpression fileExp = xpath.compile("file/text()");
|
|---|
| 39 | String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
|
|---|
| 40 | if (fileStr == null || fileStr.isEmpty()) {
|
|---|
| 41 | throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | OsmImporter importer = new OsmImporter();
|
|---|
| 45 | try (InputStream in = support.getInputStream(fileStr)) {
|
|---|
| 46 | OsmImporter.OsmImporterData importData = importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(),
|
|---|
| 47 | progressMonitor);
|
|---|
| 48 |
|
|---|
| 49 | support.addPostLayersTask(importData.getPostLayerTask());
|
|---|
| 50 | return importData.getLayer();
|
|---|
| 51 | }
|
|---|
| 52 | } catch (XPathExpressionException e) {
|
|---|
| 53 | throw new IllegalDataException(e);
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|