source: josm/trunk/src/org/openstreetmap/josm/io/session/MarkerSessionImporter.java@ 7326

Last change on this file since 7326 was 7326, checked in by Don-vip, 10 years ago

fix #10292 - allow to load a session with NMEA file + enhance reading/writing unit tests for sessions

File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.session;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.List;
9
10import javax.xml.xpath.XPath;
11import javax.xml.xpath.XPathConstants;
12import javax.xml.xpath.XPathExpression;
13import javax.xml.xpath.XPathExpressionException;
14import javax.xml.xpath.XPathFactory;
15
16import org.openstreetmap.josm.gui.layer.GpxLayer;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20import org.openstreetmap.josm.io.GpxImporter;
21import org.openstreetmap.josm.io.IllegalDataException;
22import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
23import org.w3c.dom.Element;
24
25public 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}
Note: See TracBrowser for help on using the repository browser.