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

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

sonar - squid:S1066 - Collapsible "if" statements should be merged

  • Property svn:eol-style set to native
File size: 4.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.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.Utils;
22
23/**
24 * Exports data to an .osm file.
25 * @since 1949
26 */
27public class OsmExporter extends FileExporter {
28
29 /**
30 * Constructs a new {@code OsmExporter}.
31 */
32 public OsmExporter() {
33 super(new ExtensionFileFilter(
34 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm)"));
35 }
36
37 /**
38 * Constructs a new {@code OsmExporter}.
39 * @param filter The extension file filter
40 */
41 public OsmExporter(ExtensionFileFilter filter) {
42 super(filter);
43 }
44
45 @Override
46 public boolean acceptFile(File pathname, Layer layer) {
47 if (!(layer instanceof OsmDataLayer))
48 return false;
49 return super.acceptFile(pathname, layer);
50 }
51
52 @Override
53 public void exportData(File file, Layer layer) throws IOException {
54 exportData(file, layer, false);
55 }
56
57 /**
58 * Exports OSM data to the given file.
59 * @param file Output file
60 * @param layer Data layer. Must be an instance of {@link OsmDataLayer}.
61 * @param noBackup if {@code true}, the potential backup file created if the output file already exists will be deleted
62 * after a successful export
63 * @throws IllegalArgumentException if {@code layer} is not an instance of {@code OsmDataLayer}
64 */
65 public void exportData(File file, Layer layer, boolean noBackup) {
66 checkOsmDataLayer(layer);
67 save(file, (OsmDataLayer) layer, noBackup);
68 }
69
70 protected static void checkOsmDataLayer(Layer layer) {
71 if (!(layer instanceof OsmDataLayer)) {
72 throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer
73 .getClass().getName()));
74 }
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 Main.error(e);
98 JOptionPane.showMessageDialog(
99 Main.parent,
100 tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
101 tr("Error"),
102 JOptionPane.ERROR_MESSAGE
103 );
104
105 try {
106 // if the file save failed, then the tempfile will not be deleted. So, restore the backup if we made one.
107 if (tmpFile != null && tmpFile.exists()) {
108 Utils.copyFile(tmpFile, file);
109 }
110 } catch (IOException e2) {
111 Main.error(e2);
112 JOptionPane.showMessageDialog(
113 Main.parent,
114 tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
115 tr("Error"),
116 JOptionPane.ERROR_MESSAGE
117 );
118 }
119 }
120 }
121
122 protected void doSave(File file, OsmDataLayer layer) throws IOException {
123 // create outputstream and wrap it with gzip or bzip, if necessary
124 try (
125 OutputStream out = getOutputStream(file);
126 Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
127 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion())
128 ) {
129 layer.data.getReadLock().lock();
130 try {
131 w.writeLayer(layer);
132 } finally {
133 layer.data.getReadLock().unlock();
134 }
135 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.