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

Last change on this file since 1241 was 1241, checked in by stoecker, 15 years ago

moved style name into style file

  • Property svn:eol-style set to native
File size: 9.1 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 @Override public void startElement(String uri,String name, String qName, Attributes atts) {
71 if (inDoc==true)
72 {
73 if (qName.equals("rule"))
74 inRule=true;
75 else if (qName.equals("rules"))
76 {
77 if(styleName == null)
78 {
79 String n = atts.getValue("name");
80 if(n == null) n = "standard";
81 styleName = n;
82 }
83 }
84 else if (qName.equals("scale_max"))
85 inScaleMax = true;
86 else if (qName.equals("scale_min"))
87 inScaleMin = true;
88 else if (qName.equals("condition") && inRule)
89 {
90 inCondition=true;
91 for (int count=0; count<atts.getLength(); count++)
92 {
93 if(atts.getQName(count).equals("k"))
94 rule.key = atts.getValue(count);
95 else if(atts.getQName(count).equals("v"))
96 rule.value = atts.getValue(count);
97 else if(atts.getQName(count).equals("b"))
98 rule.boolValue = atts.getValue(count);
99 }
100 }
101 else if (qName.equals("line"))
102 {
103 hadLine = inLine = true;
104 for (int count=0; count<atts.getLength(); count++)
105 {
106 if(atts.getQName(count).equals("width"))
107 rule.line.width = Integer.parseInt(atts.getValue(count));
108 else if (atts.getQName(count).equals("colour"))
109 rule.line.color=convertColor(atts.getValue(count));
110 else if (atts.getQName(count).equals("realwidth"))
111 rule.line.realWidth=Integer.parseInt(atts.getValue(count));
112 else if (atts.getQName(count).equals("dashed"))
113 rule.line.dashed=Boolean.parseBoolean(atts.getValue(count));
114 else if(atts.getQName(count).equals("priority"))
115 rule.line.priority = Integer.parseInt(atts.getValue(count));
116 }
117 }
118 else if (qName.equals("linemod"))
119 {
120 hadLineMod = inLineMod = true;
121 for (int count=0; count<atts.getLength(); count++)
122 {
123 if(atts.getQName(count).equals("width"))
124 {
125 String val = atts.getValue(count);
126 if(val.startsWith("+"))
127 {
128 rule.linemod.width = Integer.parseInt(val.substring(1));
129 rule.linemod.widthMode = LineElemStyle.WidthMode.OFFSET;
130 }
131 else if(val.startsWith("-"))
132 {
133 rule.linemod.width = Integer.parseInt(val);
134 rule.linemod.widthMode = LineElemStyle.WidthMode.OFFSET;
135 }
136 else if(val.endsWith("%"))
137 {
138 rule.linemod.width = Integer.parseInt(val.substring(0, val.length()-1));
139 rule.linemod.widthMode = LineElemStyle.WidthMode.PERCENT;
140 }
141 else
142 rule.linemod.width = Integer.parseInt(val);
143 }
144 else if (atts.getQName(count).equals("colour"))
145 rule.linemod.color=convertColor(atts.getValue(count));
146 else if (atts.getQName(count).equals("realwidth"))
147 rule.linemod.realWidth=Integer.parseInt(atts.getValue(count));
148 else if (atts.getQName(count).equals("dashed"))
149 rule.linemod.dashed=Boolean.parseBoolean(atts.getValue(count));
150 else if(atts.getQName(count).equals("priority"))
151 rule.linemod.priority = Integer.parseInt(atts.getValue(count));
152 else if(atts.getQName(count).equals("mode"))
153 rule.linemod.over = !atts.getValue(count).equals("under");
154 }
155 }
156 else if (qName.equals("icon"))
157 {
158 hadIcon = inIcon = true;
159 for (int count=0; count<atts.getLength(); count++)
160 {
161 if (atts.getQName(count).equals("src"))
162 rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count), styleName);
163 else if (atts.getQName(count).equals("annotate"))
164 rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
165 else if(atts.getQName(count).equals("priority"))
166 rule.icon.priority = Integer.parseInt(atts.getValue(count));
167 }
168 }
169 else if (qName.equals("area"))
170 {
171 hadArea = inArea = true;
172 for (int count=0; count<atts.getLength(); count++)
173 {
174 if (atts.getQName(count).equals("colour"))
175 rule.area.color=convertColor(atts.getValue(count));
176 else if(atts.getQName(count).equals("priority"))
177 rule.area.priority = Integer.parseInt(atts.getValue(count));
178 }
179 }
180 }
181 }
182
183 @Override public void endElement(String uri,String name, String qName)
184 {
185 if (inRule && qName.equals("rule"))
186 {
187 if(hadLine)
188 styles.add(styleName, rule.key, rule.value, rule.boolValue,
189 new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
190 if(hadLineMod)
191 styles.addModifier(styleName, rule.key, rule.value, rule.boolValue,
192 new LineElemStyle(rule.linemod, rule.scaleMax, rule.scaleMin));
193 if(hadIcon)
194 styles.add(styleName, rule.key, rule.value, rule.boolValue,
195 new IconElemStyle(rule.icon, rule.scaleMax, rule.scaleMin));
196 if(hadArea)
197 styles.add(styleName, rule.key, rule.value, rule.boolValue,
198 new AreaElemStyle(rule.area, rule.scaleMax, rule.scaleMin));
199 inRule = false;
200 hadLine = hadLineMod = hadIcon = hadArea = false;
201 rule.init();
202 }
203 else if (inCondition && qName.equals("condition"))
204 inCondition = false;
205 else if (inLine && qName.equals("line"))
206 inLine = false;
207 else if (inLineMod && qName.equals("linemod"))
208 inLineMod = false;
209 else if (inIcon && qName.equals("icon"))
210 inIcon = false;
211 else if (inArea && qName.equals("area"))
212 inArea = false;
213 else if (qName.equals("scale_max"))
214 inScaleMax = false;
215 else if (qName.equals("scale_min"))
216 inScaleMin = false;
217 }
218
219 @Override public void characters(char ch[], int start, int length)
220 {
221 if (inScaleMax == true)
222 rule.scaleMax = Long.parseLong(new String(ch, start, length));
223 else if (inScaleMin == true)
224 rule.scaleMin = Long.parseLong(new String(ch, start, length));
225 }
226}
Note: See TracBrowser for help on using the repository browser.