1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.io.OutputStreamWriter;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.Set;
|
---|
17 | import java.util.zip.ZipEntry;
|
---|
18 | import java.util.zip.ZipOutputStream;
|
---|
19 |
|
---|
20 | import javax.xml.parsers.DocumentBuilder;
|
---|
21 | import javax.xml.parsers.ParserConfigurationException;
|
---|
22 | import javax.xml.transform.OutputKeys;
|
---|
23 | import javax.xml.transform.Transformer;
|
---|
24 | import javax.xml.transform.TransformerException;
|
---|
25 | import javax.xml.transform.TransformerFactory;
|
---|
26 | import javax.xml.transform.dom.DOMSource;
|
---|
27 | import javax.xml.transform.stream.StreamResult;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
31 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
32 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
33 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
34 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
35 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
36 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
37 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
38 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
39 | import org.openstreetmap.josm.gui.layer.WMTSLayer;
|
---|
40 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
|
---|
41 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
42 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
43 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
44 | import org.openstreetmap.josm.tools.Utils;
|
---|
45 | import org.w3c.dom.Document;
|
---|
46 | import org.w3c.dom.Element;
|
---|
47 | import org.w3c.dom.Text;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Writes a .jos session file from current supported layers.
|
---|
51 | * @since 4685
|
---|
52 | */
|
---|
53 | public class SessionWriter {
|
---|
54 |
|
---|
55 | private static Map<Class<? extends Layer>, Class<? extends SessionLayerExporter>> sessionLayerExporters = new HashMap<>();
|
---|
56 |
|
---|
57 | private final List<Layer> layers;
|
---|
58 | private final int active;
|
---|
59 | private final Map<Layer, SessionLayerExporter> exporters;
|
---|
60 | private final MultiMap<Layer, Layer> dependencies;
|
---|
61 | private final boolean zip;
|
---|
62 |
|
---|
63 | private ZipOutputStream zipOut;
|
---|
64 |
|
---|
65 | static {
|
---|
66 | registerSessionLayerExporter(OsmDataLayer.class, OsmDataSessionExporter.class);
|
---|
67 | registerSessionLayerExporter(TMSLayer.class, ImagerySessionExporter.class);
|
---|
68 | registerSessionLayerExporter(WMSLayer.class, ImagerySessionExporter.class);
|
---|
69 | registerSessionLayerExporter(WMTSLayer.class, ImagerySessionExporter.class);
|
---|
70 | registerSessionLayerExporter(GpxLayer.class, GpxTracksSessionExporter.class);
|
---|
71 | registerSessionLayerExporter(GeoImageLayer.class, GeoImageSessionExporter.class);
|
---|
72 | registerSessionLayerExporter(MarkerLayer.class, MarkerSessionExporter.class);
|
---|
73 | registerSessionLayerExporter(NoteLayer.class, NoteSessionExporter.class);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Register a session layer exporter.
|
---|
78 | *
|
---|
79 | * The exporter class must have a one-argument constructor with layerClass as formal parameter type.
|
---|
80 | * @param layerClass layer class
|
---|
81 | * @param exporter exporter for this layer class
|
---|
82 | */
|
---|
83 | public static void registerSessionLayerExporter(Class<? extends Layer> layerClass, Class<? extends SessionLayerExporter> exporter) {
|
---|
84 | sessionLayerExporters.put(layerClass, exporter);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns the session layer exporter for the given layer.
|
---|
89 | * @param layer layer to export
|
---|
90 | * @return session layer exporter for the given layer
|
---|
91 | */
|
---|
92 | public static SessionLayerExporter getSessionLayerExporter(Layer layer) {
|
---|
93 | Class<? extends Layer> layerClass = layer.getClass();
|
---|
94 | Class<? extends SessionLayerExporter> exporterClass = sessionLayerExporters.get(layerClass);
|
---|
95 | if (exporterClass == null)
|
---|
96 | return null;
|
---|
97 | try {
|
---|
98 | return exporterClass.getConstructor(layerClass).newInstance(layer);
|
---|
99 | } catch (ReflectiveOperationException e) {
|
---|
100 | throw new JosmRuntimeException(e);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Constructs a new {@code SessionWriter}.
|
---|
106 | * @param layers The ordered list of layers to save
|
---|
107 | * @param active The index of active layer in {@code layers} (starts at 0). Ignored if set to -1
|
---|
108 | * @param exporters The exporters to use to save layers
|
---|
109 | * @param dependencies layer dependencies
|
---|
110 | * @param zip {@code true} if a joz archive has to be created, {@code false otherwise}
|
---|
111 | * @since 6271
|
---|
112 | */
|
---|
113 | public SessionWriter(List<Layer> layers, int active, Map<Layer, SessionLayerExporter> exporters,
|
---|
114 | MultiMap<Layer, Layer> dependencies, boolean zip) {
|
---|
115 | this.layers = layers;
|
---|
116 | this.active = active;
|
---|
117 | this.exporters = exporters;
|
---|
118 | this.dependencies = dependencies;
|
---|
119 | this.zip = zip;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * A class that provides some context for the individual {@link SessionLayerExporter}
|
---|
124 | * when doing the export.
|
---|
125 | */
|
---|
126 | public class ExportSupport {
|
---|
127 | private final Document doc;
|
---|
128 | private final int layerIndex;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Constructs a new {@code ExportSupport}.
|
---|
132 | * @param doc XML document
|
---|
133 | * @param layerIndex layer index
|
---|
134 | */
|
---|
135 | public ExportSupport(Document doc, int layerIndex) {
|
---|
136 | this.doc = doc;
|
---|
137 | this.layerIndex = layerIndex;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Creates an element of the type specified.
|
---|
142 | * @param name The name of the element type to instantiate
|
---|
143 | * @return A new {@code Element} object
|
---|
144 | * @see Document#createElement
|
---|
145 | */
|
---|
146 | public Element createElement(String name) {
|
---|
147 | return doc.createElement(name);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Creates a text node given the specified string.
|
---|
152 | * @param text The data for the node.
|
---|
153 | * @return The new {@code Text} object.
|
---|
154 | * @see Document#createTextNode
|
---|
155 | */
|
---|
156 | public Text createTextNode(String text) {
|
---|
157 | return doc.createTextNode(text);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Get the index of the layer that is currently exported.
|
---|
162 | * @return the index of the layer that is currently exported
|
---|
163 | */
|
---|
164 | public int getLayerIndex() {
|
---|
165 | return layerIndex;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Create a file inside the zip archive.
|
---|
170 | *
|
---|
171 | * @param zipPath the path inside the zip archive, e.g. "layers/03/data.xml"
|
---|
172 | * @return the OutputStream you can write to. Never close the returned
|
---|
173 | * output stream, but make sure to flush buffers.
|
---|
174 | * @throws IOException if any I/O error occurs
|
---|
175 | */
|
---|
176 | public OutputStream getOutputStreamZip(String zipPath) throws IOException {
|
---|
177 | if (!isZip()) throw new JosmRuntimeException("not zip");
|
---|
178 | ZipEntry entry = new ZipEntry(zipPath);
|
---|
179 | zipOut.putNextEntry(entry);
|
---|
180 | return zipOut;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Check, if the session is exported as a zip archive.
|
---|
185 | *
|
---|
186 | * @return true, if the session is exported as a zip archive (.joz file
|
---|
187 | * extension). It will always return true, if one of the
|
---|
188 | * {@link SessionLayerExporter} returns true for the
|
---|
189 | * {@link SessionLayerExporter#requiresZip()} method. Otherwise, the
|
---|
190 | * user can decide in the file chooser dialog.
|
---|
191 | */
|
---|
192 | public boolean isZip() {
|
---|
193 | return zip;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Creates XML (.jos) session document.
|
---|
199 | * @return new document
|
---|
200 | * @throws IOException if any I/O error occurs
|
---|
201 | */
|
---|
202 | public Document createJosDocument() throws IOException {
|
---|
203 | DocumentBuilder builder = null;
|
---|
204 | try {
|
---|
205 | builder = Utils.newSafeDOMBuilder();
|
---|
206 | } catch (ParserConfigurationException e) {
|
---|
207 | throw new IOException(e);
|
---|
208 | }
|
---|
209 | Document doc = builder.newDocument();
|
---|
210 |
|
---|
211 | Element root = doc.createElement("josm-session");
|
---|
212 | root.setAttribute("version", "0.1");
|
---|
213 | doc.appendChild(root);
|
---|
214 |
|
---|
215 | Element viewportEl = doc.createElement("viewport");
|
---|
216 | root.appendChild(viewportEl);
|
---|
217 | Element centerEl = doc.createElement("center");
|
---|
218 | viewportEl.appendChild(centerEl);
|
---|
219 | EastNorth center = Main.map.mapView.getCenter();
|
---|
220 | LatLon centerLL = Projections.inverseProject(center);
|
---|
221 | centerEl.setAttribute("lat", Double.toString(centerLL.lat()));
|
---|
222 | centerEl.setAttribute("lon", Double.toString(centerLL.lon()));
|
---|
223 | Element scale = doc.createElement("scale");
|
---|
224 | viewportEl.appendChild(scale);
|
---|
225 | double dist100px = Main.map.mapView.getDist100Pixel();
|
---|
226 | scale.setAttribute("meter-per-pixel", Double.toString(dist100px / 100));
|
---|
227 |
|
---|
228 | Element layersEl = doc.createElement("layers");
|
---|
229 | if (active >= 0) {
|
---|
230 | layersEl.setAttribute("active", Integer.toString(active+1));
|
---|
231 | }
|
---|
232 | root.appendChild(layersEl);
|
---|
233 |
|
---|
234 | for (int index = 0; index < layers.size(); ++index) {
|
---|
235 | Layer layer = layers.get(index);
|
---|
236 | SessionLayerExporter exporter = exporters.get(layer);
|
---|
237 | ExportSupport support = new ExportSupport(doc, index+1);
|
---|
238 | Element el = exporter.export(support);
|
---|
239 | el.setAttribute("index", Integer.toString(index+1));
|
---|
240 | el.setAttribute("name", layer.getName());
|
---|
241 | el.setAttribute("visible", Boolean.toString(layer.isVisible()));
|
---|
242 | if (!Utils.equalsEpsilon(layer.getOpacity(), 1.0)) {
|
---|
243 | el.setAttribute("opacity", Double.toString(layer.getOpacity()));
|
---|
244 | }
|
---|
245 | Set<Layer> deps = dependencies.get(layer);
|
---|
246 | if (deps != null && !deps.isEmpty()) {
|
---|
247 | List<Integer> depsInt = new ArrayList<>();
|
---|
248 | for (Layer depLayer : deps) {
|
---|
249 | int depIndex = layers.indexOf(depLayer);
|
---|
250 | if (depIndex == -1) {
|
---|
251 | Main.warn("Unable to find " + depLayer);
|
---|
252 | } else {
|
---|
253 | depsInt.add(depIndex+1);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | if (!depsInt.isEmpty()) {
|
---|
257 | el.setAttribute("depends", Utils.join(",", depsInt));
|
---|
258 | }
|
---|
259 | }
|
---|
260 | layersEl.appendChild(el);
|
---|
261 | }
|
---|
262 | return doc;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Writes given .jos document to an output stream.
|
---|
267 | * @param doc session document
|
---|
268 | * @param out output stream
|
---|
269 | * @throws IOException if any I/O error occurs
|
---|
270 | */
|
---|
271 | public void writeJos(Document doc, OutputStream out) throws IOException {
|
---|
272 | try {
|
---|
273 | OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
|
---|
274 | writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
---|
275 | TransformerFactory transfac = TransformerFactory.newInstance();
|
---|
276 | Transformer trans = transfac.newTransformer();
|
---|
277 | trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
---|
278 | trans.setOutputProperty(OutputKeys.INDENT, "yes");
|
---|
279 | trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
---|
280 | StreamResult result = new StreamResult(writer);
|
---|
281 | DOMSource source = new DOMSource(doc);
|
---|
282 | trans.transform(source, result);
|
---|
283 | } catch (TransformerException e) {
|
---|
284 | throw new JosmRuntimeException(e);
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Writes session to given file.
|
---|
290 | * @param f output file
|
---|
291 | * @throws IOException if any I/O error occurs
|
---|
292 | */
|
---|
293 | public void write(File f) throws IOException {
|
---|
294 | try (OutputStream out = new FileOutputStream(f)) {
|
---|
295 | write(out);
|
---|
296 | } catch (FileNotFoundException e) {
|
---|
297 | throw new IOException(e);
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Writes session to given output stream.
|
---|
303 | * @param out output stream
|
---|
304 | * @throws IOException if any I/O error occurs
|
---|
305 | */
|
---|
306 | public void write(OutputStream out) throws IOException {
|
---|
307 | if (zip) {
|
---|
308 | zipOut = new ZipOutputStream(new BufferedOutputStream(out), StandardCharsets.UTF_8);
|
---|
309 | }
|
---|
310 | Document doc = createJosDocument(); // as side effect, files may be added to zipOut
|
---|
311 | if (zip) {
|
---|
312 | ZipEntry entry = new ZipEntry("session.jos");
|
---|
313 | zipOut.putNextEntry(entry);
|
---|
314 | writeJos(doc, zipOut);
|
---|
315 | Utils.close(zipOut);
|
---|
316 | } else {
|
---|
317 | writeJos(doc, new BufferedOutputStream(out));
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|