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