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

Last change on this file since 1340 was 1340, checked in by ulfl, 15 years ago

add a mappaint "dashedcolour", which controls the background color of a dashed line

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