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