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