gvsig-sldtools / org.gvsig.sldsupport / trunk / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / sld / parsing / StrokeElement.java @ 36
History | View | Annotate | Download (11.1 KB)
1 |
/*******************************************************************************
|
---|---|
2 |
*
|
3 |
* gvSIG. Desktop Geographic Information System.
|
4 |
*
|
5 |
* Copyright (C) 2007-2013 gvSIG Association.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 3
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
20 |
* MA 02110-1301, USA.
|
21 |
*
|
22 |
* For any additional information, do not hesitate to contact us
|
23 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
24 |
*
|
25 |
*******************************************************************************/
|
26 |
package org.gvsig.sldsupport.impl.sld.parsing; |
27 |
|
28 |
import java.io.IOException; |
29 |
import java.util.List; |
30 |
|
31 |
import org.xmlpull.v1.XmlPullParser; |
32 |
import org.xmlpull.v1.XmlPullParserException; |
33 |
|
34 |
import org.gvsig.sldsupport.exception.InvalidSLDObjectException; |
35 |
import org.gvsig.sldsupport.exception.SLDReadException; |
36 |
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException; |
37 |
import org.gvsig.sldsupport.impl.util.SLDUtils; |
38 |
import org.gvsig.sldsupport.impl.util.XmlBuilder; |
39 |
import org.gvsig.sldsupport.sld.SLDTags; |
40 |
import org.gvsig.sldsupport.sld.filter.expression.SLDExpression; |
41 |
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDLiteral; |
42 |
import org.gvsig.sldsupport.sld.graphic.SLDGraphicFill; |
43 |
import org.gvsig.sldsupport.sld.graphic.SLDGraphicStroke; |
44 |
import org.gvsig.sldsupport.sld.symbol.misc.SLDParameterValue; |
45 |
import org.gvsig.sldsupport.sld.symbol.misc.SLDStroke; |
46 |
import org.gvsig.sldsupport.sld.symbol.misc.SLDSvgCssParameter; |
47 |
|
48 |
public class StrokeElement { |
49 |
|
50 |
public static void append(SLDStroke obj, XmlBuilder xb, String version) |
51 |
throws InvalidSLDObjectException, UnsupportedSLDObjectException {
|
52 |
|
53 |
SLDParameterValue pv = null;
|
54 |
|
55 |
xb.openTag(SLDTags.STROKE); |
56 |
// ==========================
|
57 |
if (obj.getStrokeType() == SLDStroke.STROKE_TYPE_GRAPHIC_FILL) {
|
58 |
if (obj.getGraphic() == null) { |
59 |
throw new InvalidSLDObjectException(SLDTags.STROKE, |
60 |
"No graphic found as graphic fill");
|
61 |
} |
62 |
SLDGraphicFill gf = new SLDGraphicFill(obj.getGraphic());
|
63 |
GraphicFillElement.append(gf, xb, version); |
64 |
} else {
|
65 |
if (obj.getStrokeType() == SLDStroke.STROKE_TYPE_GRAPHIC_STROKE) {
|
66 |
if (obj.getGraphic() == null) { |
67 |
throw new InvalidSLDObjectException(SLDTags.STROKE, |
68 |
"No graphic found as graphic stroke");
|
69 |
} |
70 |
SLDGraphicStroke gs = new SLDGraphicStroke(obj.getGraphic());
|
71 |
if (obj.getGraphicStrokeGap() != null) { |
72 |
pv = new SLDParameterValue();
|
73 |
pv.getExpressionList().add( |
74 |
new SLDLiteral(SLDUtils.df.format(obj
|
75 |
.getGraphicStrokeGap().doubleValue()))); |
76 |
gs.setGap(pv); |
77 |
} |
78 |
if (obj.getGraphicStrokeInitialGap() != null) { |
79 |
pv = new SLDParameterValue();
|
80 |
pv.getExpressionList().add( |
81 |
new SLDLiteral(SLDUtils.df.format(obj
|
82 |
.getGraphicStrokeInitialGap().doubleValue()))); |
83 |
gs.setInitialGap(pv); |
84 |
} |
85 |
// ============
|
86 |
GraphicStrokeElement.append(gs, xb, version); |
87 |
// ================================
|
88 |
} |
89 |
} |
90 |
|
91 |
// ===================
|
92 |
if (obj.getColor() != null) { |
93 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
94 |
p.setName(SLDTags.STROKE_ATTR); |
95 |
p.getExpressionList().add(obj.getColor()); |
96 |
SvgCssParameterElement.append(p, xb, version); |
97 |
} |
98 |
|
99 |
// ===================
|
100 |
if (obj.getWidth() != null) { |
101 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
102 |
p.setName(SLDTags.STROKE_WIDTH_ATTR); |
103 |
p.getExpressionList().add(obj.getWidth()); |
104 |
SvgCssParameterElement.append(p, xb, version); |
105 |
} |
106 |
|
107 |
|
108 |
// ===================
|
109 |
if (obj.getOpacity() != null) { |
110 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
111 |
p.setName(SLDTags.STROKE_OPACITY_ATTR); |
112 |
p.getExpressionList().add(obj.getOpacity()); |
113 |
SvgCssParameterElement.append(p, xb, version); |
114 |
} |
115 |
// ======
|
116 |
if (obj.getLineCap() != null) { |
117 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
118 |
p.setName(SLDTags.STROKE_LINECAP_ATTR); |
119 |
p.getExpressionList().add(obj.getLineCap()); |
120 |
SvgCssParameterElement.append(p, xb, version); |
121 |
} |
122 |
// ======
|
123 |
if (obj.getLineJoin() != null) { |
124 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
125 |
p.setName(SLDTags.STROKE_LINEJOIN_ATTR); |
126 |
p.getExpressionList().add(obj.getLineJoin()); |
127 |
SvgCssParameterElement.append(p, xb, version); |
128 |
} |
129 |
// ======
|
130 |
if (obj.getDashOffset() != null) { |
131 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
132 |
p.setName(SLDTags.STROKE_DASHOFFSET_ATTR); |
133 |
p.getExpressionList().add(obj.getDashOffset()); |
134 |
SvgCssParameterElement.append(p, xb, version); |
135 |
} |
136 |
// ======
|
137 |
List<Float> list = obj.getDashArray(); |
138 |
if ((list != null) && (list.size() > 0)) { |
139 |
String arr = SLDUtils.getAsString(list, " "); |
140 |
SLDSvgCssParameter p = new SLDSvgCssParameter();
|
141 |
p.setName(SLDTags.STROKE_DASHARRAY_ATTR); |
142 |
p.getExpressionList().add(new SLDLiteral(arr));
|
143 |
SvgCssParameterElement.append(p, xb, version); |
144 |
} |
145 |
// ==========================
|
146 |
// ==========================
|
147 |
xb.closeTag(); |
148 |
} |
149 |
|
150 |
public static SLDStroke parse(XmlPullParser parser, String version) |
151 |
throws XmlPullParserException, IOException, SLDReadException { |
152 |
|
153 |
parser.require(XmlPullParser.START_TAG, null, SLDTags.STROKE);
|
154 |
int tag = 0; |
155 |
|
156 |
SLDStroke resp = new SLDStroke();
|
157 |
|
158 |
tag = parser.nextTag(); |
159 |
String name = parser.getName();
|
160 |
String txt = null; |
161 |
String attName = null; |
162 |
while (!(SLDUtils.isStr(name, SLDTags.STROKE) && (tag == XmlPullParser.END_TAG))) {
|
163 |
|
164 |
switch (tag) {
|
165 |
case XmlPullParser.START_TAG:
|
166 |
if (SLDUtils.isStr(name, SLDTags.GRAPHICFILL)) {
|
167 |
SLDGraphicFill gf = |
168 |
GraphicFillElement.parse(parser, version); |
169 |
resp.setStrokeType(SLDStroke.STROKE_TYPE_GRAPHIC_FILL); |
170 |
resp.setGraphic(gf.getGraphic()); |
171 |
break;
|
172 |
} |
173 |
if (SLDUtils.isStr(name, SLDTags.GRAPHICSTROKE)) {
|
174 |
SLDGraphicStroke gf = |
175 |
GraphicStrokeElement.parse(parser, version); |
176 |
resp.setStrokeType(SLDStroke.STROKE_TYPE_GRAPHIC_STROKE); |
177 |
resp.setGraphic(gf.getGraphic()); |
178 |
break;
|
179 |
} |
180 |
if (SLDUtils.isStr(name, SLDTags.CSSPARAMETER)
|
181 |
|| SLDUtils.isStr(name, SLDTags.SVGPARAMETER)) { |
182 |
|
183 |
SLDSvgCssParameter sp = |
184 |
SvgCssParameterElement.parse(parser, version); |
185 |
if (SLDUtils.isStr(sp.getName(), SLDTags.STROKE_ATTR)) {
|
186 |
if (sp.getExpressionList().size() > 0) { |
187 |
// Using first item only
|
188 |
resp.setColor(sp.getExpressionList().get(0));
|
189 |
} |
190 |
break;
|
191 |
} |
192 |
if (SLDUtils.isStr(sp.getName(),
|
193 |
SLDTags.STROKE_OPACITY_ATTR)) { |
194 |
if (sp.getExpressionList().size() > 0) { |
195 |
resp.setOpacity(sp.getExpressionList().get(0));
|
196 |
} |
197 |
break;
|
198 |
} |
199 |
if (SLDUtils.isStr(sp.getName(), SLDTags.STROKE_WIDTH_ATTR)) {
|
200 |
if (sp.getExpressionList().size() > 0) { |
201 |
resp.setWidth(sp.getExpressionList().get(0));
|
202 |
} |
203 |
break;
|
204 |
} |
205 |
if (SLDUtils.isStr(sp.getName(),
|
206 |
SLDTags.STROKE_LINEJOIN_ATTR)) { |
207 |
if (sp.getExpressionList().size() > 0) { |
208 |
resp.setLineJoin(sp.getExpressionList().get(0));
|
209 |
} |
210 |
break;
|
211 |
} |
212 |
if (SLDUtils.isStr(sp.getName(),
|
213 |
SLDTags.STROKE_LINECAP_ATTR)) { |
214 |
if (sp.getExpressionList().size() > 0) { |
215 |
resp.setLineCap(sp.getExpressionList().get(0));
|
216 |
} |
217 |
break;
|
218 |
} |
219 |
if (SLDUtils.isStr(sp.getName(),
|
220 |
SLDTags.STROKE_DASHOFFSET_ATTR)) { |
221 |
if (sp.getExpressionList().size() > 0) { |
222 |
resp.setDashOffset(sp.getExpressionList().get(0));
|
223 |
} |
224 |
break;
|
225 |
} |
226 |
if (SLDUtils.isStr(sp.getName(),
|
227 |
SLDTags.STROKE_DASHARRAY_ATTR)) { |
228 |
if (sp.getExpressionList().size() > 0) { |
229 |
SLDExpression expr = sp.getExpressionList().get(0);
|
230 |
if (expr instanceof SLDLiteral) { |
231 |
SLDLiteral lite = (SLDLiteral) expr; |
232 |
List<Float> floats = |
233 |
SLDUtils.getAsFloats(lite.getValue(), " ");
|
234 |
resp.getDashArray().addAll(floats); |
235 |
} |
236 |
} |
237 |
break;
|
238 |
} |
239 |
break;
|
240 |
} |
241 |
/*
|
242 |
* Any other entity causes parsing error
|
243 |
*/
|
244 |
throw new SLDReadException( |
245 |
"Bad SLD file. Unexpected entity in stroke: " + name);
|
246 |
case XmlPullParser.END_TAG:
|
247 |
break;
|
248 |
case XmlPullParser.TEXT:
|
249 |
break;
|
250 |
} |
251 |
tag = parser.getEventType(); |
252 |
name = parser.getName(); |
253 |
} |
254 |
|
255 |
parser.nextTag(); |
256 |
return resp;
|
257 |
} |
258 |
} |