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 | import java.util.List;
|
---|
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.gui.layer.GpxLayer;
|
---|
17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.io.GpxImporter;
|
---|
21 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
22 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
---|
23 | import org.w3c.dom.Element;
|
---|
24 |
|
---|
25 | public class MarkerSessionImporter implements SessionLayerImporter {
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
29 | String version = elem.getAttribute("version");
|
---|
30 | if (!"0.1".equals(version)) {
|
---|
31 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for marker layer is not supported. Expected: 0.1", version));
|
---|
32 | }
|
---|
33 | try {
|
---|
34 | XPathFactory xPathFactory = XPathFactory.newInstance();
|
---|
35 | XPath xpath = xPathFactory.newXPath();
|
---|
36 | XPathExpression fileExp = xpath.compile("file/text()");
|
---|
37 | String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
|
---|
38 | if (fileStr == null || fileStr.isEmpty()) {
|
---|
39 | throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
|
---|
40 | }
|
---|
41 |
|
---|
42 | try (InputStream in = support.getInputStream(fileStr)) {
|
---|
43 | GpxImporter.GpxImporterData importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), null, progressMonitor);
|
---|
44 |
|
---|
45 | support.addPostLayersTask(importData.getPostLayerTask());
|
---|
46 |
|
---|
47 | GpxLayer gpxLayer = null;
|
---|
48 | List<SessionReader.LayerDependency> deps = support.getLayerDependencies();
|
---|
49 | if (!deps.isEmpty()) {
|
---|
50 | Layer layer = deps.iterator().next().getLayer();
|
---|
51 | if (layer instanceof GpxLayer) {
|
---|
52 | gpxLayer = (GpxLayer) layer;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | MarkerLayer markerLayer = importData.getMarkerLayer();
|
---|
57 | if (markerLayer != null) {
|
---|
58 | markerLayer.fromLayer = gpxLayer;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return markerLayer;
|
---|
62 | }
|
---|
63 | } catch (XPathExpressionException e) {
|
---|
64 | throw new IllegalDataException(e);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|