source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyleHandler.java@ 1629

Last change on this file since 1629 was 1629, checked in by stoecker, 16 years ago

close #2426 - now mappaint handles area types which require closed ways (e.g. cliff) correctly

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