1 | package org.openstreetmap.josm.gui.mappaint;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 |
|
---|
5 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
6 | import org.xml.sax.Attributes;
|
---|
7 | import org.xml.sax.helpers.DefaultHandler;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 |
|
---|
11 | public 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 | String colorString;
|
---|
52 | if(i < 0) // name only
|
---|
53 | colorString = Main.pref.get("color.mappaint."+styleName+"."+colString);
|
---|
54 | else if(i == 0) // value only
|
---|
55 | colorString = colString;
|
---|
56 | else // value and name
|
---|
57 | colorString = Main.pref.get("color.mappaint."+styleName+"."+colString.substring(0,i), colString.substring(i));
|
---|
58 | return ColorHelper.html2color(colorString);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override public void startDocument() {
|
---|
62 | inDoc = true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override public void endDocument() {
|
---|
66 | inDoc = false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override public void startElement(String uri,String name, String qName, Attributes atts) {
|
---|
70 | if (inDoc==true)
|
---|
71 | {
|
---|
72 | if (qName.equals("rule"))
|
---|
73 | inRule=true;
|
---|
74 | else if (qName.equals("scale_max"))
|
---|
75 | inScaleMax = true;
|
---|
76 | else if (qName.equals("scale_min"))
|
---|
77 | inScaleMin = true;
|
---|
78 | else if (qName.equals("condition") && inRule)
|
---|
79 | {
|
---|
80 | inCondition=true;
|
---|
81 | for (int count=0; count<atts.getLength(); count++)
|
---|
82 | {
|
---|
83 | if(atts.getQName(count).equals("k"))
|
---|
84 | rule.key = atts.getValue(count);
|
---|
85 | else if(atts.getQName(count).equals("v"))
|
---|
86 | rule.value = atts.getValue(count);
|
---|
87 | else if(atts.getQName(count).equals("b"))
|
---|
88 | rule.boolValue = atts.getValue(count);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | else if (qName.equals("line"))
|
---|
92 | {
|
---|
93 | hadLine = inLine = true;
|
---|
94 | for (int count=0; count<atts.getLength(); count++)
|
---|
95 | {
|
---|
96 | if(atts.getQName(count).equals("width"))
|
---|
97 | rule.line.width = Integer.parseInt(atts.getValue(count));
|
---|
98 | else if (atts.getQName(count).equals("colour"))
|
---|
99 | rule.line.color=convertColor(atts.getValue(count));
|
---|
100 | else if (atts.getQName(count).equals("realwidth"))
|
---|
101 | rule.line.realWidth=Integer.parseInt(atts.getValue(count));
|
---|
102 | else if (atts.getQName(count).equals("dashed"))
|
---|
103 | rule.line.dashed=Boolean.parseBoolean(atts.getValue(count));
|
---|
104 | else if(atts.getQName(count).equals("priority"))
|
---|
105 | rule.line.priority = Integer.parseInt(atts.getValue(count));
|
---|
106 | }
|
---|
107 | }
|
---|
108 | else if (qName.equals("linemod"))
|
---|
109 | {
|
---|
110 | hadLineMod = inLineMod = true;
|
---|
111 | for (int count=0; count<atts.getLength(); count++)
|
---|
112 | {
|
---|
113 | if(atts.getQName(count).equals("width"))
|
---|
114 | {
|
---|
115 | String val = atts.getValue(count);
|
---|
116 | if(val.startsWith("+"))
|
---|
117 | {
|
---|
118 | rule.linemod.width = Integer.parseInt(val.substring(1));
|
---|
119 | rule.linemod.widthMode = LineElemStyle.WidthMode.OFFSET;
|
---|
120 | }
|
---|
121 | else if(val.startsWith("-"))
|
---|
122 | {
|
---|
123 | rule.linemod.width = Integer.parseInt(val);
|
---|
124 | rule.linemod.widthMode = LineElemStyle.WidthMode.OFFSET;
|
---|
125 | }
|
---|
126 | else if(val.endsWith("%"))
|
---|
127 | {
|
---|
128 | rule.linemod.width = Integer.parseInt(val.substring(0, val.length()-1));
|
---|
129 | rule.linemod.widthMode = LineElemStyle.WidthMode.PERCENT;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | rule.linemod.width = Integer.parseInt(val);
|
---|
133 | }
|
---|
134 | else if (atts.getQName(count).equals("colour"))
|
---|
135 | rule.linemod.color=convertColor(atts.getValue(count));
|
---|
136 | else if (atts.getQName(count).equals("realwidth"))
|
---|
137 | rule.linemod.realWidth=Integer.parseInt(atts.getValue(count));
|
---|
138 | else if (atts.getQName(count).equals("dashed"))
|
---|
139 | rule.linemod.dashed=Boolean.parseBoolean(atts.getValue(count));
|
---|
140 | else if(atts.getQName(count).equals("priority"))
|
---|
141 | rule.linemod.priority = Integer.parseInt(atts.getValue(count));
|
---|
142 | else if(atts.getQName(count).equals("mode"))
|
---|
143 | rule.linemod.over = !atts.getValue(count).equals("under");
|
---|
144 | }
|
---|
145 | }
|
---|
146 | else if (qName.equals("icon"))
|
---|
147 | {
|
---|
148 | hadIcon = inIcon = true;
|
---|
149 | for (int count=0; count<atts.getLength(); count++)
|
---|
150 | {
|
---|
151 | if (atts.getQName(count).equals("src"))
|
---|
152 | rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count), styleName);
|
---|
153 | else if (atts.getQName(count).equals("annotate"))
|
---|
154 | rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
|
---|
155 | else if(atts.getQName(count).equals("priority"))
|
---|
156 | rule.icon.priority = Integer.parseInt(atts.getValue(count));
|
---|
157 | }
|
---|
158 | }
|
---|
159 | else if (qName.equals("area"))
|
---|
160 | {
|
---|
161 | hadArea = inArea = true;
|
---|
162 | for (int count=0; count<atts.getLength(); count++)
|
---|
163 | {
|
---|
164 | if (atts.getQName(count).equals("colour"))
|
---|
165 | rule.area.color=convertColor(atts.getValue(count));
|
---|
166 | else if(atts.getQName(count).equals("priority"))
|
---|
167 | rule.area.priority = Integer.parseInt(atts.getValue(count));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override public void endElement(String uri,String name, String qName)
|
---|
174 | {
|
---|
175 | if (inRule && qName.equals("rule"))
|
---|
176 | {
|
---|
177 | if(hadLine)
|
---|
178 | styles.add(styleName, rule.key, rule.value, rule.boolValue,
|
---|
179 | new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
|
---|
180 | if(hadLineMod)
|
---|
181 | styles.addModifier(styleName, rule.key, rule.value, rule.boolValue,
|
---|
182 | new LineElemStyle(rule.linemod, rule.scaleMax, rule.scaleMin));
|
---|
183 | if(hadIcon)
|
---|
184 | styles.add(styleName, rule.key, rule.value, rule.boolValue,
|
---|
185 | new IconElemStyle(rule.icon, rule.scaleMax, rule.scaleMin));
|
---|
186 | if(hadArea)
|
---|
187 | styles.add(styleName, rule.key, rule.value, rule.boolValue,
|
---|
188 | new AreaElemStyle(rule.area, rule.scaleMax, rule.scaleMin));
|
---|
189 | inRule = false;
|
---|
190 | hadLine = hadLineMod = hadIcon = hadArea = false;
|
---|
191 | rule.init();
|
---|
192 | }
|
---|
193 | else if (inCondition && qName.equals("condition"))
|
---|
194 | inCondition = false;
|
---|
195 | else if (inLine && qName.equals("line"))
|
---|
196 | inLine = false;
|
---|
197 | else if (inLineMod && qName.equals("linemod"))
|
---|
198 | inLineMod = false;
|
---|
199 | else if (inIcon && qName.equals("icon"))
|
---|
200 | inIcon = false;
|
---|
201 | else if (inArea && qName.equals("area"))
|
---|
202 | inArea = false;
|
---|
203 | else if (qName.equals("scale_max"))
|
---|
204 | inScaleMax = false;
|
---|
205 | else if (qName.equals("scale_min"))
|
---|
206 | inScaleMin = false;
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override public void characters(char ch[], int start, int length)
|
---|
210 | {
|
---|
211 | if (inScaleMax == true)
|
---|
212 | rule.scaleMax = Long.parseLong(new String(ch, start, length));
|
---|
213 | else if (inScaleMin == true)
|
---|
214 | rule.scaleMin = Long.parseLong(new String(ch, start, length));
|
---|
215 | }
|
---|
216 | }
|
---|