001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.File;
007import java.io.FileNotFoundException;
008import java.io.IOException;
009import java.io.OutputStream;
010import java.io.OutputStreamWriter;
011import java.io.PrintWriter;
012import java.io.Writer;
013import java.nio.charset.StandardCharsets;
014import java.text.MessageFormat;
015
016import javax.swing.JOptionPane;
017
018import org.openstreetmap.josm.Main;
019import org.openstreetmap.josm.actions.ExtensionFileFilter;
020import org.openstreetmap.josm.gui.layer.Layer;
021import org.openstreetmap.josm.gui.layer.OsmDataLayer;
022import org.openstreetmap.josm.tools.Utils;
023
024/**
025 * Exports data to an .osm file.
026 * @since 1949
027 */
028public class OsmExporter extends FileExporter {
029
030    /**
031     * Constructs a new {@code OsmExporter}.
032     */
033    public OsmExporter() {
034        super(OsmImporter.FILE_FILTER);
035    }
036
037    /**
038     * Constructs a new {@code OsmExporter}.
039     * @param filter The extension file filter
040     */
041    public OsmExporter(ExtensionFileFilter filter) {
042        super(filter);
043    }
044
045    @Override
046    public boolean acceptFile(File pathname, Layer layer) {
047        if (!(layer instanceof OsmDataLayer))
048            return false;
049        return super.acceptFile(pathname, layer);
050    }
051
052    @Override
053    public void exportData(File file, Layer layer) throws IOException {
054        exportData(file, layer, false);
055    }
056
057    /**
058     * Exports OSM data to the given file.
059     * @param file Output file
060     * @param layer Data layer. Must be an instance of {@link OsmDataLayer}.
061     * @param noBackup if {@code true}, the potential backup file created if the output file already exists will be deleted
062     *                 after a successful export
063     * @throws IllegalArgumentException if {@code layer} is not an instance of {@code OsmDataLayer}
064     */
065    public void exportData(File file, Layer layer, boolean noBackup) {
066        checkOsmDataLayer(layer);
067        save(file, (OsmDataLayer) layer, noBackup);
068    }
069
070    protected static void checkOsmDataLayer(Layer layer) {
071        if (!(layer instanceof OsmDataLayer)) {
072            throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer
073                    .getClass().getName()));
074        }
075    }
076
077    protected static OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
078        return Compression.getCompressedFileOutputStream(file);
079    }
080
081    private void save(File file, OsmDataLayer layer, boolean noBackup) {
082        File tmpFile = null;
083        try {
084            // use a tmp file because if something errors out in the
085            // process of writing the file, we might just end up with
086            // a truncated file.  That can destroy lots of work.
087            if (file.exists()) {
088                tmpFile = new File(file.getPath() + '~');
089                Utils.copyFile(file, tmpFile);
090            }
091
092            doSave(file, layer);
093            if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
094                if (tmpFile != null) {
095                    Utils.deleteFile(tmpFile);
096                }
097            }
098            layer.onPostSaveToFile();
099        } 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}