source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/NoteImporter.java @ 13114

Last change on this file since 13114 was 13114, checked in by Don-vip, 16 months ago

fix #11537 - Autosave also note layer

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