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

Last change on this file since 4852 was 4852, checked in by bastiK, 12 years ago

fixed #6252 - Phantom "unsaved" layers in 4021, 4032

  • 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.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.OutputStream;
12import java.io.OutputStreamWriter;
13import java.io.PrintWriter;
14import java.io.Writer;
15import java.text.MessageFormat;
16
17import javax.swing.JOptionPane;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.ExtensionFileFilter;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23
24public class OsmExporter extends FileExporter {
25
26 public OsmExporter() {
27 super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
28 }
29
30 public OsmExporter(ExtensionFileFilter filter) {
31 super(filter);
32 }
33
34 @Override
35 public boolean acceptFile(File pathname, Layer layer) {
36 if (!(layer instanceof OsmDataLayer))
37 return false;
38 return super.acceptFile(pathname, layer);
39 }
40
41 @Override
42 public void exportData(File file, Layer layer) throws IOException {
43 exportData(file, layer, false);
44 }
45
46 public void exportData(File file, Layer layer, boolean noBackup) throws IOException {
47 if (layer instanceof OsmDataLayer) {
48 save(file, (OsmDataLayer) layer, noBackup);
49 } else
50 throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer
51 .getClass().getName()));
52 }
53
54 protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
55 return new FileOutputStream(file);
56 }
57
58 private void save(File file, OsmDataLayer layer, boolean noBackup) {
59 File tmpFile = null;
60 try {
61 // use a tmp file because if something errors out in the
62 // process of writing the file, we might just end up with
63 // a truncated file. That can destroy lots of work.
64 if (file.exists()) {
65 tmpFile = new File(file.getPath() + "~");
66 copy(file, tmpFile);
67 }
68
69 // create outputstream and wrap it with gzip or bzip, if necessary
70 OutputStream out = getOutputStream(file);
71 Writer writer = new OutputStreamWriter(out, "UTF-8");
72
73 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
74 layer.data.getReadLock().lock();
75 try {
76 w.header();
77 w.writeDataSources(layer.data);
78 w.writeContent(layer.data);
79 w.footer();
80 w.close();
81 } finally {
82 layer.data.getReadLock().unlock();
83 }
84 // FIXME - how to close?
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 e.printStackTrace();
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 copy(tmpFile, file);
105 }
106 } catch (IOException e2) {
107 e2.printStackTrace();
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
118 private void copy(File src, File dst) throws IOException {
119 FileInputStream srcStream;
120 FileOutputStream dstStream;
121 try {
122 srcStream = new FileInputStream(src);
123 dstStream = new FileOutputStream(dst);
124 } catch (FileNotFoundException e) {
125 JOptionPane.showMessageDialog(Main.parent, tr("Could not back up file. Exception is: {0}", e
126 .getMessage()), tr("Error"), JOptionPane.ERROR_MESSAGE);
127 return;
128 }
129 byte buf[] = new byte[1 << 16];
130 int len;
131 while ((len = srcStream.read(buf)) != -1) {
132 dstStream.write(buf, 0, len);
133 }
134 srcStream.close();
135 dstStream.close();
136 }
137
138}
Note: See TracBrowser for help on using the repository browser.