source: josm/trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionImporter.java@ 9471

Last change on this file since 9471 was 9455, checked in by Don-vip, 8 years ago

session - refactor duplicated code, add javadoc, fix warnings

  • Property svn:eol-style set to native
File size: 2.4 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;
8
9import javax.xml.xpath.XPath;
10import javax.xml.xpath.XPathConstants;
11import javax.xml.xpath.XPathExpression;
12import javax.xml.xpath.XPathExpressionException;
13import javax.xml.xpath.XPathFactory;
14
15import org.openstreetmap.josm.gui.layer.GpxLayer;
16import org.openstreetmap.josm.gui.layer.Layer;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.GpxImporter;
19import org.openstreetmap.josm.io.IllegalDataException;
20import org.openstreetmap.josm.io.NMEAImporter;
21import org.w3c.dom.Element;
22
23/**
24 * Session exporter for {@link GpxLayer}.
25 * @since 5501
26 */
27public class GpxTracksSessionImporter implements SessionLayerImporter {
28
29 @Override
30 public Layer load(Element elem, SessionReader.ImportSupport support, ProgressMonitor progressMonitor)
31 throws IOException, IllegalDataException {
32 String version = elem.getAttribute("version");
33 if (!"0.1".equals(version)) {
34 throw new IllegalDataException(tr("Version ''{0}'' of meta data for gpx track layer is not supported. Expected: 0.1", version));
35 }
36 try {
37 XPathFactory xPathFactory = XPathFactory.newInstance();
38 XPath xpath = xPathFactory.newXPath();
39 XPathExpression fileExp = xpath.compile("file/text()");
40 String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
41 if (fileStr == null || fileStr.isEmpty()) {
42 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
43 }
44
45 try (InputStream in = support.getInputStream(fileStr)) {
46 GpxImporter.GpxImporterData importData;
47
48 if (NMEAImporter.FILE_FILTER.acceptName(fileStr)) {
49 importData = NMEAImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), null);
50 } else {
51 importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), null, progressMonitor);
52 }
53
54 support.addPostLayersTask(importData.getPostLayerTask());
55 return importData.getGpxLayer();
56 }
57
58 } catch (XPathExpressionException e) {
59 throw new IllegalDataException(e);
60 }
61 }
62}
Note: See TracBrowser for help on using the repository browser.