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