1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedList;
|
---|
7 |
|
---|
8 | import javax.swing.ImageIcon;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
12 | import org.xml.sax.Attributes;
|
---|
13 | import org.xml.sax.helpers.DefaultHandler;
|
---|
14 |
|
---|
15 | public class ElemStyleHandler extends DefaultHandler
|
---|
16 | {
|
---|
17 | boolean inDoc, inRule, inCondition, inLine, inLineMod, inIcon, inArea, inScaleMax, inScaleMin;
|
---|
18 | boolean hadLine, hadLineMod, hadIcon, hadArea;
|
---|
19 | ElemStyles styles;
|
---|
20 | String styleName;
|
---|
21 | RuleElem rule = new RuleElem();
|
---|
22 |
|
---|
23 | static class RuleElem {
|
---|
24 | Rule rule = new Rule();
|
---|
25 | Collection<Rule> rules;
|
---|
26 | long scaleMax;
|
---|
27 | long scaleMin;
|
---|
28 | LineElemStyle line = new LineElemStyle();
|
---|
29 | LineElemStyle linemod = new LineElemStyle();
|
---|
30 | AreaElemStyle area = new AreaElemStyle();
|
---|
31 | IconElemStyle icon = new IconElemStyle();
|
---|
32 | public void init()
|
---|
33 | {
|
---|
34 | rules = null;
|
---|
35 | scaleMax = 1000000000;
|
---|
36 | scaleMin = 0;
|
---|
37 | line.init();
|
---|
38 | rule.init();
|
---|
39 | linemod.init();
|
---|
40 | area.init();
|
---|
41 | icon.init();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ElemStyleHandler(String name) {
|
---|
46 | styleName = name;
|
---|
47 | inDoc=inRule=inCondition=inLine=inIcon=inArea=false;
|
---|
48 | rule.init();
|
---|
49 | styles = MapPaintStyles.getStyles();
|
---|
50 | }
|
---|
51 |
|
---|
52 | Color convertColor(String colString)
|
---|
53 | {
|
---|
54 | int i = colString.indexOf("#");
|
---|
55 | Color ret;
|
---|
56 | if(i < 0) {
|
---|
57 | ret = Main.pref.getColor("mappaint."+styleName+"."+colString, Color.red);
|
---|
58 | } else if(i == 0) {
|
---|
59 | ret = ColorHelper.html2color(colString);
|
---|
60 | } else {
|
---|
61 | ret = Main.pref.getColor("mappaint."+styleName+"."+colString.substring(0,i),
|
---|
62 | ColorHelper.html2color(colString.substring(i)));
|
---|
63 | }
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override public void startDocument() {
|
---|
68 | inDoc = true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override public void endDocument() {
|
---|
72 | inDoc = false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void error(String message) {
|
---|
76 | System.out.println(styleName + " (" + rule.rule.key + "=" + rule.rule.value + "): " + message);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void startElementLine(String qName, Attributes atts, LineElemStyle line) {
|
---|
80 | for (int count=0; count<atts.getLength(); count++)
|
---|
81 | {
|
---|
82 | if(atts.getQName(count).equals("width"))
|
---|
83 | {
|
---|
84 | String val = atts.getValue(count);
|
---|
85 | if(val.startsWith("+"))
|
---|
86 | {
|
---|
87 | line.setWidth(Integer.parseInt(val.substring(1)));
|
---|
88 | line.widthMode = LineElemStyle.WidthMode.OFFSET;
|
---|
89 | }
|
---|
90 | else if(val.startsWith("-"))
|
---|
91 | {
|
---|
92 | line.setWidth(Integer.parseInt(val));
|
---|
93 | line.widthMode = LineElemStyle.WidthMode.OFFSET;
|
---|
94 | }
|
---|
95 | else if(val.endsWith("%"))
|
---|
96 | {
|
---|
97 | line.setWidth(Integer.parseInt(val.substring(0, val.length()-1)));
|
---|
98 | line.widthMode = LineElemStyle.WidthMode.PERCENT;
|
---|
99 | } else {
|
---|
100 | line.setWidth(Integer.parseInt(val));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | else if (atts.getQName(count).equals("colour")) {
|
---|
104 | line.color=convertColor(atts.getValue(count));
|
---|
105 | } else if (atts.getQName(count).equals("realwidth")) {
|
---|
106 | line.realWidth=Integer.parseInt(atts.getValue(count));
|
---|
107 | } else if (atts.getQName(count).equals("dashed")) {
|
---|
108 | float[] dashed;
|
---|
109 | try {
|
---|
110 | String[] parts = atts.getValue(count).split(",");
|
---|
111 | dashed = new float[parts.length];
|
---|
112 | for (int i = 0; i < parts.length; i++) {
|
---|
113 | dashed[i] = (Integer.parseInt(parts[i]));
|
---|
114 | }
|
---|
115 | } catch (NumberFormatException nfe) {
|
---|
116 | boolean isDashed = Boolean.parseBoolean(atts.getValue(count));
|
---|
117 | if(isDashed) {
|
---|
118 | dashed = new float[]{9};
|
---|
119 | } else {
|
---|
120 | dashed = new float[0];
|
---|
121 | }
|
---|
122 | }
|
---|
123 | line.setDashed(dashed);
|
---|
124 | } else if (atts.getQName(count).equals("dashedcolour")) {
|
---|
125 | line.dashedColor=convertColor(atts.getValue(count));
|
---|
126 | } else if(atts.getQName(count).equals("priority")) {
|
---|
127 | line.priority = Integer.parseInt(atts.getValue(count));
|
---|
128 | } else if(atts.getQName(count).equals("mode")) {
|
---|
129 | line.over = !atts.getValue(count).equals("under");
|
---|
130 | } else {
|
---|
131 | error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override public void startElement(String uri,String name, String qName, Attributes atts) {
|
---|
137 | if (inDoc==true)
|
---|
138 | {
|
---|
139 | if (qName.equals("rule")) {
|
---|
140 | inRule=true;
|
---|
141 | } else if (qName.equals("rules"))
|
---|
142 | {
|
---|
143 | if(styleName == null)
|
---|
144 | {
|
---|
145 | String n = atts.getValue("name");
|
---|
146 | if(n == null) {
|
---|
147 | n = "standard";
|
---|
148 | }
|
---|
149 | styleName = n;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else if (qName.equals("scale_max")) {
|
---|
153 | inScaleMax = true;
|
---|
154 | } else if (qName.equals("scale_min")) {
|
---|
155 | inScaleMin = true;
|
---|
156 | } else if (qName.equals("condition") && inRule)
|
---|
157 | {
|
---|
158 | inCondition=true;
|
---|
159 | Rule r = rule.rule;
|
---|
160 | if(r.key != null)
|
---|
161 | {
|
---|
162 | if(rule.rules == null) {
|
---|
163 | rule.rules = new LinkedList<Rule>();
|
---|
164 | }
|
---|
165 | rule.rules.add(new Rule(rule.rule));
|
---|
166 | r = new Rule();
|
---|
167 | rule.rules.add(r);
|
---|
168 | }
|
---|
169 | for (int count=0; count<atts.getLength(); count++)
|
---|
170 | {
|
---|
171 | if(atts.getQName(count).equals("k")) {
|
---|
172 | r.key = atts.getValue(count);
|
---|
173 | } else if(atts.getQName(count).equals("v")) {
|
---|
174 | r.value = atts.getValue(count);
|
---|
175 | } else if(atts.getQName(count).equals("b")) {
|
---|
176 | r.boolValue = atts.getValue(count);
|
---|
177 | } else {
|
---|
178 | error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
|
---|
179 | }
|
---|
180 | }
|
---|
181 | if(r.key == null) {
|
---|
182 | error("The condition has no key!");
|
---|
183 | }
|
---|
184 | }
|
---|
185 | else if (qName.equals("line"))
|
---|
186 | {
|
---|
187 | hadLine = inLine = true;
|
---|
188 | startElementLine(qName, atts, rule.line);
|
---|
189 | if(rule.line.widthMode != LineElemStyle.WidthMode.ABSOLUTE) {
|
---|
190 | error("Relative widths are not possible for normal lines");
|
---|
191 | rule.line.widthMode = LineElemStyle.WidthMode.ABSOLUTE;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | else if (qName.equals("linemod"))
|
---|
195 | {
|
---|
196 | hadLineMod = inLineMod = true;
|
---|
197 | startElementLine(qName, atts, rule.linemod);
|
---|
198 | }
|
---|
199 | else if (qName.equals("icon"))
|
---|
200 | {
|
---|
201 | inIcon = true;
|
---|
202 | for (int count=0; count<atts.getLength(); count++)
|
---|
203 | {
|
---|
204 | if (atts.getQName(count).equals("src")) {
|
---|
205 | ImageIcon icon = MapPaintStyles.getIcon(atts.getValue(count), styleName);
|
---|
206 | hadIcon = (icon != null);
|
---|
207 | rule.icon.icon = icon;
|
---|
208 | } else if (atts.getQName(count).equals("annotate")) {
|
---|
209 | rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
|
---|
210 | } else if(atts.getQName(count).equals("priority")) {
|
---|
211 | rule.icon.priority = Integer.parseInt(atts.getValue(count));
|
---|
212 | } else {
|
---|
213 | error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | else if (qName.equals("area"))
|
---|
218 | {
|
---|
219 | hadArea = inArea = true;
|
---|
220 | for (int count=0; count<atts.getLength(); count++)
|
---|
221 | {
|
---|
222 | if (atts.getQName(count).equals("colour")) {
|
---|
223 | rule.area.color=convertColor(atts.getValue(count));
|
---|
224 | } else if (atts.getQName(count).equals("closed")) {
|
---|
225 | rule.area.closed=Boolean.parseBoolean(atts.getValue(count));
|
---|
226 | } else if(atts.getQName(count).equals("priority")) {
|
---|
227 | rule.area.priority = Integer.parseInt(atts.getValue(count));
|
---|
228 | } else {
|
---|
229 | error("The element \"" + qName + "\" has unknown attribute \"" + atts.getQName(count) + "\"!");
|
---|
230 | }
|
---|
231 | }
|
---|
232 | } else {
|
---|
233 | error("The element \"" + qName + "\" is unknown!");
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override public void endElement(String uri,String name, String qName)
|
---|
239 | {
|
---|
240 | if (inRule && qName.equals("rule"))
|
---|
241 | {
|
---|
242 | if(hadLine)
|
---|
243 | {
|
---|
244 | styles.add(styleName, rule.rule, rule.rules,
|
---|
245 | new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
|
---|
246 | }
|
---|
247 | if(hadLineMod)
|
---|
248 | {
|
---|
249 | styles.addModifier(styleName, rule.rule, rule.rules,
|
---|
250 | new LineElemStyle(rule.linemod, rule.scaleMax, rule.scaleMin));
|
---|
251 | }
|
---|
252 | if(hadIcon)
|
---|
253 | {
|
---|
254 | styles.add(styleName, rule.rule, rule.rules,
|
---|
255 | new IconElemStyle(rule.icon, rule.scaleMax, rule.scaleMin));
|
---|
256 | }
|
---|
257 | if(hadArea)
|
---|
258 | {
|
---|
259 | styles.add(styleName, rule.rule, rule.rules,
|
---|
260 | new AreaElemStyle(rule.area, rule.scaleMax, rule.scaleMin));
|
---|
261 | }
|
---|
262 | inRule = false;
|
---|
263 | hadLine = hadLineMod = hadIcon = hadArea = false;
|
---|
264 | rule.init();
|
---|
265 | }
|
---|
266 | else if (inCondition && qName.equals("condition")) {
|
---|
267 | inCondition = false;
|
---|
268 | } else if (inLine && qName.equals("line")) {
|
---|
269 | inLine = false;
|
---|
270 | } else if (inLineMod && qName.equals("linemod")) {
|
---|
271 | inLineMod = false;
|
---|
272 | } else if (inIcon && qName.equals("icon")) {
|
---|
273 | inIcon = false;
|
---|
274 | } else if (inArea && qName.equals("area")) {
|
---|
275 | inArea = false;
|
---|
276 | } else if (qName.equals("scale_max")) {
|
---|
277 | inScaleMax = false;
|
---|
278 | } else if (qName.equals("scale_min")) {
|
---|
279 | inScaleMin = false;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override public void characters(char ch[], int start, int length)
|
---|
284 | {
|
---|
285 | if (inScaleMax == true) {
|
---|
286 | rule.scaleMax = Long.parseLong(new String(ch, start, length));
|
---|
287 | } else if (inScaleMin == true) {
|
---|
288 | rule.scaleMin = Long.parseLong(new String(ch, start, length));
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|