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

Last change on this file since 4701 was 4645, checked in by framm, 12 years ago

Added three new hooks for plugins, allowing plugins to mess with JOSM's server I/O. Plugins can now add postprocessors to server reading (so they may change what JOSM receives from the OSM server), and they can add postprocessors to server writing (so they can react to an upload action *after* it happened and after IDs for new objects have been assigned - this is in addition to the existing UploadHook which is a server writing preprocessor). Thirdly, plugins can now substitute their own version of OsmWriter. Taken together, these changes make it possible for a plugin to introduce extra information from a non-OSM source into the JOSM editing process, and upon upload split away that extra information and send it to another destination. - Also made minor changes to CredentialDialog to allow plugins to subclass the dialog in case they need their own authentication.

  • Property svn:eol-style set to native
File size: 4.6 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.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 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 = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
70 layer.data.getReadLock().lock();
71 try {
72 w.header();
73 w.writeDataSources(layer.data);
74 w.writeContent(layer.data);
75 w.footer();
76 w.close();
77 } finally {
78 layer.data.getReadLock().unlock();
79 }
80 // FIXME - how to close?
81 if (!Main.pref.getBoolean("save.keepbackup", false) && (tmpFile != null)) {
82 tmpFile.delete();
83 }
84 layer.onPostSaveToFile();
85 } catch (IOException e) {
86 e.printStackTrace();
87 JOptionPane.showMessageDialog(
88 Main.parent,
89 tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
90 tr("Error"),
91 JOptionPane.ERROR_MESSAGE
92 );
93
94 try {
95 // if the file save failed, then the tempfile will not
96 // be deleted. So, restore the backup if we made one.
97 if (tmpFile != null && tmpFile.exists()) {
98 copy(tmpFile, file);
99 }
100 } catch (IOException e2) {
101 e2.printStackTrace();
102 JOptionPane.showMessageDialog(
103 Main.parent,
104 tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
105 tr("Error"),
106 JOptionPane.ERROR_MESSAGE
107 );
108 }
109 }
110 }
111
112 private void copy(File src, File dst) throws IOException {
113 FileInputStream srcStream;
114 FileOutputStream dstStream;
115 try {
116 srcStream = new FileInputStream(src);
117 dstStream = new FileOutputStream(dst);
118 } catch (FileNotFoundException e) {
119 JOptionPane.showMessageDialog(Main.parent, tr("Could not back up file. Exception is: {0}", e
120 .getMessage()), tr("Error"), JOptionPane.ERROR_MESSAGE);
121 return;
122 }
123 byte buf[] = new byte[1 << 16];
124 int len;
125 while ((len = srcStream.read(buf)) != -1) {
126 dstStream.write(buf, 0, len);
127 }
128 srcStream.close();
129 dstStream.close();
130 }
131
132}
Note: See TracBrowser for help on using the repository browser.