1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.xml;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.nio.charset.StandardCharsets;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
25 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.Range;
|
---|
27 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
28 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
30 | import org.openstreetmap.josm.io.CachedFile;
|
---|
31 | import org.openstreetmap.josm.tools.Utils;
|
---|
32 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
33 | import org.xml.sax.SAXException;
|
---|
34 | import org.xml.sax.SAXParseException;
|
---|
35 |
|
---|
36 | public class XmlStyleSource extends StyleSource implements StyleKeys {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The accepted MIME types sent in the HTTP Accept header.
|
---|
40 | * @since 6867
|
---|
41 | */
|
---|
42 | public static final String XML_STYLE_MIME_TYPES =
|
---|
43 | "application/xml, text/xml, text/plain; q=0.8, application/zip, application/octet-stream; q=0.5";
|
---|
44 |
|
---|
45 | protected final Map<String, IconPrototype> icons = new HashMap<>();
|
---|
46 | protected final Map<String, LinePrototype> lines = new HashMap<>();
|
---|
47 | protected final Map<String, LinemodPrototype> modifiers = new HashMap<>();
|
---|
48 | protected final Map<String, AreaPrototype> areas = new HashMap<>();
|
---|
49 | protected final List<IconPrototype> iconsList = new LinkedList<>();
|
---|
50 | protected final List<LinePrototype> linesList = new LinkedList<>();
|
---|
51 | protected final List<LinemodPrototype> modifiersList = new LinkedList<>();
|
---|
52 | protected final List<AreaPrototype> areasList = new LinkedList<>();
|
---|
53 |
|
---|
54 | public XmlStyleSource(String url, String name, String shortdescription) {
|
---|
55 | super(url, name, shortdescription);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public XmlStyleSource(SourceEntry entry) {
|
---|
59 | super(entry);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void init() {
|
---|
64 | super.init();
|
---|
65 | icons.clear();
|
---|
66 | lines.clear();
|
---|
67 | modifiers.clear();
|
---|
68 | areas.clear();
|
---|
69 | iconsList.clear();
|
---|
70 | linesList.clear();
|
---|
71 | modifiersList.clear();
|
---|
72 | areasList.clear();
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public void loadStyleSource() {
|
---|
77 | init();
|
---|
78 | try {
|
---|
79 | try (
|
---|
80 | InputStream in = getSourceInputStream();
|
---|
81 | InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)
|
---|
82 | ) {
|
---|
83 | XmlObjectParser parser = new XmlObjectParser(new XmlStyleSourceHandler(this));
|
---|
84 | parser.startWithValidation(reader,
|
---|
85 | Main.getXMLBase()+"/mappaint-style-1.0",
|
---|
86 | "resource://data/mappaint-style.xsd");
|
---|
87 | while (parser.hasNext());
|
---|
88 | }
|
---|
89 | } catch (IOException e) {
|
---|
90 | Main.warn(tr("Failed to load Mappaint styles from ''{0}''. Exception was: {1}", url, e.toString()));
|
---|
91 | Main.error(e);
|
---|
92 | logError(e);
|
---|
93 | } catch (SAXParseException e) {
|
---|
94 | Main.warn(tr("Failed to parse Mappaint styles from ''{0}''. Error was: [{1}:{2}] {3}",
|
---|
95 | url, e.getLineNumber(), e.getColumnNumber(), e.getMessage()));
|
---|
96 | Main.error(e);
|
---|
97 | logError(e);
|
---|
98 | } catch (SAXException e) {
|
---|
99 | Main.warn(tr("Failed to parse Mappaint styles from ''{0}''. Error was: {1}", url, e.getMessage()));
|
---|
100 | Main.error(e);
|
---|
101 | logError(e);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public InputStream getSourceInputStream() throws IOException {
|
---|
107 | CachedFile cf = getCachedFile();
|
---|
108 | InputStream zip = cf.findZipEntryInputStream("xml", "style");
|
---|
109 | if (zip != null) {
|
---|
110 | zipIcons = cf.getFile();
|
---|
111 | return zip;
|
---|
112 | } else {
|
---|
113 | zipIcons = null;
|
---|
114 | return cf.getInputStream();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public CachedFile getCachedFile() throws IOException {
|
---|
120 | return new CachedFile(url).setHttpAccept(XML_STYLE_MIME_TYPES);
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static class WayPrototypesRecord {
|
---|
124 | public LinePrototype line;
|
---|
125 | public List<LinemodPrototype> linemods;
|
---|
126 | public AreaPrototype area;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private <T extends Prototype> T update(T current, T candidate, Double scale, MultiCascade mc) {
|
---|
130 | if (requiresUpdate(current, candidate, scale, mc))
|
---|
131 | return candidate;
|
---|
132 | else
|
---|
133 | return current;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * checks whether a certain match is better than the current match
|
---|
138 | * @param current can be null
|
---|
139 | * @param candidate the new Prototype that could be used instead
|
---|
140 | * @param scale ignored if null, otherwise checks if scale is within the range of candidate
|
---|
141 | * @param mc side effect: update the valid region for the current MultiCascade
|
---|
142 | * @return {@code true} if {@code candidate} is better than the current match
|
---|
143 | */
|
---|
144 | private static boolean requiresUpdate(Prototype current, Prototype candidate, Double scale, MultiCascade mc) {
|
---|
145 | if (current == null || candidate.priority >= current.priority) {
|
---|
146 | if (scale == null)
|
---|
147 | return true;
|
---|
148 |
|
---|
149 | if (candidate.range.contains(scale)) {
|
---|
150 | mc.range = Range.cut(mc.range, candidate.range);
|
---|
151 | return true;
|
---|
152 | } else {
|
---|
153 | mc.range = mc.range.reduceAround(scale, candidate.range);
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 | private IconPrototype getNode(OsmPrimitive primitive, Double scale, MultiCascade mc) {
|
---|
161 | IconPrototype icon = null;
|
---|
162 | for (String key : primitive.keySet()) {
|
---|
163 | String val = primitive.get(key);
|
---|
164 | IconPrototype p;
|
---|
165 | if ((p = icons.get('n' + key + '=' + val)) != null) {
|
---|
166 | icon = update(icon, p, scale, mc);
|
---|
167 | }
|
---|
168 | if ((p = icons.get('b' + key + '=' + OsmUtils.getNamedOsmBoolean(val))) != null) {
|
---|
169 | icon = update(icon, p, scale, mc);
|
---|
170 | }
|
---|
171 | if ((p = icons.get('x' + key)) != null) {
|
---|
172 | icon = update(icon, p, scale, mc);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | for (IconPrototype s : iconsList) {
|
---|
176 | if (s.check(primitive)) {
|
---|
177 | icon = update(icon, s, scale, mc);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return icon;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * @param primitive OSM primitive
|
---|
185 | * @param closed The primitive is a closed way or we pretend it is closed.
|
---|
186 | * This is useful for multipolygon relations and outer ways of untagged
|
---|
187 | * multipolygon relations.
|
---|
188 | */
|
---|
189 | private void get(OsmPrimitive primitive, boolean closed, WayPrototypesRecord p, Double scale, MultiCascade mc) {
|
---|
190 | String lineIdx = null;
|
---|
191 | Map<String, LinemodPrototype> overlayMap = new HashMap<>();
|
---|
192 | boolean isNotArea = primitive.isKeyFalse("area");
|
---|
193 | for (String key : primitive.keySet()) {
|
---|
194 | String val = primitive.get(key);
|
---|
195 | AreaPrototype styleArea;
|
---|
196 | LinePrototype styleLine;
|
---|
197 | LinemodPrototype styleLinemod;
|
---|
198 | String idx = 'n' + key + '=' + val;
|
---|
199 | if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed) && !isNotArea) {
|
---|
200 | p.area = update(p.area, styleArea, scale, mc);
|
---|
201 | }
|
---|
202 | if ((styleLine = lines.get(idx)) != null) {
|
---|
203 | if (requiresUpdate(p.line, styleLine, scale, mc)) {
|
---|
204 | p.line = styleLine;
|
---|
205 | lineIdx = idx;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | if ((styleLinemod = modifiers.get(idx)) != null) {
|
---|
209 | if (requiresUpdate(null, styleLinemod, scale, mc)) {
|
---|
210 | overlayMap.put(idx, styleLinemod);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | idx = 'b' + key + '=' + OsmUtils.getNamedOsmBoolean(val);
|
---|
214 | if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed) && !isNotArea) {
|
---|
215 | p.area = update(p.area, styleArea, scale, mc);
|
---|
216 | }
|
---|
217 | if ((styleLine = lines.get(idx)) != null) {
|
---|
218 | if (requiresUpdate(p.line, styleLine, scale, mc)) {
|
---|
219 | p.line = styleLine;
|
---|
220 | lineIdx = idx;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | if ((styleLinemod = modifiers.get(idx)) != null) {
|
---|
224 | if (requiresUpdate(null, styleLinemod, scale, mc)) {
|
---|
225 | overlayMap.put(idx, styleLinemod);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | idx = 'x' + key;
|
---|
229 | if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed) && !isNotArea) {
|
---|
230 | p.area = update(p.area, styleArea, scale, mc);
|
---|
231 | }
|
---|
232 | if ((styleLine = lines.get(idx)) != null) {
|
---|
233 | if (requiresUpdate(p.line, styleLine, scale, mc)) {
|
---|
234 | p.line = styleLine;
|
---|
235 | lineIdx = idx;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | if ((styleLinemod = modifiers.get(idx)) != null) {
|
---|
239 | if (requiresUpdate(null, styleLinemod, scale, mc)) {
|
---|
240 | overlayMap.put(idx, styleLinemod);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | for (AreaPrototype s : areasList) {
|
---|
245 | if ((closed || !s.closed) && !isNotArea && s.check(primitive)) {
|
---|
246 | p.area = update(p.area, s, scale, mc);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | for (LinePrototype s : linesList) {
|
---|
250 | if (s.check(primitive)) {
|
---|
251 | p.line = update(p.line, s, scale, mc);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | for (LinemodPrototype s : modifiersList) {
|
---|
255 | if (s.check(primitive)) {
|
---|
256 | if (requiresUpdate(null, s, scale, mc)) {
|
---|
257 | overlayMap.put(s.getCode(), s);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 | overlayMap.remove(lineIdx); // do not use overlay if linestyle is from the same rule (example: railway=tram)
|
---|
262 | if (!overlayMap.isEmpty()) {
|
---|
263 | List<LinemodPrototype> tmp = new LinkedList<>();
|
---|
264 | if (p.linemods != null) {
|
---|
265 | tmp.addAll(p.linemods);
|
---|
266 | }
|
---|
267 | tmp.addAll(overlayMap.values());
|
---|
268 | Collections.sort(tmp);
|
---|
269 | p.linemods = tmp;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | public void add(XmlCondition c, Collection<XmlCondition> conditions, Prototype prot) {
|
---|
274 | if (conditions != null) {
|
---|
275 | prot.conditions = conditions;
|
---|
276 | if (prot instanceof IconPrototype) {
|
---|
277 | iconsList.add((IconPrototype) prot);
|
---|
278 | } else if (prot instanceof LinemodPrototype) {
|
---|
279 | modifiersList.add((LinemodPrototype) prot);
|
---|
280 | } else if (prot instanceof LinePrototype) {
|
---|
281 | linesList.add((LinePrototype) prot);
|
---|
282 | } else if (prot instanceof AreaPrototype) {
|
---|
283 | areasList.add((AreaPrototype) prot);
|
---|
284 | } else
|
---|
285 | throw new RuntimeException();
|
---|
286 | } else {
|
---|
287 | String key = c.getKey();
|
---|
288 | prot.code = key;
|
---|
289 | if (prot instanceof IconPrototype) {
|
---|
290 | icons.put(key, (IconPrototype) prot);
|
---|
291 | } else if (prot instanceof LinemodPrototype) {
|
---|
292 | modifiers.put(key, (LinemodPrototype) prot);
|
---|
293 | } else if (prot instanceof LinePrototype) {
|
---|
294 | lines.put(key, (LinePrototype) prot);
|
---|
295 | } else if (prot instanceof AreaPrototype) {
|
---|
296 | areas.put(key, (AreaPrototype) prot);
|
---|
297 | } else
|
---|
298 | throw new RuntimeException();
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | @Override
|
---|
303 | public void apply(MultiCascade mc, OsmPrimitive osm, double scale, boolean pretendWayIsClosed) {
|
---|
304 | Cascade def = mc.getOrCreateCascade("default");
|
---|
305 | boolean useMinMaxScale = Main.pref.getBoolean("mappaint.zoomLevelDisplay", false);
|
---|
306 |
|
---|
307 | if (osm instanceof Node || (osm instanceof Relation && "restriction".equals(osm.get("type")))) {
|
---|
308 | IconPrototype icon = getNode(osm, useMinMaxScale ? scale : null, mc);
|
---|
309 | if (icon != null) {
|
---|
310 | def.put(ICON_IMAGE, icon.icon);
|
---|
311 | if (osm instanceof Node) {
|
---|
312 | if (icon.annotate != null) {
|
---|
313 | if (icon.annotate) {
|
---|
314 | def.put(TEXT, Keyword.AUTO);
|
---|
315 | } else {
|
---|
316 | def.remove(TEXT);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 | } else if (osm instanceof Way || (osm instanceof Relation && ((Relation) osm).isMultipolygon())) {
|
---|
322 | WayPrototypesRecord p = new WayPrototypesRecord();
|
---|
323 | get(osm, pretendWayIsClosed || !(osm instanceof Way) || ((Way) osm).isClosed(), p, useMinMaxScale ? scale : null, mc);
|
---|
324 | if (p.line != null) {
|
---|
325 | def.put(WIDTH, new Float(p.line.getWidth()));
|
---|
326 | def.putOrClear(REAL_WIDTH, p.line.realWidth != null ? new Float(p.line.realWidth) : null);
|
---|
327 | def.putOrClear(COLOR, p.line.color);
|
---|
328 | if (p.line.color != null) {
|
---|
329 | int alpha = p.line.color.getAlpha();
|
---|
330 | if (alpha != 255) {
|
---|
331 | def.put(OPACITY, Utils.color_int2float(alpha));
|
---|
332 | }
|
---|
333 | }
|
---|
334 | def.putOrClear(DASHES, p.line.getDashed());
|
---|
335 | def.putOrClear(DASHES_BACKGROUND_COLOR, p.line.dashedColor);
|
---|
336 | }
|
---|
337 | Float refWidth = def.get(WIDTH, null, Float.class);
|
---|
338 | if (refWidth != null && p.linemods != null) {
|
---|
339 | int numOver = 0, numUnder = 0;
|
---|
340 |
|
---|
341 | while (mc.hasLayer(String.format("over_%d", ++numOver)));
|
---|
342 | while (mc.hasLayer(String.format("under_%d", ++numUnder)));
|
---|
343 |
|
---|
344 | for (LinemodPrototype mod : p.linemods) {
|
---|
345 | Cascade c;
|
---|
346 | if (mod.over) {
|
---|
347 | String layer = String.format("over_%d", numOver);
|
---|
348 | c = mc.getOrCreateCascade(layer);
|
---|
349 | c.put(OBJECT_Z_INDEX, new Float(numOver));
|
---|
350 | ++numOver;
|
---|
351 | } else {
|
---|
352 | String layer = String.format("under_%d", numUnder);
|
---|
353 | c = mc.getOrCreateCascade(layer);
|
---|
354 | c.put(OBJECT_Z_INDEX, new Float(-numUnder));
|
---|
355 | ++numUnder;
|
---|
356 | }
|
---|
357 | c.put(WIDTH, new Float(mod.getWidth(refWidth)));
|
---|
358 | c.putOrClear(COLOR, mod.color);
|
---|
359 | if (mod.color != null) {
|
---|
360 | int alpha = mod.color.getAlpha();
|
---|
361 | if (alpha != 255) {
|
---|
362 | c.put(OPACITY, Utils.color_int2float(alpha));
|
---|
363 | }
|
---|
364 | }
|
---|
365 | c.putOrClear(DASHES, mod.getDashed());
|
---|
366 | c.putOrClear(DASHES_BACKGROUND_COLOR, mod.dashedColor);
|
---|
367 | }
|
---|
368 | }
|
---|
369 | if (p.area != null) {
|
---|
370 | def.putOrClear(FILL_COLOR, p.area.color);
|
---|
371 | def.putOrClear(TEXT_POSITION, Keyword.CENTER);
|
---|
372 | def.putOrClear(TEXT, Keyword.AUTO);
|
---|
373 | def.remove(FILL_IMAGE);
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|