source: josm/trunk/src/org/openstreetmap/josm/io/OsmExporter.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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