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

Revision 5025, 4.7 KB checked in by Don-vip, 3 months ago (diff)

see #4043 - Have an 'upload prohibited' flag in .osm files

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