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

Last change on this file since 2626 was 2626, checked in by jttt, 15 years ago

Fixed some of the warnings found by FindBugs

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