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.w3c.dom.Element;
016
017 import org.openstreetmap.josm.gui.layer.Layer;
018 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
019 import org.openstreetmap.josm.io.IllegalDataException;
020 import org.openstreetmap.josm.io.OsmImporter;
021 import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
022
023 public class OsmDataSessionImporter implements SessionLayerImporter {
024
025 @Override
026 public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) 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 osm data 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.equals("")) {
037 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
038 }
039
040 OsmImporter importer = new OsmImporter();
041 InputStream in = support.getInputStream(fileStr);
042 OsmImporter.OsmImporterData importData = importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
043
044 support.addPostLayersTask(importData.getPostLayerTask());
045 return importData.getLayer();
046
047 } catch (XPathExpressionException e) {
048 throw new RuntimeException(e);
049 }
050 }
051 }
052