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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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