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.FileInputStream;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.FileOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.OutputStream;
|
---|
12 | import java.io.OutputStreamWriter;
|
---|
13 | import java.io.PrintWriter;
|
---|
14 | import java.io.Writer;
|
---|
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 |
|
---|
23 | public class OsmExporter extends FileExporter {
|
---|
24 |
|
---|
25 | public OsmExporter() {
|
---|
26 | super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
|
---|
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 | if (layer instanceof OsmDataLayer) {
|
---|
43 | save(file, (OsmDataLayer) layer);
|
---|
44 | } else
|
---|
45 | throw new IllegalArgumentException(tr("Expected instance of OsmDataLayer. Got ''{0}''.", layer
|
---|
46 | .getClass().getName()));
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
|
---|
50 | return new FileOutputStream(file);
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void save(File file, OsmDataLayer layer) {
|
---|
54 | File tmpFile = null;
|
---|
55 | try {
|
---|
56 | // use a tmp file because if something errors out in the
|
---|
57 | // process of writing the file, we might just end up with
|
---|
58 | // a truncated file. That can destroy lots of work.
|
---|
59 | if (file.exists()) {
|
---|
60 | tmpFile = new File(file.getPath() + "~");
|
---|
61 | copy(file, tmpFile);
|
---|
62 | }
|
---|
63 |
|
---|
64 | // create outputstream and wrap it with gzip or bzip, if necessary
|
---|
65 | OutputStream out = getOutputStream(file);
|
---|
66 | Writer writer = new OutputStreamWriter(out, "UTF-8");
|
---|
67 |
|
---|
68 | OsmWriter w = new OsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
|
---|
69 | w.header();
|
---|
70 | w.writeDataSources(layer.data);
|
---|
71 | w.writeContent(layer.data);
|
---|
72 | w.footer();
|
---|
73 | w.close();
|
---|
74 | // FIXME - how to close?
|
---|
75 | if (!Main.pref.getBoolean("save.keepbackup") && (tmpFile != null)) {
|
---|
76 | tmpFile.delete();
|
---|
77 | }
|
---|
78 | layer.onPostSaveToFile();
|
---|
79 | } catch (IOException e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | JOptionPane.showMessageDialog(
|
---|
82 | Main.parent,
|
---|
83 | tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
|
---|
84 | tr("Error"),
|
---|
85 | JOptionPane.ERROR_MESSAGE
|
---|
86 | );
|
---|
87 |
|
---|
88 | try {
|
---|
89 | // if the file save failed, then the tempfile will not
|
---|
90 | // be deleted. So, restore the backup if we made one.
|
---|
91 | if (tmpFile != null && tmpFile.exists()) {
|
---|
92 | copy(tmpFile, file);
|
---|
93 | }
|
---|
94 | } catch (IOException e2) {
|
---|
95 | e2.printStackTrace();
|
---|
96 | JOptionPane.showMessageDialog(
|
---|
97 | Main.parent,
|
---|
98 | tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
|
---|
99 | tr("Error"),
|
---|
100 | JOptionPane.ERROR_MESSAGE
|
---|
101 | );
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void copy(File src, File dst) throws IOException {
|
---|
107 | FileInputStream srcStream;
|
---|
108 | FileOutputStream dstStream;
|
---|
109 | try {
|
---|
110 | srcStream = new FileInputStream(src);
|
---|
111 | dstStream = new FileOutputStream(dst);
|
---|
112 | } catch (FileNotFoundException e) {
|
---|
113 | JOptionPane.showMessageDialog(Main.parent, tr("Could not back up file. Exception is: {0}", e
|
---|
114 | .getMessage()), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | byte buf[] = new byte[1 << 16];
|
---|
118 | int len;
|
---|
119 | while ((len = srcStream.read(buf)) != -1) {
|
---|
120 | dstStream.write(buf, 0, len);
|
---|
121 | }
|
---|
122 | srcStream.close();
|
---|
123 | dstStream.close();
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|