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

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

Sonar/FindBugs - Nested blocks of code should not be left empty

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