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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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