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.OutputStream;
|
---|
9 | import java.io.OutputStreamWriter;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.io.Writer;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.nio.file.InvalidPathException;
|
---|
14 | import java.text.MessageFormat;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
19 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.io.Compression;
|
---|
23 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
24 | import org.openstreetmap.josm.io.OsmWriterFactory;
|
---|
25 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
26 | import org.openstreetmap.josm.tools.Logging;
|
---|
27 | import org.openstreetmap.josm.tools.Utils;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Exports data to an .osm file.
|
---|
31 | * @since 1949
|
---|
32 | */
|
---|
33 | public class OsmExporter extends FileExporter {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code OsmExporter}.
|
---|
37 | */
|
---|
38 | public OsmExporter() {
|
---|
39 | super(new ExtensionFileFilter(
|
---|
40 | "osm,xml", "osm", tr("OSM Server Files") + " (*.osm)"));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code OsmExporter}.
|
---|
45 | * @param filter The extension file filter
|
---|
46 | */
|
---|
47 | public OsmExporter(ExtensionFileFilter filter) {
|
---|
48 | super(filter);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public boolean acceptFile(File pathname, Layer layer) {
|
---|
53 | if (!(layer instanceof OsmDataLayer))
|
---|
54 | return false;
|
---|
55 | return super.acceptFile(pathname, layer);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void exportData(File file, Layer layer) throws IOException {
|
---|
60 | exportData(file, layer, false);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Exports OSM data to the given file.
|
---|
65 | * @param file Output file
|
---|
66 | * @param layer Data layer. Must be an instance of {@link OsmDataLayer}.
|
---|
67 | * @param noBackup if {@code true}, the potential backup file created if the output file already exists will be deleted
|
---|
68 | * after a successful export
|
---|
69 | * @throws IOException in case of IO errors
|
---|
70 | * @throws InvalidPathException when file name cannot be converted into a Path
|
---|
71 | * @throws IllegalArgumentException if {@code layer} is not an instance of {@code OsmDataLayer}
|
---|
72 | */
|
---|
73 | public void exportData(File file, Layer layer, boolean noBackup) throws IOException {
|
---|
74 | if (!(layer instanceof OsmDataLayer)) {
|
---|
75 | throw new IllegalArgumentException(
|
---|
76 | MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer.getClass().getName()));
|
---|
77 | }
|
---|
78 | save(file, (OsmDataLayer) layer, noBackup);
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected static OutputStream getOutputStream(File file) throws IOException {
|
---|
82 | return Compression.getCompressedFileOutputStream(file);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void save(File file, OsmDataLayer layer, boolean noBackup) throws IOException {
|
---|
86 | File tmpFile = null;
|
---|
87 | try {
|
---|
88 |
|
---|
89 | // use a tmp file because if something errors out in the process of writing the file,
|
---|
90 | // we might just end up with a truncated file. That can destroy lots of work.
|
---|
91 | if (file.exists()) {
|
---|
92 | tmpFile = new File(file.getPath() + '~');
|
---|
93 | Utils.copyFile(file, tmpFile);
|
---|
94 | }
|
---|
95 |
|
---|
96 | doSave(file, layer);
|
---|
97 | if ((noBackup || !Config.getPref().getBoolean("save.keepbackup", false)) && tmpFile != null) {
|
---|
98 | Utils.deleteFile(tmpFile);
|
---|
99 | }
|
---|
100 | layer.onPostSaveToFile();
|
---|
101 | } catch (IOException | InvalidPathException e) {
|
---|
102 | Logging.error(e);
|
---|
103 |
|
---|
104 | try {
|
---|
105 | // if the file save failed, then the tempfile will not be deleted. So, restore the backup if we made one.
|
---|
106 | if (tmpFile != null && tmpFile.exists()) {
|
---|
107 | Utils.copyFile(tmpFile, file);
|
---|
108 | }
|
---|
109 | } catch (IOException | InvalidPathException e2) {
|
---|
110 | Logging.error(e2);
|
---|
111 | JOptionPane.showMessageDialog(
|
---|
112 | MainApplication.getMainFrame(),
|
---|
113 | tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>",
|
---|
114 | Utils.escapeReservedCharactersHTML(e2.getMessage())),
|
---|
115 | tr("Error"),
|
---|
116 | JOptionPane.ERROR_MESSAGE
|
---|
117 | );
|
---|
118 | }
|
---|
119 | // re-throw original error
|
---|
120 | throw e;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected void doSave(File file, OsmDataLayer layer) throws IOException {
|
---|
125 | // create outputstream and wrap it with gzip, xz or bzip, if necessary
|
---|
126 | try (
|
---|
127 | OutputStream out = getOutputStream(file);
|
---|
128 | Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
|
---|
129 | OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion())
|
---|
130 | ) {
|
---|
131 | layer.data.getReadLock().lock();
|
---|
132 | try {
|
---|
133 | w.write(layer.data);
|
---|
134 | } finally {
|
---|
135 | layer.data.getReadLock().unlock();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|