source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java@ 7005

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

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