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