1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io.importexport;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
12 | import org.openstreetmap.josm.data.notes.Note;
|
---|
13 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
14 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.io.Compression;
|
---|
17 | import org.openstreetmap.josm.io.NoteReader;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * File importer that reads note dump files (*.osn, .osn.gz and .osn.bz2)
|
---|
23 | * @since 7538
|
---|
24 | */
|
---|
25 | public 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 | }
|
---|