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

Last change on this file since 18954 was 18954, checked in by GerdP, 2 years ago

fix #23427 load session performance strongly depends on order of layers
The patch

  • removes the duplicated code regarding XPath and calls existing method OsmDataSessionImporter.extractFileName() instead
  • improves that method to first try to find a "file" node given the current element before falling back to the possibly very slow XPath evaluate call (not sure if the XPath part is still needed, it is never used in my example).
  • Property svn:eol-style set to native
File size: 4.6 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.File;
7import java.io.IOException;
8import java.io.InputStream;
9
10import javax.xml.xpath.XPath;
11import javax.xml.xpath.XPathConstants;
12import javax.xml.xpath.XPathExpression;
13import javax.xml.xpath.XPathExpressionException;
14import javax.xml.xpath.XPathFactory;
15
16import org.openstreetmap.josm.actions.ExtensionFileFilter;
17import org.openstreetmap.josm.gui.io.importexport.FileImporter;
18import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.gui.layer.OsmDataLayer;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.io.IllegalDataException;
23import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
24import org.openstreetmap.josm.tools.Utils;
25import org.w3c.dom.Attr;
26import org.w3c.dom.Element;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
30/**
31 * Session importer for {@link OsmDataLayer}.
32 * @since 4685
33 */
34public class OsmDataSessionImporter implements SessionLayerImporter {
35
36 private static Attr x2;
37
38 @Override
39 public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
40 checkMetaVersion(elem);
41 final String fileStr = extractFileName(elem, support);
42 final File file = new File(fileStr);
43 for (FileImporter importer : ExtensionFileFilter.getImporters()) {
44 if (importer instanceof OsmImporter && importer.acceptFile(file)) {
45 return importData((OsmImporter) importer, support, fileStr, progressMonitor);
46 }
47 }
48 // Fall back to the default OsmImporter, just in case. It will probably fail.
49 return importData(new OsmImporter(), support, fileStr, progressMonitor);
50 }
51
52 /**
53 * Checks that element defines the expected version number.
54 * @param elem element to check
55 * @throws IllegalDataException if version is not the expected one
56 * @since 15377
57 */
58 public static void checkMetaVersion(Element elem) throws IllegalDataException {
59 String version = elem.getAttribute("version");
60 if (!"0.1".equals(version)) {
61 throw new IllegalDataException(tr("Version ''{0}'' of meta data for osm data layer is not supported. Expected: 0.1", version));
62 }
63 }
64
65 /**
66 * Extract file name from element.
67 * @param elem element to parse
68 * @param support import/export support
69 * @return file name, if present
70 * @throws IllegalDataException if file name missing or empty
71 * @since 15377
72 */
73 public static String extractFileName(Element elem, ImportSupport support) throws IllegalDataException {
74 try {
75 // see #23427: try first to avoid possibly very slow XPath call
76 NodeList x = elem.getElementsByTagName("file");
77 if (x.getLength() > 0 && x.item(0).getNodeType() == Node.ELEMENT_NODE) {
78 String fileStr = x.item(0).getTextContent();
79 if (!Utils.isEmpty(fileStr))
80 return fileStr;
81 }
82 XPathFactory xPathFactory = XPathFactory.newInstance();
83 XPath xpath = xPathFactory.newXPath();
84 XPathExpression fileExp = xpath.compile("file/text()");
85 String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
86 if (Utils.isEmpty(fileStr)) {
87 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
88 }
89 return fileStr;
90 } catch (XPathExpressionException e) {
91 throw new IllegalDataException(e);
92 }
93 }
94
95 /**
96 * Import data as a new layer.
97 * @param osmImporter OSM importer
98 * @param support import/export support
99 * @param fileStr file name to import
100 * @param progressMonitor progress monitor
101 * @return new layer
102 * @throws IOException in case of I/O error
103 * @throws IllegalDataException in case of illegal data
104 * @since 15377
105 */
106 public static Layer importData(OsmImporter osmImporter, ImportSupport support, String fileStr, ProgressMonitor progressMonitor)
107 throws IOException, IllegalDataException {
108 try (InputStream in = support.getInputStream(fileStr)) {
109 OsmImporter.OsmImporterData importData = osmImporter.loadLayer(
110 in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
111
112 support.addPostLayersTask(importData.getPostLayerTask());
113 return importData.getLayer();
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.