source: josm/trunk/src/org/openstreetmap/josm/io/session/NoteSessionImporter.java@ 11035

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

fix #12497 - add support for notes in session files

  • Property svn:mime-type set to text/plain
File size: 2.1 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.NoteLayer;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.IllegalDataException;
19import org.openstreetmap.josm.io.NoteImporter;
20import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
21import org.w3c.dom.Element;
22import org.xml.sax.SAXException;
23
24/**
25 * Session importer for {@link NoteLayer}.
26 * @since 9746
27 */
28public class NoteSessionImporter implements SessionLayerImporter {
29
30 @Override
31 public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) 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 note 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 NoteImporter importer = new NoteImporter();
46 try (InputStream in = support.getInputStream(fileStr)) {
47 return importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
48 }
49 } catch (XPathExpressionException | SAXException e) {
50 throw new IllegalDataException(e);
51 }
52 }
53}
Note: See TracBrowser for help on using the repository browser.