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

Last change on this file since 7070 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 4.1 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.FileNotFoundException;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.io.OutputStreamWriter;
11import java.io.PrintWriter;
12import java.io.Writer;
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
23public class OsmExporter extends FileExporter {
24
25 /**
26 * Constructs a new {@code OsmExporter}.
27 */
28 public OsmExporter() {
29 super(OsmImporter.FILE_FILTER);
30 }
31
32 public OsmExporter(ExtensionFileFilter filter) {
33 super(filter);
34 }
35
36 @Override
37 public boolean acceptFile(File pathname, Layer layer) {
38 if (!(layer instanceof OsmDataLayer))
39 return false;
40 return super.acceptFile(pathname, layer);
41 }
42
43 @Override
44 public void exportData(File file, Layer layer) throws IOException {
45 exportData(file, layer, false);
46 }
47
48 public void exportData(File file, Layer layer, boolean noBackup) throws IllegalArgumentException {
49 if (layer instanceof OsmDataLayer) {
50 save(file, (OsmDataLayer) layer, noBackup);
51 } else
52 throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer
53 .getClass().getName()));
54 }
55
56 protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
57 return Compression.getCompressedFileOutputStream(file);
58 }
59
60 private void save(File file, OsmDataLayer layer, boolean noBackup) {
61 File tmpFile = null;
62 try {
63 // use a tmp file because if something errors out in the
64 // process of writing the file, we might just end up with
65 // a truncated file. That can destroy lots of work.
66 if (file.exists()) {
67 tmpFile = new File(file.getPath() + "~");
68 Utils.copyFile(file, tmpFile);
69 }
70
71 // create outputstream and wrap it with gzip or bzip, if necessary
72 try (
73 OutputStream out = getOutputStream(file);
74 Writer writer = new OutputStreamWriter(out, Utils.UTF_8);
75 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
76 ) {
77 layer.data.getReadLock().lock();
78 try {
79 w.writeLayer(layer);
80 } finally {
81 layer.data.getReadLock().unlock();
82 }
83 }
84 if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
85 if (tmpFile != null) {
86 tmpFile.delete();
87 }
88 }
89 layer.onPostSaveToFile();
90 } catch (IOException e) {
91 Main.error(e);
92 JOptionPane.showMessageDialog(
93 Main.parent,
94 tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
95 tr("Error"),
96 JOptionPane.ERROR_MESSAGE
97 );
98
99 try {
100 // if the file save failed, then the tempfile will not
101 // be deleted. So, restore the backup if we made one.
102 if (tmpFile != null && tmpFile.exists()) {
103 Utils.copyFile(tmpFile, file);
104 }
105 } catch (IOException e2) {
106 Main.error(e2);
107 JOptionPane.showMessageDialog(
108 Main.parent,
109 tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
110 tr("Error"),
111 JOptionPane.ERROR_MESSAGE
112 );
113 }
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.