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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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