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

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

see #15182 - move file importers/exporters from io package to gui.io.importexport package, as they rely heavily on GUI and are mainly used from Open/Save actions

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