source: josm/trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionImporter.java@ 9455

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

session - refactor duplicated code, add javadoc, fix warnings

  • Property svn:eol-style set to native
File size: 2.3 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.Layer;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.IllegalDataException;
19import org.openstreetmap.josm.io.OsmImporter;
20import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
21import org.w3c.dom.Element;
22
23/**
24 * Session importer for {@link OsmDataLayer}.
25 * @since 4685
26 */
27public class OsmDataSessionImporter implements SessionLayerImporter {
28
29 @Override
30 public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
31 String version = elem.getAttribute("version");
32 if (!"0.1".equals(version)) {
33 throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
34 }
35 try {
36 XPathFactory xPathFactory = XPathFactory.newInstance();
37 XPath xpath = xPathFactory.newXPath();
38 XPathExpression fileExp = xpath.compile("file/text()");
39 String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
40 if (fileStr == null || fileStr.isEmpty()) {
41 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
42 }
43
44 OsmImporter importer = new OsmImporter();
45 try (InputStream in = support.getInputStream(fileStr)) {
46 OsmImporter.OsmImporterData importData = importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(),
47 progressMonitor);
48
49 support.addPostLayersTask(importData.getPostLayerTask());
50 return importData.getLayer();
51 }
52 } catch (XPathExpressionException e) {
53 throw new IllegalDataException(e);
54 }
55 }
56}
Note: See TracBrowser for help on using the repository browser.