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

Last change on this file since 9995 was 9537, checked in by bastiK, 8 years ago

applied #12370 - adapted osm file filter to other file types (patch by kolesar)

  • Property svn:eol-style set to native
File size: 4.9 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.nio.charset.StandardCharsets;
14import java.text.MessageFormat;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.ExtensionFileFilter;
20import org.openstreetmap.josm.gui.layer.Layer;
21import org.openstreetmap.josm.gui.layer.OsmDataLayer;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * Exports data to an .osm file.
26 * @since 1949
27 */
28public class OsmExporter extends FileExporter {
29
30 /**
31 * Constructs a new {@code OsmExporter}.
32 */
33 public OsmExporter() {
34 super(new ExtensionFileFilter(
35 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm)"));
36 }
37
38 /**
39 * Constructs a new {@code OsmExporter}.
40 * @param filter The extension file filter
41 */
42 public OsmExporter(ExtensionFileFilter filter) {
43 super(filter);
44 }
45
46 @Override
47 public boolean acceptFile(File pathname, Layer layer) {
48 if (!(layer instanceof OsmDataLayer))
49 return false;
50 return super.acceptFile(pathname, layer);
51 }
52
53 @Override
54 public void exportData(File file, Layer layer) throws IOException {
55 exportData(file, layer, false);
56 }
57
58 /**
59 * Exports OSM data to the given file.
60 * @param file Output file
61 * @param layer Data layer. Must be an instance of {@link OsmDataLayer}.
62 * @param noBackup if {@code true}, the potential backup file created if the output file already exists will be deleted
63 * after a successful export
64 * @throws IllegalArgumentException if {@code layer} is not an instance of {@code OsmDataLayer}
65 */
66 public void exportData(File file, Layer layer, boolean noBackup) {
67 checkOsmDataLayer(layer);
68 save(file, (OsmDataLayer) layer, noBackup);
69 }
70
71 protected static void checkOsmDataLayer(Layer layer) {
72 if (!(layer instanceof OsmDataLayer)) {
73 throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer
74 .getClass().getName()));
75 }
76 }
77
78 protected static OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
79 return Compression.getCompressedFileOutputStream(file);
80 }
81
82 private void save(File file, OsmDataLayer layer, boolean noBackup) {
83 File tmpFile = null;
84 try {
85 // use a tmp file because if something errors out in the
86 // process of writing the file, we might just end up with
87 // a truncated file. That can destroy lots of work.
88 if (file.exists()) {
89 tmpFile = new File(file.getPath() + '~');
90 Utils.copyFile(file, tmpFile);
91 }
92
93 doSave(file, layer);
94 if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
95 if (tmpFile != null) {
96 Utils.deleteFile(tmpFile);
97 }
98 }
99 layer.onPostSaveToFile();
100 } catch (IOException e) {
101 Main.error(e);
102 JOptionPane.showMessageDialog(
103 Main.parent,
104 tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
105 tr("Error"),
106 JOptionPane.ERROR_MESSAGE
107 );
108
109 try {
110 // if the file save failed, then the tempfile will not
111 // be deleted. So, restore the backup if we made one.
112 if (tmpFile != null && tmpFile.exists()) {
113 Utils.copyFile(tmpFile, file);
114 }
115 } catch (IOException e2) {
116 Main.error(e2);
117 JOptionPane.showMessageDialog(
118 Main.parent,
119 tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
120 tr("Error"),
121 JOptionPane.ERROR_MESSAGE
122 );
123 }
124 }
125 }
126
127 protected void doSave(File file, OsmDataLayer layer) throws IOException, FileNotFoundException {
128 // create outputstream and wrap it with gzip or bzip, if necessary
129 try (
130 OutputStream out = getOutputStream(file);
131 Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
132 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
133 ) {
134 layer.data.getReadLock().lock();
135 try {
136 w.writeLayer(layer);
137 } finally {
138 layer.data.getReadLock().unlock();
139 }
140 }
141 }
142}
Note: See TracBrowser for help on using the repository browser.