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