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

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

fix #13001 - Add MainPanel + some new methods (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 2.8 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.xml.sax.SAXException;
17
18/**
19 * File importer that reads note dump files (*.osn, .osn.gz and .osn.bz2)
20 * @since 7538
21 */
22public 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}
Note: See TracBrowser for help on using the repository browser.