svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / RendererXMLHandler.java @ 312
History | View | Annotate | Download (8.37 KB)
1 |
package com.iver.cit.gvsig.project; |
---|---|
2 |
|
3 |
import java.awt.BasicStroke; |
4 |
|
5 |
import org.apache.log4j.Logger; |
6 |
import org.xml.sax.Attributes; |
7 |
import org.xml.sax.ContentHandler; |
8 |
import org.xml.sax.Locator; |
9 |
import org.xml.sax.SAXException; |
10 |
|
11 |
import com.iver.cit.gvsig.fmap.FInterval; |
12 |
import com.iver.cit.gvsig.fmap.FRenderer; |
13 |
import com.iver.cit.gvsig.fmap.FSymbol; |
14 |
import com.iver.cit.gvsig.fmap.FSymbolFactory; |
15 |
import com.iver.utiles.StringUtilities; |
16 |
|
17 |
|
18 |
public class RendererXMLHandler implements ContentHandler{ |
19 |
private static Logger logger = Logger.getLogger(RendererXMLHandler.class.getName()); |
20 |
|
21 |
private int state = 3; |
22 |
private FRenderer renderer = null; |
23 |
private Object currentValue; |
24 |
private FInterval currentInterval;
|
25 |
|
26 |
/* (non-Javadoc)
|
27 |
* @see org.xml.sax.ContentHandler#endDocument()
|
28 |
*/
|
29 |
public void endDocument() throws SAXException { |
30 |
// TODO Auto-generated method stub
|
31 |
|
32 |
} |
33 |
|
34 |
/* (non-Javadoc)
|
35 |
* @see org.xml.sax.ContentHandler#startDocument()
|
36 |
*/
|
37 |
public void startDocument() throws SAXException { |
38 |
// TODO Auto-generated method stub
|
39 |
|
40 |
} |
41 |
|
42 |
/* (non-Javadoc)
|
43 |
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
44 |
*/
|
45 |
public void characters(char[] arg0, int arg1, int arg2) throws SAXException { |
46 |
// TODO Auto-generated method stub
|
47 |
|
48 |
} |
49 |
|
50 |
/* (non-Javadoc)
|
51 |
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
|
52 |
*/
|
53 |
public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException { |
54 |
// TODO Auto-generated method stub
|
55 |
|
56 |
} |
57 |
|
58 |
/* (non-Javadoc)
|
59 |
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
60 |
*/
|
61 |
public void endPrefixMapping(String arg0) throws SAXException { |
62 |
// TODO Auto-generated method stub
|
63 |
|
64 |
} |
65 |
|
66 |
/* (non-Javadoc)
|
67 |
* @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
68 |
*/
|
69 |
public void skippedEntity(String arg0) throws SAXException { |
70 |
// TODO Auto-generated method stub
|
71 |
|
72 |
} |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
76 |
*/
|
77 |
public void setDocumentLocator(Locator arg0) { |
78 |
// TODO Auto-generated method stub
|
79 |
|
80 |
} |
81 |
|
82 |
/* (non-Javadoc)
|
83 |
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
|
84 |
*/
|
85 |
public void processingInstruction(String arg0, String arg1) throws SAXException { |
86 |
// TODO Auto-generated method stub
|
87 |
|
88 |
} |
89 |
|
90 |
/* (non-Javadoc)
|
91 |
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
|
92 |
*/
|
93 |
public void startPrefixMapping(String arg0, String arg1) throws SAXException { |
94 |
// TODO Auto-generated method stub
|
95 |
|
96 |
} |
97 |
/**
|
98 |
* @see org.xml.sax.ContentHandler#endElement(java.lang.String,
|
99 |
* java.lang.String, java.lang.String)
|
100 |
*/
|
101 |
public void endElement(String namespaceURI, String localName, String qName) |
102 |
throws SAXException {
|
103 |
try{
|
104 |
if (localName.equals("renderer")) { |
105 |
state = 2;
|
106 |
} else if (localName.equals("symbol")) { |
107 |
if (state == 4) state = 3; |
108 |
else if (state == 12) state = 11; |
109 |
else if (state == 8) state = 10; |
110 |
} else if (localName.equals("classes")) { |
111 |
state = 3;
|
112 |
} else if (localName.equals("valueClass")) { |
113 |
state = 5;
|
114 |
} else if (localName.equals("intervalClass")) { |
115 |
state = 5;
|
116 |
} else if (localName.equals("interval")) { |
117 |
state = 11;
|
118 |
} else if (localName.equals("value")) { |
119 |
state = 10;
|
120 |
} |
121 |
}catch (Exception e) { |
122 |
logger.debug("end Element : "+ localName,e);
|
123 |
throw new SAXException(e); |
124 |
} |
125 |
} |
126 |
|
127 |
/**
|
128 |
* DOCUMENT ME!
|
129 |
*
|
130 |
* @param obj DOCUMENT ME!
|
131 |
* @param parent DOCUMENT ME!
|
132 |
*
|
133 |
* @return DOCUMENT ME!
|
134 |
*/
|
135 |
private boolean isAncestor(Class obj, Class parent) { |
136 |
Class ancestor = obj;
|
137 |
|
138 |
while ((ancestor != parent) && (ancestor != null)) { |
139 |
ancestor = ancestor.getSuperclass(); |
140 |
} |
141 |
|
142 |
if (ancestor == null) { |
143 |
return false; |
144 |
} else {
|
145 |
return true; |
146 |
} |
147 |
} |
148 |
|
149 |
private FRenderer renderer(Attributes atts){ |
150 |
FRenderer renderer = new FRenderer();
|
151 |
renderer.m_bDrawShapes = new Boolean(atts.getValue("drawShapes")).booleanValue(); |
152 |
String labelField = atts.getValue("labelField"); |
153 |
if (labelField != null){ |
154 |
renderer.setLabelField(labelField); |
155 |
} |
156 |
String field = atts.getValue("field"); |
157 |
if (field != null){ |
158 |
renderer.setField(field); |
159 |
} |
160 |
renderer.m_HeightField = atts.getValue("heightField");
|
161 |
renderer.m_bUseDefaultSymbol = new Boolean(atts.getValue( |
162 |
"useDefaultSymbol")).booleanValue();
|
163 |
renderer.setLegendType(new Integer(atts.getValue("legendType")).intValue()); |
164 |
|
165 |
return renderer;
|
166 |
|
167 |
} |
168 |
|
169 |
private FSymbol symbol(Attributes atts){ |
170 |
FSymbol symbol = new FSymbol();
|
171 |
symbol.m_symbolType = new Integer(atts.getValue("symbolType")).intValue(); |
172 |
symbol.m_Style = new Integer(atts.getValue("style")).intValue(); |
173 |
symbol.m_useOutline = new Boolean(atts.getValue("useOutline")).booleanValue(); |
174 |
String color = atts.getValue("color"); |
175 |
if (color != null){ |
176 |
symbol.m_Color = StringUtilities.string2Color(color); |
177 |
}else{
|
178 |
symbol.m_Color = null;
|
179 |
} |
180 |
symbol.m_outlineColor = StringUtilities.string2Color(atts.getValue( |
181 |
"outlineColor"));
|
182 |
symbol.m_Size = new Integer(atts.getValue("size")).intValue(); |
183 |
|
184 |
symbol.m_FontColor = StringUtilities.string2Color(atts.getValue("fontColor"));
|
185 |
|
186 |
String fillType = atts.getValue("fillType"); |
187 |
|
188 |
if (fillType != null){ |
189 |
if (fillType.equals("solid")) { |
190 |
symbol.m_Fill = StringUtilities.string2Color(atts.getValue( |
191 |
"fillCode"));
|
192 |
} else if (fillType.equals("texture")) { //texture |
193 |
symbol.m_Fill = FSymbolFactory.createPatternFills(symbol.m_Color)[symbol.m_Style-3];
|
194 |
} |
195 |
} |
196 |
|
197 |
symbol.m_LinePattern = atts.getValue("linePattern");
|
198 |
symbol.m_bUseSize = new Boolean(atts.getValue("useSize")).booleanValue(); |
199 |
|
200 |
String strokeWidth = atts.getValue("strokeWidth"); |
201 |
|
202 |
if (strokeWidth != null) { |
203 |
String dashArray = atts.getValue("strokeDashArray"); |
204 |
|
205 |
if (dashArray == null) { |
206 |
BasicStroke stroke = new BasicStroke(new Float( |
207 |
strokeWidth).floatValue(), |
208 |
new Integer(atts.getValue("strokeCap")).intValue(), |
209 |
new Integer(atts.getValue("strokeJoin")).intValue(), |
210 |
new Float(atts.getValue("strokeMitterLimit")).floatValue()); |
211 |
symbol.m_Stroke = stroke; |
212 |
} else {
|
213 |
BasicStroke stroke = new BasicStroke(new Float( |
214 |
strokeWidth).floatValue(), |
215 |
new Integer(atts.getValue("strokeCap")).intValue(), |
216 |
new Integer(atts.getValue("strokeJoin")).intValue(), |
217 |
new Float(atts.getValue("strokeMitterLimit")).floatValue(), |
218 |
StringUtilities.string2FloatArray(atts.getValue( |
219 |
"strokeDashArray")),
|
220 |
new Float(atts.getValue("strokeDashPhase")).floatValue()); |
221 |
symbol.m_Stroke = stroke; |
222 |
} |
223 |
} |
224 |
|
225 |
String description = atts.getValue("description"); |
226 |
if (description != null){ |
227 |
symbol.m_Descrip = description; |
228 |
} |
229 |
|
230 |
return symbol;
|
231 |
|
232 |
} |
233 |
private void value(Attributes atts){ |
234 |
currentValue = atts.getValue("value");
|
235 |
} |
236 |
|
237 |
private void interval(Attributes atts){ |
238 |
currentInterval = new FInterval();
|
239 |
currentInterval.setMin(new Double(atts.getValue("ini")).doubleValue()); |
240 |
currentInterval.setMax(new Double(atts.getValue("fin")).doubleValue()); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* @see org.xml.sax.ContentHandler#startElement(java.lang.String,
|
245 |
* java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
246 |
*/
|
247 |
public void startElement(String namespaceURI, String localName, |
248 |
String qName, Attributes atts) throws SAXException { |
249 |
try {
|
250 |
String atributo;
|
251 |
|
252 |
if (localName.equals("renderer")) { |
253 |
logger.debug("renderer");
|
254 |
renderer = renderer(atts); |
255 |
state = 3;
|
256 |
} else if (localName.equals("classes")) { |
257 |
logger.debug("classes");
|
258 |
state = 5;
|
259 |
} else if (localName.equals("valueClass")) { |
260 |
logger.debug("valueClass");
|
261 |
state = 10;
|
262 |
} else if (localName.equals("intervalClass")) { |
263 |
logger.debug("intervalClass");
|
264 |
state = 11;
|
265 |
} else if (localName.equals("value")) { |
266 |
logger.debug("value");
|
267 |
value(atts); |
268 |
state = 13;
|
269 |
} else if (localName.equals("interval")) { |
270 |
logger.debug("value");
|
271 |
interval(atts); |
272 |
state = 14;
|
273 |
} else if (localName.equals("symbol")) { |
274 |
logger.debug("symbol");
|
275 |
|
276 |
FSymbol symbol = symbol(atts); |
277 |
switch (state){
|
278 |
case 3: |
279 |
renderer.setDefaultSymbol(symbol); |
280 |
state = 4;
|
281 |
break;
|
282 |
case 11: |
283 |
renderer.m_legendIntervals.add(currentInterval); |
284 |
renderer.addSymbol(currentInterval, symbol); |
285 |
state = 12;
|
286 |
break;
|
287 |
case 10: |
288 |
renderer.addSymbol(currentValue, symbol); |
289 |
state = 8;
|
290 |
break;
|
291 |
} |
292 |
} |
293 |
} catch (Exception e) { |
294 |
logger.debug("error", e);
|
295 |
throw new SAXException(e); |
296 |
} |
297 |
} |
298 |
|
299 |
/**
|
300 |
* @return
|
301 |
*/
|
302 |
public FRenderer getRenderer() {
|
303 |
return renderer;
|
304 |
} |
305 |
|
306 |
} |