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

Last change on this file since 8395 was 8291, checked in by Don-vip, 9 years ago

fix squid:RedundantThrowsDeclarationCheck + consistent Javadoc for exceptions

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