Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / remoteClient / sld / sld1_0_0 / SLDStroke1_0_0.java @ 29013

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.remoteClient.sld.sld1_0_0;
42

    
43
import java.io.IOException;
44

    
45
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
46
import org.gvsig.remoteClient.sld.SLDStroke;
47
import org.gvsig.remoteClient.sld.SLDTags;
48
import org.gvsig.remoteClient.sld.SLDUtils;
49
import org.gvsig.remoteClient.sld.filterEncoding.FExpression;
50
import org.gvsig.remoteClient.sld.filterEncoding.FilterTags;
51
import org.gvsig.remoteClient.sld.filterEncoding.FilterUtils;
52
import org.xmlpull.v1.XmlPullParserException;
53

    
54
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException;
55
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder;
56

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

    
68
        String cad;
69

    
70
        public void parse(XMLSchemaParser parser, int cuTag, String expressionType) throws XmlPullParserException, IOException, LegendDriverException {
71

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

    
77
                while (!end)
78
                {
79
                        switch(currentTag)
80
                        {
81
                        case XMLSchemaParser.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 LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
117

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

    
123

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

    
132
                                                if (!SLDUtils.isColor(expressionColor.getLiteral()))
133
                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
134

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

    
140

    
141

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

    
150
                                                if (!SLDUtils.isANumber(expressionOpacity.getLiteral()))
151
                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
152

    
153
                                                setExpressionOpacity(expressionOpacity);
154
                                        }
155
                                        else if(attributeName.compareTo(SLDTags.STROKE_LINEJOIN_ATTR)==0) {
156

    
157
                                                FExpression expressionLineJoin = new FExpression();
158

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

    
167
                                                if (!SLDUtils.isLineJoin(expressionLineJoin.getLiteral()))
168
                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
169

    
170
                                                setExpressionLineJoin(expressionLineJoin);
171
                                        }
172
                                        else if(attributeName.compareTo(SLDTags.STROKE_LINECAP_ATTR)==0) {
173

    
174
                                                FExpression expressionLineCap = new FExpression();
175

    
176
                                                try {
177
                                                        expressionLineCap.parse(parser,parser.nextTag(), parser.getName());
178
                                                }
179
                                                catch(XmlPullParserException e) {
180
                                                        String s = parser.getText().trim();
181
                                                        expressionLineCap.setLiteral(s);
182
                                                }
183

    
184
                                                if (!SLDUtils.isLineCap(expressionLineCap.getLiteral()))
185
                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
186

    
187
                                                setExpressionLineCap(expressionLineCap);
188
                                        }
189
                                        else if(attributeName.compareTo(SLDTags.STROKE_DASHARRAY_ATTR)==0) {
190

    
191

    
192
                                                FExpression expressionDashArray =  new FExpression();
193
                                                try {
194
                                                        expressionDashArray.parse(parser,parser.nextTag(), parser.getName());
195
                                                        cad = expressionDashArray.getLiteral();
196
                                                        if (cad == null)
197
                                                                cad = parser.getText().trim();
198
                                                }
199
                                                catch(XmlPullParserException e) {
200
                                                        cad = parser.getText().trim();
201
                                                }
202

    
203
                                                if (cad==null /*|| "".equals(cad)*/) {
204
                                                        throw new LegendDriverException (LegendDriverException.PARSE_LEGEND_FILE_ERROR);
205
                                                } else {
206
                                                        String x="",y="";
207
                                                        int cont = 0;
208

    
209
                                                        for(int i=0;i < cad.length();i++) {
210
                                                                x = String.valueOf(cad.charAt(i));
211
                                                                if(x.compareTo(",") == 0) {
212
                                                                        if (SLDUtils.isANumber(y)) {
213
                                                                                dashArray.add(Float.valueOf(y));
214
                                                                                y = "";
215
                                                                                cont++;
216
                                                                        }
217
                                                                        else
218
                                                                                throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
219
                                                                }
220
                                                                else {
221
                                                                        if(x.compareTo(" ") != 0)
222
                                                                                y += String.valueOf(cad.charAt(i));
223
                                                                        if ( i == cad.length()-1 ) {
224
                                                                                if (SLDUtils.isANumber(y)) {
225
                                                                                        dashArray.add(Float.valueOf(y));
226
                                                                                        cont++;
227
                                                                                }
228
                                                                                else
229
                                                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
230
                                                                        }
231

    
232
                                                                }
233
                                                        }
234
                                                }
235
                                        }
236

    
237
                                        else if(attributeName.compareTo(SLDTags.STROKE_DASHOFFSET_ATTR)==0) {
238
                                                FExpression expressionDashOffset = new FExpression();
239

    
240

    
241
                                                try {
242
                                                        expressionDashOffset.parse(parser,parser.nextTag(), parser.getName());
243
                                                }
244
                                                catch(XmlPullParserException e) {
245
                                                        String s = parser.getText().trim();
246
                                                        expressionDashOffset.setLiteral(s);
247
                                                }
248

    
249
                                                if (!SLDUtils.isANumber(expressionDashOffset.getLiteral()))
250
                                                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
251

    
252
                                                setExpressionDashOffset(expressionDashOffset);
253
                                        }
254
                                }
255

    
256
                                break;
257
                        case XMLSchemaParser.END_TAG:
258
                                if (parser.getName().compareTo(SLDTags.STROKE) == 0)
259
                                        end = true;
260
                                break;
261
                        case XMLSchemaParser.TEXT:
262
                                break;
263
                        }
264
                        if (!end)
265
                                currentTag = parser.next();
266
                }
267

    
268
                parser.require(XMLSchemaParser.END_TAG, null, SLDTags.STROKE);
269

    
270
        }
271

    
272

    
273
        private void parserDashArray(XMLSchemaParser parser, int i, String name) throws XmlPullParserException, IOException, LegendDriverException {
274

    
275
                int currentTag;
276
                boolean end = false;
277
                currentTag = i;
278

    
279
                while (!end)
280
                {
281
                        switch(currentTag)
282
                        {
283
                        case XMLSchemaParser.START_TAG:
284

    
285
                                if (parser.getName().compareTo(FilterUtils.remNameSpace(FilterTags.LITERAL))==0) {
286
                                        this.cad = parser.nextText();
287
                                        end = true;
288

    
289
                                }
290
                        case XMLSchemaParser.END_TAG:
291
                                if (parser.getName().compareTo(FilterUtils.remNameSpace(SLDTags.CSSPARAMETER)) == 0) {
292
                                        parser.next();
293
                                }
294
                        }
295
                        break;
296
                }
297
                if (!end)
298
                        currentTag = parser.next();
299

    
300
        }
301

    
302

    
303
        public String toXML() {
304
                XmlBuilder xmlBuilder = new XmlBuilder();
305

    
306
                xmlBuilder.openTag(SLDTags.STROKE);
307
                if (getGraphic() != null) {
308
                        xmlBuilder.writeRaw(getGraphic().toXML());
309
                }
310
                if(getExpressionColor().getLiteral() != null){
311
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_ATTR);
312
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionColor().getLiteral());
313
                        xmlBuilder.closeTag();
314
                }
315
                if(getExpressionOpacity().getLiteral() != null){
316
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_OPACITY_ATTR);
317
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionOpacity().getLiteral());
318
                        xmlBuilder.closeTag();
319
                }
320
                if(getExpressionWidth().getLiteral() != null){
321
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_WIDTH_ATTR);
322
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionWidth().getLiteral());
323
                        xmlBuilder.closeTag();
324
                }
325
                if(getExpressionLineCap().getLiteral() != null){
326
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_LINECAP_ATTR);
327
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionLineCap().getLiteral());
328
                        xmlBuilder.closeTag();
329
                }
330
                if(getExpressionLineJoin().getLiteral() != null){
331
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_LINEJOIN_ATTR);
332
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionLineJoin().getLiteral());
333
                        xmlBuilder.closeTag();
334
                }
335
                if(getDashArray() != null && getDashArray().size() > 0){
336
                        String myDash = "";
337
                        for (int i = 0; i <getDashArray().size(); i++) {
338
                                myDash += getDashArray().get(i);
339
                                if ((i+1) < getDashArray().size())
340
                                        myDash += ", ";
341
                        }
342

    
343
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_DASHARRAY_ATTR);
344
                        xmlBuilder.writeTag(FilterTags.LITERAL, myDash);
345
                        xmlBuilder.closeTag();
346
                }
347
                if(getExpressionDashOffset().getLiteral() != null){
348
                        xmlBuilder.openTag(SLDTags.CSSPARAMETER,SLDTags.NAME_ATTR,SLDTags.STROKE_DASHOFFSET_ATTR);
349
                        xmlBuilder.writeTag(FilterTags.LITERAL, getExpressionDashOffset().getLiteral());
350
                        xmlBuilder.closeTag();
351
                }
352
                xmlBuilder.closeTag();
353

    
354
                return xmlBuilder.getXML();
355
        }
356

    
357

    
358

    
359
}