source: josm/trunk/src/org/openstreetmap/josm/io/NoteImporter.java@ 9997

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

fix #12531 - NPE when importing notes files (regression from r9746)

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.io.InputStream;
9import java.util.List;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.ExtensionFileFilter;
13import org.openstreetmap.josm.data.notes.Note;
14import org.openstreetmap.josm.gui.layer.NoteLayer;
15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
16import org.openstreetmap.josm.gui.util.GuiHelper;
17import org.xml.sax.SAXException;
18
19/**
20 * File importer that reads note dump files (*.osn, .osn.gz and .osn.bz2)
21 * @since 7538
22 */
23public class NoteImporter extends FileImporter {
24
25 private static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
26 "osn", "osn", tr("Note Files"), true);
27
28 /** Create an importer for note dump files */
29 public NoteImporter() {
30 super(FILE_FILTER);
31 }
32
33 @Override
34 public void importData(final File file, ProgressMonitor progressMonitor) throws IOException {
35 if (Main.isDebugEnabled()) {
36 Main.debug("importing notes file " + file.getAbsolutePath());
37 }
38 try (InputStream is = Compression.getUncompressedFileInputStream(file)) {
39 final NoteLayer layer = loadLayer(is, file, file.getName(), progressMonitor);
40 if (Main.map == null || !Main.map.mapView.hasLayer(layer)) {
41 GuiHelper.runInEDT(new Runnable() {
42 @Override
43 public void run() {
44 Main.main.addLayer(layer);
45 }
46 });
47 }
48 } catch (SAXException e) {
49 Main.error("error opening up notes file");
50 Main.error(e, true);
51 throw new IOException(e.getMessage(), e);
52 }
53 }
54
55 /**
56 * Load note layer from InputStream.
57 * @param in input stream
58 * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file)
59 * @param layerName name of generated layer
60 * @param progressMonitor handler for progress monitoring and canceling
61 * @return note layer
62 * @throws IOException if any I/O error occurs
63 * @throws SAXException if any SAX error occurs
64 * @since 9746
65 */
66 public NoteLayer loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor)
67 throws SAXException, IOException {
68 final List<Note> fileNotes = new NoteReader(in).parse();
69 List<NoteLayer> noteLayers = null;
70 if (Main.map != null) {
71 noteLayers = Main.map.mapView.getLayersOfType(NoteLayer.class);
72 }
73 final NoteLayer layer;
74 if (noteLayers != null && !noteLayers.isEmpty()) {
75 layer = noteLayers.get(0);
76 layer.getNoteData().addNotes(fileNotes);
77 } else {
78 layer = new NoteLayer(fileNotes, associatedFile != null ? associatedFile.getName() : tr("Notes"));
79 }
80 return layer;
81 }
82}
Note: See TracBrowser for help on using the repository browser.