1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileNotFoundException;
|
---|
8 | import java.io.FileOutputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.OutputStream;
|
---|
11 | import java.io.OutputStreamWriter;
|
---|
12 | import java.io.PrintWriter;
|
---|
13 | import java.io.Writer;
|
---|
14 | import java.text.MessageFormat;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.tools.Utils;
|
---|
23 |
|
---|
24 | public class OsmExporter extends FileExporter {
|
---|
25 |
|
---|
26 | public OsmExporter() {
|
---|
27 | super(OsmImporter.FILE_FILTER);
|
---|
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 | Utils.copyFile(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, Utils.UTF_8);
|
---|
72 |
|
---|
73 | OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
|
---|
74 | layer.data.getReadLock().lock();
|
---|
75 | try {
|
---|
76 | w.writeLayer(layer);
|
---|
77 | } finally {
|
---|
78 | Utils.close(w);
|
---|
79 | layer.data.getReadLock().unlock();
|
---|
80 | }
|
---|
81 | // FIXME - how to close?
|
---|
82 | if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
|
---|
83 | if (tmpFile != null) {
|
---|
84 | tmpFile.delete();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | layer.onPostSaveToFile();
|
---|
88 | } catch (IOException e) {
|
---|
89 | e.printStackTrace();
|
---|
90 | JOptionPane.showMessageDialog(
|
---|
91 | Main.parent,
|
---|
92 | tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
|
---|
93 | tr("Error"),
|
---|
94 | JOptionPane.ERROR_MESSAGE
|
---|
95 | );
|
---|
96 |
|
---|
97 | try {
|
---|
98 | // if the file save failed, then the tempfile will not
|
---|
99 | // be deleted. So, restore the backup if we made one.
|
---|
100 | if (tmpFile != null && tmpFile.exists()) {
|
---|
101 | Utils.copyFile(tmpFile, file);
|
---|
102 | }
|
---|
103 | } catch (IOException e2) {
|
---|
104 | e2.printStackTrace();
|
---|
105 | JOptionPane.showMessageDialog(
|
---|
106 | Main.parent,
|
---|
107 | tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
|
---|
108 | tr("Error"),
|
---|
109 | JOptionPane.ERROR_MESSAGE
|
---|
110 | );
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|