001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io.session; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.IOException; 007import java.io.InputStream; 008 009import javax.xml.xpath.XPath; 010import javax.xml.xpath.XPathConstants; 011import javax.xml.xpath.XPathExpression; 012import javax.xml.xpath.XPathExpressionException; 013import javax.xml.xpath.XPathFactory; 014 015import org.openstreetmap.josm.gui.layer.Layer; 016import org.openstreetmap.josm.gui.progress.ProgressMonitor; 017import org.openstreetmap.josm.io.GpxImporter; 018import org.openstreetmap.josm.io.IllegalDataException; 019import org.openstreetmap.josm.io.NMEAImporter; 020import org.w3c.dom.Element; 021 022public class GpxTracksSessionImporter implements SessionLayerImporter { 023 024 @Override 025 public Layer load(Element elem, SessionReader.ImportSupport support, ProgressMonitor progressMonitor) 026 throws IOException, IllegalDataException { 027 String version = elem.getAttribute("version"); 028 if (!"0.1".equals(version)) { 029 throw new IllegalDataException(tr("Version ''{0}'' of meta data for gpx track layer is not supported. Expected: 0.1", version)); 030 } 031 try { 032 XPathFactory xPathFactory = XPathFactory.newInstance(); 033 XPath xpath = xPathFactory.newXPath(); 034 XPathExpression fileExp = xpath.compile("file/text()"); 035 String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING); 036 if (fileStr == null || fileStr.isEmpty()) { 037 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex())); 038 } 039 040 try (InputStream in = support.getInputStream(fileStr)) { 041 GpxImporter.GpxImporterData importData = null; 042 043 if (NMEAImporter.FILE_FILTER.acceptName(fileStr)) { 044 importData = NMEAImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), null); 045 } else { 046 importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), null, progressMonitor); 047 } 048 049 support.addPostLayersTask(importData.getPostLayerTask()); 050 return importData.getGpxLayer(); 051 } 052 053 } catch (XPathExpressionException e) { 054 throw new IllegalDataException(e); 055 } 056 } 057}