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