source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/OsmExporter.java@ 12800

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

see #15229 - see #15182 - deprecate OsmWriter.writeLayer(OsmDataLayer) - replacement: OsmWriter.write(DataSet)

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