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

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

see #8465 - use diamond operator where applicable

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