Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / using-sld-model / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / sld1_0_0 / SLDStroke1_0_0.java @ 40758

History | View | Annotate | Download (11.5 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.sldsupport.impl.sld1_0_0;
42

    
43
import java.io.IOException;
44

    
45
import org.gvsig.sldsupport.exception.SLDReadException;
46
import org.gvsig.sldsupport.filterencoding.FExpression;
47
import org.gvsig.sldsupport.filterencoding.FilterTags;
48
import org.gvsig.sldsupport.filterencoding.FilterUtils;
49
import org.gvsig.sldsupport.graphic.SLDStroke;
50
import org.gvsig.sldsupport.util.SLDTags;
51
import org.gvsig.sldsupport.util.SLDUtils;
52
import org.gvsig.sldsupport.util.XmlBuilder;
53
import org.xmlpull.v1.XmlPullParser;
54
import org.xmlpull.v1.XmlPullParserException;
55

    
56
/**
57
 * Implements the Stroke element of an SLD specification which
58
 * encapsulates the graphical-symbolization parameters for linear
59
 * geometries
60
 *
61
 * @see http://portal.opengeospatial.org/files/?artifact_id=1188
62
 *
63
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
64
 */
65
public class SLDStroke1_0_0 extends SLDStroke {
66

    
67
        String cad;
68

    
69
        public void parse(XmlPullParser parser, int cuTag, String expressionType)
70
                        throws XmlPullParserException, IOException, SLDReadException {
71

    
72
                int currentTag;
73
                boolean end = false;
74
                parser.require(XmlPullParser.START_TAG, null, SLDTags.STROKE);
75
                currentTag = parser.next();
76

    
77
                while (!end)
78
                {
79
                        switch(currentTag)
80
                        {
81
                        case XmlPullParser.START_TAG:
82
                                if (parser.getName().compareTo(SLDTags.GRAPHICFILL)==0 ||
83
                                                parser.getName().compareTo(SLDTags.GRAPHICSTROKE)==0)  {
84
                                        String name = parser.getName();
85
                                        SLDGraphic1_0_0 graphic = new SLDGraphic1_0_0();
86
                                        graphic.parse(parser,currentTag,parser.getName());
87
                                        setGraphic(graphic);
88

    
89
                                        if (name.compareTo(SLDTags.GRAPHICFILL)== 0)
90
                                                setHasGraphicFill(true);
91
                                        if (name.compareTo(SLDTags.GRAPHICSTROKE)== 0)
92
                                                setHasGraphicStroke(true);
93
                                }
94
                                else if (parser.getName().compareTo(SLDTags.CSSPARAMETER)==0) {
95
                                        String attributeName = parser.getAttributeValue("", SLDTags.NAME_ATTR);
96
                                        /*
97
                                         * Parche para compatibilizar leyendas generadas con la 1.1.2
98
                                         * donde el atributo "name" se pone con la primera letra mayuscula "Name"
99
                                         */
100
                                        if (attributeName == null){
101
                                                attributeName = parser.getAttributeValue("", SLDTags.NAME_ATTR_VERSAL);
102
                                        }
103
                                        /* Fin del parche */
104
                                        if(attributeName.compareTo(SLDTags.STROKE_WIDTH_ATTR)==0) {
105
                                                FExpression expressionWidth = new FExpression();
106

    
107
                                                try {
108
                                                        expressionWidth.parse(parser,parser.nextTag(), parser.getName());
109
                                                }
110
                                                catch(XmlPullParserException e) {
111
                                                        String s = parser.getText().trim();
112
                                                        expressionWidth.setLiteral(s);
113
                                                }
114

    
115
                                                if (!SLDUtils.isANumber(expressionWidth.getLiteral()))
116
                                                        throw new SLDReadException(new Exception(
117
                                                                        "Stroke width value is not numeric."));
118

    
119
                                                setExpressionWidth(expressionWidth);
120
                                        }
121
                                        else if(attributeName.compareTo(SLDTags.STROKE_ATTR)==0) {
122
                                                FExpression expressionColor = new FExpression();
123

    
124

    
125
                                                try {
126
                                                        expressionColor.parse(parser, parser.nextTag(),parser.getName());
127
                                                }
128
                                                catch(XmlPullParserException e) {
129
                                                        String s = parser.getText().trim();
130
                                                        expressionColor.setLiteral(s);
131
                                                }
132

    
133
                                                if (!SLDUtils.isColor(expressionColor.getLiteral()))
134
                                                        throw new SLDReadException(new Exception(
135
                                                                        "Stroke color value is not a color."));
136

    
137
                                                setExpressionColor(expressionColor);
138
                                        }
139
                                        else if(attributeName.compareTo(SLDTags.STROKE_OPACITY_ATTR)==0) {
140
                                                FExpression expressionOpacity = new FExpression();
141

    
142

    
143

    
144
                                                try {
145
                                                        expressionOpacity.parse(parser,parser.nextTag(), parser.getName());
146
                                                }
147
                                                catch(XmlPullParserException e) {
148
                                                        String s = parser.getText().trim();
149
                                                        expressionOpacity.setLiteral(s);
150
                                                }
151

    
152
                                                if (!SLDUtils.isANumber(expressionOpacity.getLiteral()))
153
                                                        throw new SLDReadException(new Exception(
154
                                                                        "Stroke opacity value is not numeric."));
155

    
156
                                                setExpressionOpacity(expressionOpacity);
157
                                        }
158
                                        else if(attributeName.compareTo(SLDTags.STROKE_LINEJOIN_ATTR)==0) {
159

    
160
                                                FExpression expressionLineJoin = new FExpression();
161

    
162
                                                try {
163
                                                        expressionLineJoin.parse(parser,parser.nextTag(), parser.getName());
164
                                                }
165
                                                catch(XmlPullParserException e) {
166
                                                        String s = parser.getText().trim();
167
                                                        expressionLineJoin.setLiteral(s);
168
                                                }
169

    
170
                                                if (!SLDUtils.isLineJoin(expressionLineJoin.getLiteral()))
171
                                                        throw new SLDReadException(new Exception(
172
                                                                        "Stroke line join value is not valid."));
173

    
174
                                                setExpressionLineJoin(expressionLineJoin);
175
                                        }
176
                                        else if(attributeName.compareTo(SLDTags.STROKE_LINECAP_ATTR)==0) {
177

    
178
                                                FExpression expressionLineCap = new FExpression();
179

    
180
                                                try {
181
                                                        expressionLineCap.parse(parser,parser.nextTag(), parser.getName());
182
                                                }
183
                                                catch(XmlPullParserException e) {
184
                                                        String s = parser.getText().trim();
185
                                                        expressionLineCap.setLiteral(s);
186
                                                }
187

    
188
                                                if (!SLDUtils.isLineCap(expressionLineCap.getLiteral()))
189
                                                        throw new SLDReadException(new Exception(
190
                                                                        "Stroke line cap value is not valid."));
191

    
192
                                                setExpressionLineCap(expressionLineCap);
193
                                        }
194
                                        else if(attributeName.compareTo(SLDTags.STROKE_DASHARRAY_ATTR)==0) {
195

    
196

    
197
                                                FExpression expressionDashArray =  new FExpression();
198
                                                try {
199
                                                        expressionDashArray.parse(parser,parser.nextTag(), parser.getName());
200
                                                        cad = expressionDashArray.getLiteral();
201
                                                        if (cad == null)
202
                                                                cad = parser.getText().trim();
203
                                                }
204
                                                catch(XmlPullParserException e) {
205
                                                        cad = parser.getText().trim();
206
                                                }
207

    
208
                                                if (cad==null /*|| "".equals(cad)*/) {
209
                                                        
210
                                                        throw new SLDReadException(new Exception(
211
                                                                        "Stroke dash expression is not valid."));
212
                                                        
213
                                                } else {
214
                                                        String x="",y="";
215
                                                        int cont = 0;
216

    
217
                                                        for(int i=0;i < cad.length();i++) {
218
                                                                x = String.valueOf(cad.charAt(i));
219
                                                                if(x.compareTo(",") == 0) {
220
                                                                        if (SLDUtils.isANumber(y)) {
221
                                                                                dashArray.add(Float.valueOf(y));
222
                                                                                y = "";
223
                                                                                cont++;
224
                                                                        }
225
                                                                        else
226
                                                                                throw new SLDReadException(new Exception(
227
                                                                                                "Stroke dash expression is not valid."));
228
                                                                }
229
                                                                else {
230
                                                                        if(x.compareTo(" ") != 0)
231
                                                                                y += String.valueOf(cad.charAt(i));
232
                                                                        if ( i == cad.length()-1 ) {
233
                                                                                if (SLDUtils.isANumber(y)) {
234
                                                                                        dashArray.add(Float.valueOf(y));
235
                                                                                        cont++;
236
                                                                                }
237
                                                                                else
238
                                                                                        throw new SLDReadException(new Exception(
239
                                                                                                        "Stroke dash expression is not valid."));
240
                                                                        }
241

    
242
                                                                }
243
                                                        }
244
                                                }
245
                                        }
246

    
247
                                        else if(attributeName.compareTo(SLDTags.STROKE_DASHOFFSET_ATTR)==0) {
248
                                                FExpression expressionDashOffset = new FExpression();
249

    
250

    
251
                                                try {
252
                                                        expressionDashOffset.parse(parser,parser.nextTag(), parser.getName());
253
                                                }
254
                                                catch(XmlPullParserException e) {
255
                                                        String s = parser.getText().trim();
256
                                                        expressionDashOffset.setLiteral(s);
257
                                                }
258

    
259
                                                if (!SLDUtils.isANumber(expressionDashOffset.getLiteral()))
260
                                                        throw new SLDReadException(new Exception(
261
                                                                        "Stroke dash offset expression is not valid."));
262

    
263
                                                setExpressionDashOffset(expressionDashOffset);
264
                                        }
265
                                }
266

    
267
                                break;
268
                        case XmlPullParser.END_TAG:
269
                                if (parser.getName().compareTo(SLDTags.STROKE) == 0)
270
                                        end = true;
271
                                break;
272
                        case XmlPullParser.TEXT:
273
                                break;
274
                        }
275
                        if (!end)
276
                                currentTag = parser.next();
277
                }
278

    
279
                parser.require(XmlPullParser.END_TAG, null, SLDTags.STROKE);
280

    
281
        }
282

    
283

    
284
        private void parserDashArray(XmlPullParser parser, int i, String name) throws XmlPullParserException, IOException, SLDReadException {
285

    
286
                int currentTag;
287
                boolean end = false;
288
                currentTag = i;
289

    
290
                while (!end)
291
                {
292
                        switch(currentTag)
293
                        {
294
                        case XmlPullParser.START_TAG:
295

    
296
                                if (parser.getName().compareTo(FilterUtils.remNameSpace(FilterTags.LITERAL))==0) {
297
                                        this.cad = parser.nextText();
298
                                        end = true;
299

    
300
                                }
301
                        case XmlPullParser.END_TAG:
302
                                if (parser.getName().compareTo(FilterUtils.remNameSpace(SLDTags.CSSPARAMETER)) == 0) {
303
                                        parser.next();
304
                                }
305
                        }
306
                        break;
307
                }
308
                if (!end)
309
                        currentTag = parser.next();
310

    
311
        }
312

    
313

    
314
        public String toXML() {
315
                XmlBuilder xmlBuilder = new XmlBuilder();
316

    
317
                xmlBuilder.openTag(SLDTags.STROKE);
318
                if (getGraphic() != null) {
319
                        xmlBuilder.writeRaw(getGraphic().toXML());
320
                }
321
                if(getExpressionColor().getLiteral() != null){
322
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_ATTR);
323
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionColor().getLiteral());
324
                        xmlBuilder.closeTag();
325
                }
326
                if(getExpressionOpacity().getLiteral() != null){
327
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_OPACITY_ATTR);
328
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionOpacity().getLiteral());
329
                        xmlBuilder.closeTag();
330
                }
331
                if(getExpressionWidth().getLiteral() != null){
332
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_WIDTH_ATTR);
333
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionWidth().getLiteral());
334
                        xmlBuilder.closeTag();
335
                }
336
                if(getExpressionLineCap().getLiteral() != null){
337
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_LINECAP_ATTR);
338
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionLineCap().getLiteral());
339
                        xmlBuilder.closeTag();
340
                }
341
                if(getExpressionLineJoin().getLiteral() != null){
342
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_LINEJOIN_ATTR);
343
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionLineJoin().getLiteral());
344
                        xmlBuilder.closeTag();
345
                }
346
                if(getDashArray() != null && getDashArray().size() > 0){
347
                        String myDash = "";
348
                        for (int i = 0; i <getDashArray().size(); i++) {
349
                                myDash += getDashArray().get(i);
350
                                if ((i+1) < getDashArray().size())
351
                                        myDash += ", ";
352
                        }
353

    
354
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_DASHARRAY_ATTR);
355
                        xmlBuilder.writeTag(FilterTags.LITERAL, myDash);
356
                        xmlBuilder.closeTag();
357
                }
358
                if(getExpressionDashOffset().getLiteral() != null){
359
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_DASHOFFSET_ATTR);
360
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionDashOffset().getLiteral());
361
                        xmlBuilder.closeTag();
362
                }
363
                xmlBuilder.closeTag();
364

    
365
                return xmlBuilder.getXML();
366
        }
367

    
368

    
369

    
370
}