1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
6 |
|
---|
7 | import java.io.BufferedInputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileNotFoundException;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.lang.reflect.InvocationTargetException;
|
---|
14 | import java.net.URI;
|
---|
15 | import java.net.URISyntaxException;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.Enumeration;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Map.Entry;
|
---|
23 | import java.util.TreeMap;
|
---|
24 | import java.util.zip.ZipEntry;
|
---|
25 | import java.util.zip.ZipException;
|
---|
26 | import java.util.zip.ZipFile;
|
---|
27 |
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.SwingUtilities;
|
---|
30 | import javax.xml.parsers.DocumentBuilder;
|
---|
31 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
32 | import javax.xml.parsers.ParserConfigurationException;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.data.ViewportData;
|
---|
36 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
37 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
38 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
39 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
40 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
41 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
42 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
43 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
44 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
45 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
46 | import org.openstreetmap.josm.tools.Utils;
|
---|
47 | import org.w3c.dom.Document;
|
---|
48 | import org.w3c.dom.Element;
|
---|
49 | import org.w3c.dom.Node;
|
---|
50 | import org.w3c.dom.NodeList;
|
---|
51 | import org.xml.sax.SAXException;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Reads a .jos session file and loads the layers in the process.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | public class SessionReader {
|
---|
58 |
|
---|
59 | private static Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>();
|
---|
60 | static {
|
---|
61 | registerSessionLayerImporter("osm-data", OsmDataSessionImporter.class);
|
---|
62 | registerSessionLayerImporter("imagery", ImagerySessionImporter.class);
|
---|
63 | registerSessionLayerImporter("tracks", GpxTracksSessionImporter.class);
|
---|
64 | registerSessionLayerImporter("geoimage", GeoImageSessionImporter.class);
|
---|
65 | registerSessionLayerImporter("markers", MarkerSessionImporter.class);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static void registerSessionLayerImporter(String layerType, Class<? extends SessionLayerImporter> importer) {
|
---|
69 | sessionLayerImporters.put(layerType, importer);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static SessionLayerImporter getSessionLayerImporter(String layerType) {
|
---|
73 | Class<? extends SessionLayerImporter> importerClass = sessionLayerImporters.get(layerType);
|
---|
74 | if (importerClass == null)
|
---|
75 | return null;
|
---|
76 | SessionLayerImporter importer = null;
|
---|
77 | try {
|
---|
78 | importer = importerClass.newInstance();
|
---|
79 | } catch (InstantiationException | IllegalAccessException e) {
|
---|
80 | throw new RuntimeException(e);
|
---|
81 | }
|
---|
82 | return importer;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private URI sessionFileURI;
|
---|
86 | private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
|
---|
87 | private ZipFile zipFile;
|
---|
88 | private List<Layer> layers = new ArrayList<>();
|
---|
89 | private int active = -1;
|
---|
90 | private List<Runnable> postLoadTasks = new ArrayList<>();
|
---|
91 | private ViewportData viewport;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @return list of layers that are later added to the mapview
|
---|
95 | */
|
---|
96 | public List<Layer> getLayers() {
|
---|
97 | return layers;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @return active layer, or {@code null} if not set
|
---|
102 | * @since 6271
|
---|
103 | */
|
---|
104 | public Layer getActive() {
|
---|
105 | // layers is in reverse order because of the way TreeMap is built
|
---|
106 | return (active >= 0 && active < layers.size()) ? layers.get(layers.size()-1-active) : null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @return actions executed in EDT after layers have been added (message dialog, etc.)
|
---|
111 | */
|
---|
112 | public List<Runnable> getPostLoadTasks() {
|
---|
113 | return postLoadTasks;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Return the viewport (map position and scale).
|
---|
118 | * @return The viewport. Can be null when no viewport info is found in the file.
|
---|
119 | */
|
---|
120 | public ViewportData getViewport() {
|
---|
121 | return viewport;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public class ImportSupport {
|
---|
125 |
|
---|
126 | private String layerName;
|
---|
127 | private int layerIndex;
|
---|
128 | private List<LayerDependency> layerDependencies;
|
---|
129 |
|
---|
130 | public ImportSupport(String layerName, int layerIndex, List<LayerDependency> layerDependencies) {
|
---|
131 | this.layerName = layerName;
|
---|
132 | this.layerIndex = layerIndex;
|
---|
133 | this.layerDependencies = layerDependencies;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Path of the file inside the zip archive.
|
---|
138 | * Used as alternative return value for getFile method.
|
---|
139 | */
|
---|
140 | private String inZipPath;
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Add a task, e.g. a message dialog, that should
|
---|
144 | * be executed in EDT after all layers have been added.
|
---|
145 | */
|
---|
146 | public void addPostLayersTask(Runnable task) {
|
---|
147 | postLoadTasks.add(task);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Return an InputStream for a URI from a .jos/.joz file.
|
---|
152 | *
|
---|
153 | * The following forms are supported:
|
---|
154 | *
|
---|
155 | * - absolute file (both .jos and .joz):
|
---|
156 | * "file:///home/user/data.osm"
|
---|
157 | * "file:/home/user/data.osm"
|
---|
158 | * "file:///C:/files/data.osm"
|
---|
159 | * "file:/C:/file/data.osm"
|
---|
160 | * "/home/user/data.osm"
|
---|
161 | * "C:\files\data.osm" (not a URI, but recognized by File constructor on Windows systems)
|
---|
162 | * - standalone .jos files:
|
---|
163 | * - relative uri:
|
---|
164 | * "save/data.osm"
|
---|
165 | * "../project2/data.osm"
|
---|
166 | * - for .joz files:
|
---|
167 | * - file inside zip archive:
|
---|
168 | * "layers/01/data.osm"
|
---|
169 | * - relativ to the .joz file:
|
---|
170 | * "../save/data.osm" ("../" steps out of the archive)
|
---|
171 | *
|
---|
172 | * @throws IOException Thrown when no Stream can be opened for the given URI, e.g. when the linked file has been deleted.
|
---|
173 | */
|
---|
174 | public InputStream getInputStream(String uriStr) throws IOException {
|
---|
175 | File file = getFile(uriStr);
|
---|
176 | if (file != null) {
|
---|
177 | try {
|
---|
178 | return new BufferedInputStream(new FileInputStream(file));
|
---|
179 | } catch (FileNotFoundException e) {
|
---|
180 | throw new IOException(tr("File ''{0}'' does not exist.", file.getPath()));
|
---|
181 | }
|
---|
182 | } else if (inZipPath != null) {
|
---|
183 | ZipEntry entry = zipFile.getEntry(inZipPath);
|
---|
184 | if (entry != null) {
|
---|
185 | return zipFile.getInputStream(entry);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | throw new IOException(tr("Unable to locate file ''{0}''.", uriStr));
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Return a File for a URI from a .jos/.joz file.
|
---|
193 | *
|
---|
194 | * Returns null if the URI points to a file inside the zip archive.
|
---|
195 | * In this case, inZipPath will be set to the corresponding path.
|
---|
196 | */
|
---|
197 | public File getFile(String uriStr) throws IOException {
|
---|
198 | inZipPath = null;
|
---|
199 | try {
|
---|
200 | URI uri = new URI(uriStr);
|
---|
201 | if ("file".equals(uri.getScheme()))
|
---|
202 | // absolute path
|
---|
203 | return new File(uri);
|
---|
204 | else if (uri.getScheme() == null) {
|
---|
205 | // Check if this is an absolute path without 'file:' scheme part.
|
---|
206 | // At this point, (as an exception) platform dependent path separator will be recognized.
|
---|
207 | // (This form is discouraged, only for users that like to copy and paste a path manually.)
|
---|
208 | File file = new File(uriStr);
|
---|
209 | if (file.isAbsolute())
|
---|
210 | return file;
|
---|
211 | else {
|
---|
212 | // for relative paths, only forward slashes are permitted
|
---|
213 | if (isZip()) {
|
---|
214 | if (uri.getPath().startsWith("../")) {
|
---|
215 | // relative to session file - "../" step out of the archive
|
---|
216 | String relPath = uri.getPath().substring(3);
|
---|
217 | return new File(sessionFileURI.resolve(relPath));
|
---|
218 | } else {
|
---|
219 | // file inside zip archive
|
---|
220 | inZipPath = uriStr;
|
---|
221 | return null;
|
---|
222 | }
|
---|
223 | } else
|
---|
224 | return new File(sessionFileURI.resolve(uri));
|
---|
225 | }
|
---|
226 | } else
|
---|
227 | throw new IOException(tr("Unsupported scheme ''{0}'' in URI ''{1}''.", uri.getScheme(), uriStr));
|
---|
228 | } catch (URISyntaxException e) {
|
---|
229 | throw new IOException(e);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Determines if we are reading from a .joz file.
|
---|
235 | * @return {@code true} if we are reading from a .joz file, {@code false} otherwise
|
---|
236 | */
|
---|
237 | public boolean isZip() {
|
---|
238 | return zip;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Name of the layer that is currently imported.
|
---|
243 | */
|
---|
244 | public String getLayerName() {
|
---|
245 | return layerName;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Index of the layer that is currently imported.
|
---|
250 | */
|
---|
251 | public int getLayerIndex() {
|
---|
252 | return layerIndex;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Dependencies - maps the layer index to the importer of the given
|
---|
257 | * layer. All the dependent importers have loaded completely at this point.
|
---|
258 | */
|
---|
259 | public List<LayerDependency> getLayerDependencies() {
|
---|
260 | return layerDependencies;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | public static class LayerDependency {
|
---|
265 | private Integer index;
|
---|
266 | private Layer layer;
|
---|
267 | private SessionLayerImporter importer;
|
---|
268 |
|
---|
269 | public LayerDependency(Integer index, Layer layer, SessionLayerImporter importer) {
|
---|
270 | this.index = index;
|
---|
271 | this.layer = layer;
|
---|
272 | this.importer = importer;
|
---|
273 | }
|
---|
274 |
|
---|
275 | public SessionLayerImporter getImporter() {
|
---|
276 | return importer;
|
---|
277 | }
|
---|
278 |
|
---|
279 | public Integer getIndex() {
|
---|
280 | return index;
|
---|
281 | }
|
---|
282 |
|
---|
283 | public Layer getLayer() {
|
---|
284 | return layer;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | private static void error(String msg) throws IllegalDataException {
|
---|
289 | throw new IllegalDataException(msg);
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void parseJos(Document doc, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
293 | Element root = doc.getDocumentElement();
|
---|
294 | if (!equal(root.getTagName(), "josm-session")) {
|
---|
295 | error(tr("Unexpected root element ''{0}'' in session file", root.getTagName()));
|
---|
296 | }
|
---|
297 | String version = root.getAttribute("version");
|
---|
298 | if (!"0.1".equals(version)) {
|
---|
299 | error(tr("Version ''{0}'' of session file is not supported. Expected: 0.1", version));
|
---|
300 | }
|
---|
301 |
|
---|
302 | Element viewportEl = getElementByTagName(root, "viewport");
|
---|
303 | if (viewportEl != null) {
|
---|
304 | EastNorth center = null;
|
---|
305 | Element centerEl = getElementByTagName(viewportEl, "center");
|
---|
306 | if (centerEl != null && centerEl.hasAttribute("lat") && centerEl.hasAttribute("lon")) {
|
---|
307 | try {
|
---|
308 | LatLon centerLL = new LatLon(Double.parseDouble(centerEl.getAttribute("lat")), Double.parseDouble(centerEl.getAttribute("lon")));
|
---|
309 | center = Projections.project(centerLL);
|
---|
310 | } catch (NumberFormatException ex) {
|
---|
311 | Main.warn(ex);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | if (center != null) {
|
---|
315 | Element scaleEl = getElementByTagName(viewportEl, "scale");
|
---|
316 | if (scaleEl != null && scaleEl.hasAttribute("meter-per-pixel")) {
|
---|
317 | try {
|
---|
318 | double meterPerPixel = Double.parseDouble(scaleEl.getAttribute("meter-per-pixel"));
|
---|
319 | Projection proj = Main.getProjection();
|
---|
320 | // Get a "typical" distance in east/north units that
|
---|
321 | // corresponds to a couple of pixels. Shouldn't be too
|
---|
322 | // large, to keep it within projection bounds and
|
---|
323 | // not too small to avoid rounding errors.
|
---|
324 | double dist = 0.01 * proj.getDefaultZoomInPPD();
|
---|
325 | LatLon ll1 = proj.eastNorth2latlon(new EastNorth(center.east() - dist, center.north()));
|
---|
326 | LatLon ll2 = proj.eastNorth2latlon(new EastNorth(center.east() + dist, center.north()));
|
---|
327 | double meterPerEasting = ll1.greatCircleDistance(ll2) / dist / 2;
|
---|
328 | double scale = meterPerPixel / meterPerEasting; // unit: easting per pixel
|
---|
329 | viewport = new ViewportData(center, scale);
|
---|
330 | } catch (NumberFormatException ex) {
|
---|
331 | Main.warn(ex);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | Element layersEl = getElementByTagName(root, "layers");
|
---|
338 | if (layersEl == null) return;
|
---|
339 |
|
---|
340 | String activeAtt = layersEl.getAttribute("active");
|
---|
341 | try {
|
---|
342 | active = (activeAtt != null && !activeAtt.isEmpty()) ? Integer.parseInt(activeAtt)-1 : -1;
|
---|
343 | } catch (NumberFormatException e) {
|
---|
344 | Main.warn("Unsupported value for 'active' layer attribute. Ignoring it. Error was: "+e.getMessage());
|
---|
345 | active = -1;
|
---|
346 | }
|
---|
347 |
|
---|
348 | MultiMap<Integer, Integer> deps = new MultiMap<>();
|
---|
349 | Map<Integer, Element> elems = new HashMap<>();
|
---|
350 |
|
---|
351 | NodeList nodes = layersEl.getChildNodes();
|
---|
352 |
|
---|
353 | for (int i=0; i<nodes.getLength(); ++i) {
|
---|
354 | Node node = nodes.item(i);
|
---|
355 | if (node.getNodeType() == Node.ELEMENT_NODE) {
|
---|
356 | Element e = (Element) node;
|
---|
357 | if (equal(e.getTagName(), "layer")) {
|
---|
358 |
|
---|
359 | if (!e.hasAttribute("index")) {
|
---|
360 | error(tr("missing mandatory attribute ''index'' for element ''layer''"));
|
---|
361 | }
|
---|
362 | Integer idx = null;
|
---|
363 | try {
|
---|
364 | idx = Integer.parseInt(e.getAttribute("index"));
|
---|
365 | } catch (NumberFormatException ex) {
|
---|
366 | Main.warn(ex);
|
---|
367 | }
|
---|
368 | if (idx == null) {
|
---|
369 | error(tr("unexpected format of attribute ''index'' for element ''layer''"));
|
---|
370 | }
|
---|
371 | if (elems.containsKey(idx)) {
|
---|
372 | error(tr("attribute ''index'' ({0}) for element ''layer'' must be unique", Integer.toString(idx)));
|
---|
373 | }
|
---|
374 | elems.put(idx, e);
|
---|
375 |
|
---|
376 | deps.putVoid(idx);
|
---|
377 | String depStr = e.getAttribute("depends");
|
---|
378 | if (depStr != null && !depStr.isEmpty()) {
|
---|
379 | for (String sd : depStr.split(",")) {
|
---|
380 | Integer d = null;
|
---|
381 | try {
|
---|
382 | d = Integer.parseInt(sd);
|
---|
383 | } catch (NumberFormatException ex) {
|
---|
384 | Main.warn(ex);
|
---|
385 | }
|
---|
386 | if (d != null) {
|
---|
387 | deps.put(idx, d);
|
---|
388 | }
|
---|
389 | }
|
---|
390 | }
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | List<Integer> sorted = Utils.topologicalSort(deps);
|
---|
396 | final Map<Integer, Layer> layersMap = new TreeMap<>(Collections.reverseOrder());
|
---|
397 | final Map<Integer, SessionLayerImporter> importers = new HashMap<>();
|
---|
398 | final Map<Integer, String> names = new HashMap<>();
|
---|
399 |
|
---|
400 | progressMonitor.setTicksCount(sorted.size());
|
---|
401 | LAYER: for (int idx: sorted) {
|
---|
402 | Element e = elems.get(idx);
|
---|
403 | if (e == null) {
|
---|
404 | error(tr("missing layer with index {0}", idx));
|
---|
405 | }
|
---|
406 | if (!e.hasAttribute("name")) {
|
---|
407 | error(tr("missing mandatory attribute ''name'' for element ''layer''"));
|
---|
408 | }
|
---|
409 | String name = e.getAttribute("name");
|
---|
410 | names.put(idx, name);
|
---|
411 | if (!e.hasAttribute("type")) {
|
---|
412 | error(tr("missing mandatory attribute ''type'' for element ''layer''"));
|
---|
413 | }
|
---|
414 | String type = e.getAttribute("type");
|
---|
415 | SessionLayerImporter imp = getSessionLayerImporter(type);
|
---|
416 | if (imp == null) {
|
---|
417 | CancelOrContinueDialog dialog = new CancelOrContinueDialog();
|
---|
418 | dialog.show(
|
---|
419 | tr("Unable to load layer"),
|
---|
420 | tr("Cannot load layer of type ''{0}'' because no suitable importer was found.", type),
|
---|
421 | JOptionPane.WARNING_MESSAGE,
|
---|
422 | progressMonitor
|
---|
423 | );
|
---|
424 | if (dialog.isCancel()) {
|
---|
425 | progressMonitor.cancel();
|
---|
426 | return;
|
---|
427 | } else {
|
---|
428 | continue;
|
---|
429 | }
|
---|
430 | } else {
|
---|
431 | importers.put(idx, imp);
|
---|
432 | List<LayerDependency> depsImp = new ArrayList<>();
|
---|
433 | for (int d : deps.get(idx)) {
|
---|
434 | SessionLayerImporter dImp = importers.get(d);
|
---|
435 | if (dImp == null) {
|
---|
436 | CancelOrContinueDialog dialog = new CancelOrContinueDialog();
|
---|
437 | dialog.show(
|
---|
438 | tr("Unable to load layer"),
|
---|
439 | tr("Cannot load layer {0} because it depends on layer {1} which has been skipped.", idx, d),
|
---|
440 | JOptionPane.WARNING_MESSAGE,
|
---|
441 | progressMonitor
|
---|
442 | );
|
---|
443 | if (dialog.isCancel()) {
|
---|
444 | progressMonitor.cancel();
|
---|
445 | return;
|
---|
446 | } else {
|
---|
447 | continue LAYER;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | depsImp.add(new LayerDependency(d, layersMap.get(d), dImp));
|
---|
451 | }
|
---|
452 | ImportSupport support = new ImportSupport(name, idx, depsImp);
|
---|
453 | Layer layer = null;
|
---|
454 | Exception exception = null;
|
---|
455 | try {
|
---|
456 | layer = imp.load(e, support, progressMonitor.createSubTaskMonitor(1, false));
|
---|
457 | } catch (IllegalDataException | IOException ex) {
|
---|
458 | exception = ex;
|
---|
459 | }
|
---|
460 | if (exception != null) {
|
---|
461 | Main.error(exception);
|
---|
462 | CancelOrContinueDialog dialog = new CancelOrContinueDialog();
|
---|
463 | dialog.show(
|
---|
464 | tr("Error loading layer"),
|
---|
465 | tr("<html>Could not load layer {0} ''{1}''.<br>Error is:<br>{2}</html>", idx, name, exception.getMessage()),
|
---|
466 | JOptionPane.ERROR_MESSAGE,
|
---|
467 | progressMonitor
|
---|
468 | );
|
---|
469 | if (dialog.isCancel()) {
|
---|
470 | progressMonitor.cancel();
|
---|
471 | return;
|
---|
472 | } else {
|
---|
473 | continue;
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (layer == null) throw new RuntimeException();
|
---|
478 | layersMap.put(idx, layer);
|
---|
479 | }
|
---|
480 | progressMonitor.worked(1);
|
---|
481 | }
|
---|
482 |
|
---|
483 | layers = new ArrayList<>();
|
---|
484 | for (int idx : layersMap.keySet()) {
|
---|
485 | Layer layer = layersMap.get(idx);
|
---|
486 | if (layer == null) {
|
---|
487 | continue;
|
---|
488 | }
|
---|
489 | Element el = elems.get(idx);
|
---|
490 | if (el.hasAttribute("visible")) {
|
---|
491 | layer.setVisible(Boolean.parseBoolean(el.getAttribute("visible")));
|
---|
492 | }
|
---|
493 | if (el.hasAttribute("opacity")) {
|
---|
494 | try {
|
---|
495 | double opacity = Double.parseDouble(el.getAttribute("opacity"));
|
---|
496 | layer.setOpacity(opacity);
|
---|
497 | } catch (NumberFormatException ex) {
|
---|
498 | Main.warn(ex);
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | for (Entry<Integer, Layer> e : layersMap.entrySet()) {
|
---|
503 | Layer l = e.getValue();
|
---|
504 | if (l == null) {
|
---|
505 | continue;
|
---|
506 | }
|
---|
507 |
|
---|
508 | l.setName(names.get(e.getKey()));
|
---|
509 | layers.add(l);
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Show Dialog when there is an error for one layer.
|
---|
515 | * Ask the user whether to cancel the complete session loading or just to skip this layer.
|
---|
516 | *
|
---|
517 | * This is expected to run in a worker thread (PleaseWaitRunnable), so invokeAndWait is
|
---|
518 | * needed to block the current thread and wait for the result of the modal dialog from EDT.
|
---|
519 | */
|
---|
520 | private static class CancelOrContinueDialog {
|
---|
521 |
|
---|
522 | private boolean cancel;
|
---|
523 |
|
---|
524 | public void show(final String title, final String message, final int icon, final ProgressMonitor progressMonitor) {
|
---|
525 | try {
|
---|
526 | SwingUtilities.invokeAndWait(new Runnable() {
|
---|
527 | @Override public void run() {
|
---|
528 | ExtendedDialog dlg = new ExtendedDialog(
|
---|
529 | Main.parent,
|
---|
530 | title,
|
---|
531 | new String[] { tr("Cancel"), tr("Skip layer and continue") }
|
---|
532 | );
|
---|
533 | dlg.setButtonIcons(new String[] {"cancel", "dialogs/next"});
|
---|
534 | dlg.setIcon(icon);
|
---|
535 | dlg.setContent(message);
|
---|
536 | dlg.showDialog();
|
---|
537 | cancel = dlg.getValue() != 2;
|
---|
538 | }
|
---|
539 | });
|
---|
540 | } catch (InvocationTargetException | InterruptedException ex) {
|
---|
541 | throw new RuntimeException(ex);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | public boolean isCancel() {
|
---|
546 | return cancel;
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 | public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException {
|
---|
551 | if (progressMonitor == null) {
|
---|
552 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
553 | }
|
---|
554 |
|
---|
555 | try (InputStream josIS = createInputStream(sessionFile, zip)) {
|
---|
556 | loadSession(josIS, sessionFile.toURI(), zip, progressMonitor);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | private InputStream createInputStream(File sessionFile, boolean zip) throws IOException, IllegalDataException {
|
---|
561 | if (zip) {
|
---|
562 | try {
|
---|
563 | zipFile = new ZipFile(sessionFile);
|
---|
564 | return getZipInputStream(zipFile);
|
---|
565 | } catch (ZipException ze) {
|
---|
566 | throw new IOException(ze);
|
---|
567 | }
|
---|
568 | } else {
|
---|
569 | try {
|
---|
570 | return new FileInputStream(sessionFile);
|
---|
571 | } catch (FileNotFoundException ex) {
|
---|
572 | throw new IOException(ex);
|
---|
573 | }
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | private static InputStream getZipInputStream(ZipFile zipFile) throws ZipException, IOException, IllegalDataException {
|
---|
578 | ZipEntry josEntry = null;
|
---|
579 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
---|
580 | while (entries.hasMoreElements()) {
|
---|
581 | ZipEntry entry = entries.nextElement();
|
---|
582 | if (entry.getName().toLowerCase().endsWith(".jos")) {
|
---|
583 | josEntry = entry;
|
---|
584 | break;
|
---|
585 | }
|
---|
586 | }
|
---|
587 | if (josEntry == null) {
|
---|
588 | error(tr("expected .jos file inside .joz archive"));
|
---|
589 | }
|
---|
590 | return zipFile.getInputStream(josEntry);
|
---|
591 | }
|
---|
592 |
|
---|
593 | private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
594 |
|
---|
595 | this.sessionFileURI = sessionFileURI;
|
---|
596 | this.zip = zip;
|
---|
597 |
|
---|
598 | try {
|
---|
599 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
---|
600 | builderFactory.setValidating(false);
|
---|
601 | builderFactory.setNamespaceAware(true);
|
---|
602 | DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
---|
603 | Document document = builder.parse(josIS);
|
---|
604 | parseJos(document, progressMonitor);
|
---|
605 | } catch (SAXException e) {
|
---|
606 | throw new IllegalDataException(e);
|
---|
607 | } catch (ParserConfigurationException e) {
|
---|
608 | throw new IOException(e);
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | private static Element getElementByTagName(Element root, String name) {
|
---|
613 | NodeList els = root.getElementsByTagName(name);
|
---|
614 | if (els.getLength() == 0) return null;
|
---|
615 | return (Element) els.item(0);
|
---|
616 | }
|
---|
617 | }
|
---|