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