Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / org.gvsig.sldsupport / org.gvsig.sldsupport.basic / src / main / java / org / gvsig / sldsupport / basic / sld1_0_0 / BasicSLDUtils.java @ 40754

History | View | Annotate | Download (41.6 KB)

1
package org.gvsig.sldsupport.basic.sld1_0_0;
2

    
3
import java.awt.BasicStroke;
4
import java.awt.Color;
5
import java.io.File;
6
import java.io.IOException;
7
import java.io.OutputStream;
8
import java.io.OutputStreamWriter;
9
import java.net.URL;
10
import java.net.UnknownHostException;
11
import java.text.DecimalFormat;
12
import java.text.DecimalFormatSymbols;
13
import java.text.NumberFormat;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17

    
18
import org.gvsig.fmap.dal.exception.DataException;
19
import org.gvsig.fmap.dal.feature.FeatureStore;
20
import org.gvsig.fmap.dal.feature.FeatureType;
21
import org.gvsig.fmap.geom.Geometry;
22
import org.gvsig.fmap.mapcontext.MapContextLocator;
23
import org.gvsig.fmap.mapcontext.MapContextManager;
24
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
25
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
26
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
27
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
28
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
29
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
30
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
31
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
32
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
33
import org.gvsig.sldsupport.basic.handler.SLDProtocolHandler;
34
import org.gvsig.sldsupport.exception.SLDReadException;
35
import org.gvsig.sldsupport.exception.SLDWriteException;
36
import org.gvsig.sldsupport.filterencoding.FExpression;
37
import org.gvsig.sldsupport.filterencoding.Filter;
38
import org.gvsig.sldsupport.filterencoding.FilterTags;
39
import org.gvsig.sldsupport.graphic.SLDExternalGraphic;
40
import org.gvsig.sldsupport.graphic.SLDFill;
41
import org.gvsig.sldsupport.graphic.SLDGraphic;
42
import org.gvsig.sldsupport.graphic.SLDMark;
43
import org.gvsig.sldsupport.graphic.SLDStroke;
44
import org.gvsig.sldsupport.layer.ISLDLayer;
45
import org.gvsig.sldsupport.symbolizer.ISLDSymbolizer;
46
import org.gvsig.sldsupport.symbolizer.SLDLineSymbolizer;
47
import org.gvsig.sldsupport.symbolizer.SLDMultiLineSymbolizer;
48
import org.gvsig.sldsupport.symbolizer.SLDMultiPointSymbolizer;
49
import org.gvsig.sldsupport.symbolizer.SLDMultiPolygonSymbolizer;
50
import org.gvsig.sldsupport.symbolizer.SLDPointSymbolizer;
51
import org.gvsig.sldsupport.symbolizer.SLDPolygonSymbolizer;
52
import org.gvsig.sldsupport.util.SLDTags;
53
import org.gvsig.sldsupport.util.SLDUtils;
54
import org.gvsig.sldsupport.util.XmlBuilder;
55
import org.gvsig.sldsupport.version.sld1_0_0.SLDExternalGraphic1_0_0;
56
import org.gvsig.sldsupport.version.sld1_0_0.SLDFill1_0_0;
57
import org.gvsig.sldsupport.version.sld1_0_0.SLDGraphic1_0_0;
58
import org.gvsig.sldsupport.version.sld1_0_0.SLDMark1_0_0;
59
import org.gvsig.sldsupport.version.sld1_0_0.SLDStroke1_0_0;
60
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDLineSymbolizer1_0_0;
61
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDMultiLineSymbolizer1_0_0;
62
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDMultiPointSymbolizer1_0_0;
63
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDMultiPolygonSymbolizer1_0_0;
64
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDPointSymbolizer1_0_0;
65
import org.gvsig.sldsupport.version.sld1_0_0.symbolizers.SLDPolygonSymbolizer1_0_0;
66
import org.gvsig.symbology.SymbologyLocator;
67
import org.gvsig.symbology.SymbologyManager;
68
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.IMultiShapeSymbol;
69
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
70
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMarkerFillSymbol;
71
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMultiLayerFillSymbol;
72
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IPictureFillSymbol;
73
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
74
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
75
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.IMultiLayerLineSymbol;
76
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.IPictureLineSymbol;
77
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
78
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
79
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMultiLayerMarkerSymbol;
80
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
81
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
82
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
83
import org.slf4j.Logger;
84
import org.slf4j.LoggerFactory;
85

    
86
/**
87
 * 
88
 * Basic symbology utility methods for SLD version 1.0.0
89
 * 
90
 * @author jldominguez
91
 *
92
 */
93
public class BasicSLDUtils {
94

    
95
        private static Logger logger = LoggerFactory.getLogger(BasicSLDUtils.class);
96
        
97
        public static final String VERSION = "1.0.0";
98
        private static final String DESCRIPTION = "Styled Layer Descriptor";
99
        private static final String FILE_EXTENSION = "sld";
100
        private static final String DEFAULT_ENCODING = "ISO-8859-1";
101
        
102
        private static SLDProtocolHandler handler = new SLDProtocolHandler1_0_0();
103
        
104
        private static MapContextManager mcoMan = null;
105
        private static SymbologyManager symMan = null;
106
        private static SymbolManager syMan = null;
107
        
108
        public static List<ISLDLayer> readSLDLayers(File sld_file)
109
        throws SLDReadException {
110
                
111
                try {
112
                        handler.parse(sld_file);
113
                } catch (Exception e) {
114
                        throw new SLDReadException(e);
115
                }
116
                
117
                List<ISLDLayer> sld_layers = handler.getLayers();
118
                return sld_layers;
119
        }
120
        
121
        
122
        public static ILegend createLegendFor(ISLDLayer sldLayer, VectorLayer vect)
123
                        throws NumberFormatException, SLDReadException, DataException {
124
                
125
                ArrayList<Filter> filters = sldLayer.getLayerFilters();
126
                String[] fNames = sldLayer.getFieldNames();
127

    
128
                if (!checkFieldNames(fNames, vect)) {
129
                        throw new SLDReadException(new Exception(
130
                                        "SLD layer uses fields which are not available in layer: " + vect.getName()));
131
                        
132
                }
133

    
134
                //Multishape not yet supported
135
                if (vect.getShapeType() == Geometry.TYPES.GEOMETRY ||
136
                                vect.getShapeType() == Geometry.TYPES.AGGREGATE) {
137
                        throw new SLDReadException(new Exception(
138
                                        "Multi/aggregate geometry type layers not supported."));
139
                }
140

    
141
                //SingleSymbolLegend
142
                else if (!sldLayer.layerHasFilterForSymbolizers(vect.getShapeType())) {
143
                        
144
                        ISingleSymbolLegend leg = (ISingleSymbolLegend)
145
                                        mcoMan().createLegend(ISingleSymbolLegend.LEGEND_NAME);
146
                        
147
                        ArrayList<ISLDSymbolizer> symbolizers;
148
                        symbolizers = sldLayer.getSymbolizersByShapeType(vect.getShapeType());
149
                        
150
                        IMultiLayerSymbol msym = null;
151
                        msym = syMan().createMultiLayerSymbol(vect.getShapeType());
152

    
153
                        if (msym != null) {
154
                                for (int i = 0; i < symbolizers.size(); i++) {
155
                                        ISymbol sym = SLDSymbolizer2ISymbol(symbolizers.get(i));
156
                                        msym.addLayer(sym);
157
                                }
158
                                ISymbol defaultSym = (msym.getLayerCount() > 1) ?
159
                                                msym : msym.getLayer(0);
160

    
161
                                defaultSym.setDescription("Default");
162
                                leg.setDefaultSymbol(defaultSym);
163
                                return leg;
164
                        }
165
                        throw new SLDReadException(new Exception(
166
                                        "Unable to parse symbol in SingleSymbolLegend."));
167
                }
168
                
169
                throw new SLDReadException(new Exception(
170
                                "Unsupported SLD legend."));
171
                /*
172
                 * 
173
                //VectorialFilterExpressionLegend
174
                else  {
175
                        IClassifiedVectorLegend lege = (IClassifiedVectorLegend)
176
                                        mcoMan().createLegend("FILTER_EXPRESSION_LEGEND_NAME");
177
                        
178
                        if (lege == null) {
179
                                throw new SLDReadException(new Exception("Filter expression legend not available."));
180
                        }
181
                        for (int i = 0; i < filters.size(); i++) {
182
                                for (int j = 0; j < filters.get(i).getSymbolizers().size(); j++) {
183

184
                                        ISLDSymbolizer sldSym = filters.get(i).getSymbolizers().get(j);
185
                                        ISymbol sym = SLDSymbolizer2ISymbol(sldSym);
186
                                        sym.setDescription(filters.get(i).getExpression().toString());
187
                                        lege.addSymbol(filters.get(i).getExpression().toString(), sym);
188
                                }
189
                        }
190
                        return lege;
191
                }
192
                */
193
        }
194
        
195
        
196
        private static boolean checkFieldNames(String[] fieldNames, VectorLayer lyr)
197
                        throws DataException {
198

    
199
                FeatureStore sto = lyr.getFeatureStore();
200
                FeatureType fty = sto.getDefaultFeatureType();
201
                
202
                for (int i = 0; i < fieldNames.length; i++) {
203
                        if (fty.getAttributeDescriptor(fieldNames[i]) == null) {
204
                                return false;
205
                        }
206
                }
207
                return true;
208
        }        
209
        
210
        private static SymbolManager syMan() {
211
                if (syMan == null) {
212
                        syMan = MapContextLocator.getSymbolManager();
213
                }
214
                return syMan;
215
        }
216
        
217
        private static MapContextManager mcoMan() {
218
                if (mcoMan == null) {
219
                        mcoMan = MapContextLocator.getMapContextManager();
220
                }
221
                return mcoMan;
222
        }
223
        
224
        private static SymbologyManager symMan() {
225
                if (symMan == null) {
226
                        symMan = SymbologyLocator.getSymbologyManager();
227
                }
228
                return symMan;
229
        }
230
        
231
        
232
        /**
233
         * Transforms a SLD Symbolizer into an ISymbol.
234
         *
235
         * @param symbolizer to be transfomed into an ISymbol
236
         * @return ISymbol
237
         * @throws NumberFormatException
238
         * @throws LegendDriverException
239
         * @throws UnknownHostException
240
         */
241
        private static ISymbol SLDSymbolizer2ISymbol(ISLDSymbolizer symbolizer)
242
                        throws NumberFormatException, SLDReadException {
243
                
244
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
245
                //LINE//
246
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
247
                if (symbolizer instanceof SLDLineSymbolizer) {
248
                        SLDLineSymbolizer myLine = (SLDLineSymbolizer) symbolizer;
249

    
250
                        ISimpleLineStyle simplLine = symMan().createSimpleLineStyle();
251
                        int lineCap;
252
                        if (myLine.getStroke().getExpressionLineCap().getLiteral() != null)
253
                                lineCap = myLine.getStroke().getLineCap();
254
                        else lineCap = BasicStroke.CAP_SQUARE;
255

    
256
                        int lineJoin;
257
                        if (myLine.getStroke().getExpressionLineJoin().getLiteral() != null)
258
                                lineJoin = myLine.getStroke().getLineJoin();
259
                        else lineJoin = BasicStroke.JOIN_MITER;
260

    
261
                        Float dashOffset;
262
                        if (myLine.getStroke().getExpressionDashOffset().getLiteral() != null)
263
                                dashOffset = Float.valueOf(myLine.getStroke().getExpressionDashOffset().getLiteral());
264
                        else dashOffset = 0f;
265

    
266
                        Float width;
267
                        if (myLine.getStroke().getExpressionWidth().getLiteral()!= null)
268
                                width = myLine.getStroke().getStrokeWidth();
269
                        else width = 0f;
270

    
271
                        float[] dash;
272
                        if(myLine.getStroke().getFloatDashArray().length == 0) {
273
                                dash = null;
274
                        }
275
                        else dash = myLine.getStroke().getFloatDashArray();
276

    
277
                        BasicStroke str = new BasicStroke(width,
278
                                        lineCap,
279
                                        lineJoin,
280
                                        10,
281
                                        dash,
282
                                        dashOffset);
283

    
284

    
285
                        simplLine.setStroke(str);
286

    
287
                        //SimpleLineSymbol
288
                        if ( !myLine.getStroke().isHasGraphicFill() && !myLine.getStroke().isHasGraphicStroke() ) {
289
                                
290
                                ISimpleLineSymbol line = symMan().createSimpleLineSymbol();
291
                                line.setLineStyle(simplLine);
292
                                line.setUnit(-1);
293
                                line.setLineColor(myLine.getStroke().getStrokeColor());
294
                                if(myLine.getStroke().getExpressionOpacity().getLiteral() != null)
295
                                        line.setAlpha((int)(255 * Float.valueOf(myLine.getStroke().getExpressionOpacity().getLiteral())));
296
                                if (myLine.getStroke().getExpressionWidth().getLiteral() != null)
297
                                        line.setLineWidth(myLine.getStroke().getStrokeWidth());
298
                                return line;
299
                        }
300
                        else {
301
                                //MarkerLineSymbol
302
                                IPictureLineSymbol line = null;
303
                                //If the marker is an instance of SimpleMarkerSymbol
304
                                if(myLine.getStroke().getGraphic().getMarks().size() > 0) {
305
                                        /*
306
                                         * TODO We need MarkerLineSymbol in gvSIG 2
307
                                         * 
308
                                        SLDMark myMark = myLine.getStroke().getGraphic().getMarks().get(0);
309
                                        FExpression size = myLine.getStroke().getGraphic().getExpressionSize();
310
                                        FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation();
311
                                        ISimpleMarkerSymbol marker = createMarker(myMark,size,rotation);
312
                                        line.setLineWidth(marker.getSize());
313
                                        marker.setUnit(-1);
314
                                        marker.setStyle(SLDUtils.setMarkerStyle(myMark.getWellKnownName().getLiteral()));
315
                                        line.s.setMarker(marker);
316
                                        line.setAlpha(marker.getColor().getAlpha());
317
                                        if(str.getDashArray() != null)
318
                                                line.setSeparation(str.getDashArray()[1]);
319
                                        return line;
320
                                        */
321
                                }
322
                                //If the marker is an instance of PictureMarkerSymbol
323
                                else if (myLine.getStroke().getGraphic().getExternalGraphics().size() > 0) {
324
                                        
325
                                        SLDExternalGraphic myExtGraphic = myLine.getStroke().getGraphic().getExternalGraphics().get(0);
326
                                        FExpression size = myLine.getStroke().getGraphic().getExpressionSize();
327
                                        FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation();
328
                                        IPictureMarkerSymbol marker = createMarker(myExtGraphic, size, rotation);
329
                                        if (marker != null) {
330

    
331
                                                marker.setUnit(-1);
332
                                                try {
333
                                                        line = symMan().createPictureLineSymbol(
334
                                                                        marker.getSource(), marker.getSelectedSource());
335
                                                } catch (IOException e) {
336
                                                        throw new SLDReadException(e);
337
                                                }
338
                                                line.setLineStyle(simplLine);
339
                                                line.setUnit(-1);
340
                                                line.setLineWidth(marker.getSize());
341
                                                line.setAlpha(marker.getColor().getAlpha());
342
                                                return line;
343
                                        } else {
344
                                                throw new SLDReadException(new Exception(
345
                                                                "Unable to create picture marker symbol"));
346
                                        }
347
                                }
348
                        }
349
                }
350

    
351

    
352
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
353
                //POLYGON//
354
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
355
                else if (symbolizer instanceof SLDPolygonSymbolizer) {
356
                        
357
                        SLDPolygonSymbolizer myPolygon = (SLDPolygonSymbolizer)symbolizer;
358
                        //SimpleFillSymbol
359
                        if ( myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() == null) {
360
                                
361
                                ISimpleFillSymbol simpleFill = symMan().createSimpleFillSymbol();
362

    
363
                                if( myPolygon.getFill().getExpressionColor().getLiteral() != null) {
364
                                        Color color = myPolygon.getFill().getFillColor();
365
                                        if(myPolygon.getFill().getExpressionOpacity().getLiteral() != null) {
366
                                                simpleFill.setHasFill(true);
367
                                                int alpha = (int) ((myPolygon.getFill().getFillOpacity()) * 255);
368
                                                Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
369
                                                simpleFill.setFillColor(myColor);
370
                                        }
371
                                        else
372
                                                simpleFill.setFillColor(color);
373
                                }
374

    
375
                                //Outline of the symbol
376
                                if(myPolygon.getStroke() != null) {
377
                                        simpleFill.setHasOutline(true);
378
                                        simpleFill.setOutline(createOutline(myPolygon.getStroke()));
379
                                }
380
                                return simpleFill;
381
                        }
382
                        else if (myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() != null) {
383
                                
384
                                SLDGraphic myFill = myPolygon.getFill().getFillGraphic();
385

    
386
                                //MarkerFillSymbol
387
                                if(myFill.getMarks().size() > 0 || myFill.getExternalGraphics().size() > 0) {
388
                                        
389
                                        IMarkerFillSymbol markerFill = symMan().createMarkerFillSymbol();
390
                                        // IMarkerFillPropertiesStyle markerProperties = markerFill.getMarkerFillProperties();
391
                                        // markerProperties.setFillStyle(markerFill.GRID_FILL);
392
                                        // markerFill.setMarkerFillProperties(markerProperties);
393

    
394
                                        FExpression size = myFill.getExpressionSize();
395
                                        FExpression rotation = myFill.getExpressionRotation();
396

    
397

    
398
                                        //Outline of the symbol
399
                                        if(myPolygon.getStroke() != null) {
400
                                                markerFill.setHasOutline(true);
401
                                                markerFill.setOutline(createOutline(myPolygon.getStroke()));
402
                                        }
403
                                        //Marker->SimpleMarkerSymbol
404
                                        if(myFill.getMarks().size() > 0) {
405
                                                ISimpleMarkerSymbol marker = null;
406
                                                SLDMark myMark = myFill.getMarks().get(0);
407
                                                marker = createMarker(myMark, size, rotation);
408
                                                if (marker != null) {
409
                                                        markerFill.setMarker(marker);
410
                                                        if(marker.getColor() != null )
411
                                                                markerFill.setFillColor(marker.getColor());
412
                                                }
413
                                                return markerFill;
414
                                        }
415
                                        //Marker->PictureMarkerSymbol
416
                                        else if (myFill.getExternalGraphics().size() > 0) {
417
                                                
418
                                                IPictureMarkerSymbol marker = null;
419
                                                SLDExternalGraphic myExtGraphic = myFill.getExternalGraphics().get(0);
420
                                                marker = createMarker(myExtGraphic, size, rotation);
421
                                                if (marker != null) {
422
                                                        markerFill.setMarker(marker);
423
                                                        if(marker.getColor() != null)
424
                                                                markerFill.setFillColor(marker.getColor());
425
                                                }
426
                                                return markerFill;
427
                                        }
428
                                }
429
                        }
430
                }
431
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
432
                //POINT//
433
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
434
                else if (symbolizer instanceof SLDPointSymbolizer) {
435
                        
436
                        SLDPointSymbolizer myPoint = (SLDPointSymbolizer)symbolizer;
437
                        //Marker->SimpleMarkerSymbol
438
                        if(myPoint.getGraphic().getMarks().size() > 0) {
439
                                SLDMark myMark;
440
                                myMark = myPoint.getGraphic().getMarks().get(0);
441
                                FExpression size = myPoint.getGraphic().getExpressionSize();
442
                                FExpression rotation = myPoint.getGraphic().getExpressionRotation();
443
                                
444
                                ISimpleMarkerSymbol marker = createMarker(myMark,size,rotation);
445
                                marker.setUnit(-1);
446
                                return marker;
447
                        }
448
                        //Marker->PictureMarkerSymbol
449
                        else if (myPoint.getGraphic().getExternalGraphics().size() > 0) {
450
                                SLDExternalGraphic myExtGraphic = myPoint.getGraphic().getExternalGraphics().get(0);
451
                                FExpression size = myPoint.getGraphic().getExpressionSize();
452
                                FExpression rotation = myPoint.getGraphic().getExpressionRotation();
453
                                IPictureMarkerSymbol marker = createMarker(myExtGraphic, size, rotation);
454
                                marker.setUnit(-1);
455
                                return marker;
456
                        }
457
                }
458
                return null;
459

    
460
        }
461
        
462
        
463
        // =======================================
464
        
465
        private static ISimpleMarkerSymbol createMarker(
466
                        SLDMark myMark, FExpression size, FExpression rotation)
467
                                        throws NumberFormatException, SLDReadException {
468

    
469
                ISimpleMarkerSymbol marker = symMan().createSimpleMarkerSymbol();
470
                String name = myMark.getWellKnownName().getLiteral();
471
                marker.setStyle(SLDUtils.getMarkerStyle(name));
472

    
473

    
474
                if(myMark.getFill() != null) {
475
                        if( myMark.getFill().getExpressionColor().getLiteral() != null) {
476
                                Color color = myMark.getFill().getFillColor();
477
                                if(myMark.getFill().getExpressionOpacity().getLiteral() != null) {
478
                                        int alpha = (int) ((myMark.getFill().getFillOpacity()) * 255);
479
                                        Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
480
                                        marker.setColor(myColor);
481
                                }
482
                                else
483
                                        marker.setColor(color);
484
                        }
485
                }
486
                if (myMark.getStroke() != null) {
487
                        marker.setOutlined(false);
488
                        if( myMark.getStroke().getExpressionColor().getLiteral() != null &&
489
                                        myMark.getStroke().getExpressionWidth().getLiteral() != null ) {
490
                                marker.setOutlineColor(myMark.getStroke().getStrokeColor());
491
                                marker.setOutlineSize(myMark.getStroke().getStrokeWidth());
492
                                marker.setOutlined(true);
493
                        }
494
                }
495
                if( size.getLiteral() != null) {
496
                        marker.setSize(Float.valueOf(size.getLiteral()));
497
                }
498
                if( rotation.getLiteral() != null)
499
                        marker.setRotation(Float.valueOf(rotation.getLiteral()));
500

    
501
                return marker;
502
        }        
503
        
504
        
505
        private static IPictureMarkerSymbol createMarker(
506
                        SLDExternalGraphic myExtGraphic,FExpression size, FExpression rotation)
507
                                        throws SLDReadException {
508
                
509
                IPictureMarkerSymbol marker = null;
510

    
511
                //We take the first URL by default
512
                URL url = myExtGraphic.getOnlineResource().get(0);
513
                try {
514
                        if (url != null) {
515
                                marker = symMan().createPictureMarkerSymbol(url,url);
516
                                if( size.getLiteral() != null) {
517
                                        marker.setUnit(-1);
518
                                        marker.setSize(Float.valueOf(size.getLiteral()));
519
                                }
520
                                if( rotation.getLiteral() != null)
521
                                        marker.setRotation(Float.valueOf(rotation.getLiteral()));
522
                        }
523
                } catch (IOException e1) {
524
                        throw new SLDReadException(e1);
525
                }
526

    
527

    
528
                return marker;
529
        }        
530
        
531
        
532
        private static ILineSymbol createOutline(SLDStroke stroke)
533
                        throws NumberFormatException, SLDReadException {
534
                
535
                SLDLineSymbolizer line;
536
                try {
537
                        line = new SLDLineSymbolizer1_0_0();
538
                        line.setStroke(stroke);
539
                        return (ILineSymbol) SLDSymbolizer2ISymbol(line);
540
                } catch (Exception e) {
541
                        throw new SLDReadException(e);
542
                }
543
        }        
544
        
545
        // ====================================================
546
        
547
        public static void writeLegend(
548
                        String layer_name, ILegend legend, OutputStream outstream)
549
                        throws SLDWriteException  {
550

    
551
                DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols ();
552
                dformater_rules.setDecimalSeparator ('.');
553
                DecimalFormat df = new DecimalFormat("##########.0##########",dformater_rules);
554

    
555
                HashMap<String, String> attributes = new HashMap<String, String>();
556
                attributes.put("xsi:schemaLocation","http://www.opengis.net/sld StyledLayerDescriptor.xsd");
557
                attributes.put("xmlns","http://www.opengis.net/sld");
558
                attributes.put("xmlns:ogc","http://www.opengis.net/ogc");
559
                attributes.put("xmlns:xlink","http://www.w3.org/1999/xlink");
560
                attributes.put("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
561
                attributes.put(SLDTags.VERSION_ATTR, VERSION);
562

    
563
                XmlBuilder xmlBuilder = new XmlBuilder();
564
                xmlBuilder.setEncoding(DEFAULT_ENCODING);
565
                xmlBuilder.writeHeader();
566
                xmlBuilder.openTag(SLDTags.SLD_ROOT, attributes);
567
                xmlBuilder.openTag(SLDTags.NAMEDLAYER);
568
                xmlBuilder.writeTag(SLDTags.NAME, layer_name);
569
                xmlBuilder.openTag(SLDTags.USERSTYLE);
570
                xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
571
                try {
572

    
573
                        //VectorialIntervalLegend
574
                        if (legend instanceof IVectorialIntervalLegend) {
575
                                
576
                                IVectorialIntervalLegend myLegend = (IVectorialIntervalLegend) legend;
577
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME, "Feature");
578

    
579
                                ISymbol[] symbols = myLegend.getSymbols();
580
                                if (symbols.length==0) {
581
                                        throw new SLDWriteException(new Exception(
582
                                                        "VectorialIntervalLegend has no symbols."));
583
                                }
584
                                
585
                                if (symbols[0] != null && symbols[0] instanceof IMultiShapeSymbol) {
586
                                        throw new SLDWriteException(new Exception(
587
                                                        "VectorialIntervalLegend has symbol of unsupported type (MultiShapeSymbol)."));
588
                                }
589
                                
590
                                Object[] values = myLegend.getValues();
591
                                NumberFormat nf=NumberFormat.getInstance();
592
                                IInterval interval;
593
                                ISymbol def_sym = null;
594

    
595
                                for(int i = 0; i < symbols.length; i++ )
596
                                {
597
                                        if (values[i] == null || values[i] instanceof String) {
598
                                                /*
599
                                                 * We'll consider this is the default symbol
600
                                                 */
601
                                                def_sym = symbols[i];
602
                                                continue;
603
                                        }
604

    
605
                                        interval = (IInterval) values[i];
606
                                        xmlBuilder.openTag(SLDTags.RULE);
607
                                        xmlBuilder.writeTag(FilterTags.NAME, ""+interval.getMin() +" - " +interval.getMax());
608
                                        xmlBuilder.openTag(FilterTags.FILTER);
609
                                        xmlBuilder.openTag(FilterTags.AND);
610
                                        xmlBuilder.openTag(FilterTags.PROPERTYISGREATEROREQUALTHAN);
611
                                        xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
612
                                        double f = (double) interval.getMin();
613

    
614
                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
615
                                        xmlBuilder.closeTag();
616
                                        xmlBuilder.openTag(FilterTags.PROPERTYISLESSOREQUALTHAN);
617
                                        xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
618
                                        f= (double)interval.getMax();
619
                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
620
                                        xmlBuilder.closeTag();
621
                                        xmlBuilder.closeTag();
622
                                        xmlBuilder.closeTag();
623

    
624
                                        ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(
625
                                                        symbols[i]);
626
                                        xmlBuilder.writeRaw(symSLD.toXML());
627

    
628
                                        xmlBuilder.closeTag();
629
                                }
630
                                /*
631
                                 * Write the default symbol in a "ElseFilter" rule
632
                                 */
633
                                if (def_sym != null) {
634
                                        xmlBuilder.openTag(SLDTags.RULE);
635
                                        xmlBuilder.openTag(SLDTags.ELSEFILTER);
636
                                        xmlBuilder.closeTag(); // close else
637
                                        ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(def_sym);
638
                                        xmlBuilder.writeRaw(symSLD.toXML());                                
639
                                        xmlBuilder.closeTag();
640
                                }
641
                        }
642
                        else if (legend instanceof IVectorialUniqueValueLegend) {
643

    
644
                                IVectorialUniqueValueLegend myLegend = (IVectorialUniqueValueLegend)legend;
645
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
646
                                ISymbol defSym = null;
647
                                ISymbol[] symbols = myLegend.getSymbols();
648
                                if (symbols.length==0){
649
                                        throw new SLDWriteException(new Exception(
650
                                                        "VectorialUniqueValueLegend has no symbols."));
651
                                }
652

    
653
                                if(symbols[0] != null && symbols[0] instanceof IMultiShapeSymbol) {
654
                                        throw new SLDWriteException(new Exception(
655
                                                        "VectorialUniqueValueLegend has symbol of unsupported type (MultiShapeSymbol)."));
656
                                }
657

    
658
                                Object[] values = myLegend.getValues();
659
                                Object myVal;
660

    
661
                                for(int i = 0; i < symbols.length; i++ )
662
                                {
663
                                        if (values[i] == null) {
664
                                                continue;
665
                                        }
666

    
667
                                        myVal = values[i];
668

    
669
                                        if (myVal.toString().compareTo("Default") == 0) {
670
                                                defSym = symbols[i];
671
                                        }
672
                                        else {
673
                                                xmlBuilder.openTag(SLDTags.RULE);
674
                                                if(myVal instanceof String)
675
                                                        xmlBuilder.writeTag(FilterTags.NAME,"\""+myVal.toString()+"\"");
676
                                                else
677
                                                        xmlBuilder.writeTag(FilterTags.NAME, ""+df.format(Double.valueOf(myVal.toString())));
678

    
679
                                                xmlBuilder.openTag(FilterTags.FILTER);
680

    
681
                                                xmlBuilder.openTag(FilterTags.PROPERTYISEQUALTO);
682
                                                xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
683
                                                if(myVal instanceof String)
684
                                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+"\""+myVal.toString()+"\"");
685
                                                else
686
                                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(Double.valueOf(myVal.toString())));
687

    
688
                                                xmlBuilder.closeTag();
689
                                                xmlBuilder.closeTag();
690

    
691
                                                ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(
692
                                                                symbols[i]);
693
                                                xmlBuilder.writeRaw(symSLD.toXML());
694

    
695
                                                xmlBuilder.closeTag();
696
                                        }
697
                                }
698
                        }
699
                        /*
700
                        else if (legend instanceof VectorFilterExpressionLegend) {
701
                                VectorFilterExpressionLegend myLegend = (VectorFilterExpressionLegend)legend;
702
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
703

704
                                ISymbol[] symbols = myLegend.getSymbols();
705
                                if (symbols.length==0){
706
                                        NotificationManager.showMessageInfo(PluginServices.getText(this, "not_save_empty_legend"), null);
707
                                        return;
708
                                }
709
                                Object[] values = myLegend.getValues();
710
                                String[] descriptions = myLegend.getDescriptions();
711
                                for(int i = 0; i < symbols.length; i++ ){
712
                                        if (!(values[i] instanceof Expression)){
713
                                                throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
714
                                        }
715
                                        Expression filter4Symbol = (Expression) values[i];
716
                                        FilterFactory filterFactory = new FilterFactory();
717

718
                                        xmlBuilder.openTag(SLDTags.RULE);
719
                                        xmlBuilder.writeTag(FilterTags.NAME, descriptions[i]);
720
                                        xmlBuilder.openTag(FilterTags.FILTER);
721
                                        xmlBuilder.writeRaw(filterFactory.createXMLFromExpression(filter4Symbol));
722
                                        xmlBuilder.closeTag();
723

724
                                        ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(symbols[i], sldVersion);
725
                                        xmlBuilder.writeRaw(symSLD.toXML());
726
                                        xmlBuilder.closeTag();
727

728
                                }
729
                        }
730
                        */
731
                        //SingleSymbolLegend
732
                        else if (legend instanceof ISingleSymbolLegend ) {
733
                                ISingleSymbolLegend myLegend = (ISingleSymbolLegend)legend;
734
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
735
                                xmlBuilder.openTag(SLDTags.RULE);
736
                                ISLDSymbolizer symSLD =        (ISLDSymbolizer)ISymbol2SLDSymbolizer(
737
                                                myLegend.getDefaultSymbol());
738
                                xmlBuilder.writeRaw(symSLD.toXML());
739
                                xmlBuilder.closeTag();
740
                        }
741
                        else {
742
                                
743
                                throw new SLDWriteException(new Exception(
744
                                                "Unsupported legend type: " + legend.getClass().getName()));
745
                        }
746
                        xmlBuilder.closeTag();
747
                        xmlBuilder.closeTag();
748
                        xmlBuilder.closeTag();
749
                        xmlBuilder.closeTag();
750

    
751
                        OutputStreamWriter writer = new OutputStreamWriter(outstream, DEFAULT_ENCODING);
752
//                        FileWriter writer = new FileWriter(file.getAbsolutePath());
753
                        writer.write( xmlBuilder.getXML());
754
                        writer.close();
755

    
756
                } catch (Exception e) {
757
                        throw new SLDWriteException(e);
758
                }
759
        }
760

    
761
//        private static String classNameForVersion(Class sldClass, String sldVersion) {
762
//                String versionString = sldVersion.replaceAll("\\.", "_");
763
////                String packageName = sldClass.getPackage().getName();
764
////                String packageName = "org.gvsig.remoteClient.sld.";
765
////                String subPackage = packageName.substring(packageName.lastIndexOf(".")+1, packageName.length());
766
////                packageName = packageName.substring(0, packageName.lastIndexOf('.')+1);
767
////                String cName = packageName + "sld" + versionString + "." +
768
////                subPackage + sldClass.getName().substring(
769
////                                sldClass.getName().lastIndexOf(".", sldClass.getName().length())) + versionString;
770
//
771
//                String path = sldClass.toString();
772
//                path = sldClass.toString().substring("class ".length(),path.length());
773
//                path = path.replace(".sld.", ".sld.sld"+versionString+".");
774
//                String cName = path+versionString;
775
//
776
//                return cName;
777
//        }
778
        /**
779
         * Transforms an ISymbol into a SLD Symbolizer
780
         *
781
         * @param symbol ISymbol to be transformed
782
         * @return SLD Symbolizer
783
         * @throws LegendDriverException
784
         * @throws ClassNotFoundException
785
         * @throws IllegalAccessException
786
         * @throws InstantiationException
787
         */
788
        private static ISLDSymbolizer ISymbol2SLDSymbolizer(ISymbol symbol)
789
        throws SLDWriteException {
790
                //MultiLayer Symbol
791
                if(symbol instanceof IMultiLayerSymbol) {
792
                        //Made up of  lines
793
                        if(symbol instanceof IMultiLayerLineSymbol) {
794
                                IMultiLayerLineSymbol multiLine = (IMultiLayerLineSymbol) symbol;
795
                                SLDMultiLineSymbolizer multiSldLine = new SLDMultiLineSymbolizer1_0_0(); 
796
                                for (int i = 0; i < multiLine.getLayerCount(); i++) {
797
                                        multiSldLine.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(
798
                                                        multiLine.getLayer(i)));
799
                                }
800
                                return (ISLDSymbolizer) multiSldLine;
801
                        }
802
                        //Made up of polygons
803
                        else if (symbol instanceof IMultiLayerFillSymbol) {
804
                                IMultiLayerFillSymbol multiFill = (IMultiLayerFillSymbol) symbol;
805
                                SLDMultiPolygonSymbolizer multiSldPolygon =
806
                                                new SLDMultiPolygonSymbolizer1_0_0(); 
807
                                for (int i = 0; i < multiFill.getLayerCount(); i++) {
808
                                        multiSldPolygon.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(
809
                                                        multiFill.getLayer(i)));
810
                                }
811
                                return (ISLDSymbolizer) multiSldPolygon;
812
                        }
813
                        //Made up of markers
814
                        else if (symbol instanceof IMultiLayerMarkerSymbol) {
815
                                IMultiLayerMarkerSymbol multiMarker = (IMultiLayerMarkerSymbol) symbol;
816
                                SLDMultiPointSymbolizer multiSldPoint =
817
                                                new SLDMultiPointSymbolizer1_0_0();
818
                                for (int i = 0; i < multiMarker.getLayerCount(); i++) {
819
                                        multiSldPoint.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(
820
                                                        multiMarker.getLayer(i)));
821
                                }
822
                                return (ISLDSymbolizer) multiSldPoint;
823
                        }
824
                }
825
//                MultishapeSymbol not yet supported
826
//                else if(symbol instanceof MultiShapeSymbol) {
827
//                MultiShapeSymbol multiShape = (MultiShapeSymbol) symbol;
828
//                SLDMultiShapeSymbolizer sldMultiShape = new SLDMultiShapeSymbolizer();
829

    
830
//                sldMultiShape.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiShape.getLineSymbol()));
831
//                sldMultiShape.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiShape.getFillSymbol()));
832
//                sldMultiShape.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiShape.getMarkerSymbol()));
833

    
834
//                return sldMultiShape;
835
//                }
836
                //Single symbol
837
                else {
838
                        //Made up of a line
839
                        if (symbol instanceof ILineSymbol) {
840
                                ILineSymbol myLine = (ILineSymbol)symbol;
841
                                SLDLineSymbolizer mySldLine = new SLDLineSymbolizer1_0_0();
842
                                if (myLine.getLineStyle() != null)
843
                                        mySldLine.setStroke(createStroke(myLine));
844
                                return mySldLine;
845
                        }
846
                        //Made up of a polygon
847
                        else if (symbol instanceof IFillSymbol) {
848
                                IFillSymbol myFill = (IFillSymbol)symbol;
849
                                SLDPolygonSymbolizer mySldPolygon = new SLDPolygonSymbolizer1_0_0();
850
                                SLDFill fill = new SLDFill1_0_0();
851
                                SLDGraphic graphic = new SLDGraphic1_0_0();
852

    
853
                                if (myFill.getOutline() != null) {
854
                                        mySldPolygon.setStroke(createStroke(myFill.getOutline()));
855
                                }
856

    
857
                                if (myFill instanceof IMarkerFillSymbol) {
858
                                        IMarkerFillSymbol markerFill = (IMarkerFillSymbol) symbol;
859
                                        fill.setFillGraphic(createMarkerFill(
860
                                                        markerFill.getMarker(), graphic));
861
                                }
862
                                else if (myFill instanceof IPictureFillSymbol) {
863
                                        IPictureFillSymbol picFill = (IPictureFillSymbol) symbol;
864
                                        graphic.addExternalGraphic(createPictureFill(
865
                                                        picFill.getSource()));
866
                                        fill.setFillGraphic(graphic);
867
                                }
868
                                else if (!(myFill instanceof IMarkerFillSymbol)
869
                                                && !(myFill instanceof IPictureFillSymbol)){
870
                                        
871
                                        if (myFill.getFillColor() != null && myFill.hasFill()){
872

    
873
                                                FExpression expressionColor = new FExpression();
874
                                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(myFill.getFillColor()));
875
                                                fill.setExpressionColor(expressionColor);
876

    
877
                                                FExpression expressionOpacity = new FExpression();
878
                                                float opacity =(float ) myFill.getFillColor().getAlpha() / (float) 255;
879
                                                expressionOpacity.setLiteral(String.valueOf(opacity));
880
                                                fill.setExpressionOpacity(expressionOpacity);
881

    
882
                                        }
883
                                        else if (myFill.getFillColor() != null && !myFill.hasFill()){
884

    
885
                                                FExpression expressionColor = new FExpression();
886
                                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(Color.white));
887
                                                fill.setExpressionColor(expressionColor);
888

    
889
                                                FExpression expressionOpacity = new FExpression();
890
                                                expressionOpacity.setLiteral(String.valueOf(255));
891
                                                fill.setExpressionOpacity(expressionOpacity);
892
                                        }
893
                                }
894

    
895
                                mySldPolygon.setFill(fill);
896

    
897
                                return mySldPolygon;
898
                        }
899
                        //Made up of a marker
900
                        else if (symbol instanceof IMarkerSymbol) {
901
                                IMarkerSymbol myMarker = (IMarkerSymbol)symbol;
902
                                SLDPointSymbolizer mySldPoint = new SLDPointSymbolizer1_0_0();
903
                                mySldPoint.setGraphic(createGraphic(myMarker));
904
                                return mySldPoint;
905
                        }
906
                }
907
                
908
                throw new SLDWriteException(new Exception(
909
                                "Unsupported symbol: " + symbol.getClass().getName()));
910
        }
911

    
912
        private static SLDGraphic createMarkerFill(
913
                        IMarkerSymbol markerFill,
914
                        SLDGraphic graphic) {
915
                
916
                SLDGraphic myGraphic = new SLDGraphic1_0_0();
917
                SLDFill fill = new SLDFill1_0_0();
918
                boolean hasOpacity = false;
919
                float opacity = 0 ;
920

    
921
                myGraphic = graphic;
922

    
923
                if (markerFill.getColor() != null) {
924
                        FExpression expressionColor = new FExpression();
925
                        expressionColor.setLiteral(SLDUtils.convertColorToHexString(markerFill.getColor()));
926
                        fill.setExpressionColor(expressionColor);
927

    
928
                        FExpression expressionOpacity = new FExpression();
929
                        opacity =(float ) markerFill.getColor().getAlpha() / (float) 255;
930
                        expressionOpacity.setLiteral(String.valueOf(opacity));
931

    
932
                        hasOpacity = true;
933
                }
934
                
935
                if(markerFill instanceof IMultiLayerMarkerSymbol) {
936
                        IMultiLayerMarkerSymbol multi = (IMultiLayerMarkerSymbol)markerFill;
937
                        IMarkerSymbol marker = null;
938
                        double max = 0;
939
                        for (int i = 0; i < multi.getLayerCount(); i++) {
940
                                if (max < ((IMarkerSymbol)multi.getLayer(i)).getSize()) {
941
                                        marker =  (IMarkerSymbol) multi.getLayer(i);
942
                                        max = ((IMarkerSymbol)multi.getLayer(i)).getSize();
943
                                }
944
                        }
945
                        myGraphic = createMarkerFill(marker, myGraphic);
946
                }
947
                else if (markerFill instanceof ISimpleMarkerSymbol) {
948
                        ISimpleMarkerSymbol myMarker = (ISimpleMarkerSymbol) markerFill;
949
                        SLDMark mark = new SLDMark1_0_0();
950
                        FExpression wellKnownName = new FExpression();
951
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(myMarker.getStyle()));
952
                        mark.setWellKnownName(wellKnownName);
953
                        mark.setFill(fill);
954
                        myGraphic.addMark(mark);
955
                }
956
                else if (markerFill instanceof IPictureMarkerSymbol) {
957
                        IPictureMarkerSymbol myMarkerSymbol = (IPictureMarkerSymbol) markerFill;
958
                        myGraphic.addExternalGraphic(
959
                                        createPictureFill(myMarkerSymbol.getSource()));
960
                }
961
                /*
962
                else if (markerFill instanceof ICharacterMarkerSymbol) {
963
                        SLDMark mark = (SLDMark) Class.forName(
964
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
965
                        FExpression wellKnownName = new FExpression();
966
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
967
                        mark.setWellKnownName(wellKnownName);
968
                        mark.setFill(fill);
969
                        myGraphic.addMark(mark);
970
                }
971
                */
972

    
973
                FExpression expressionSize = new FExpression();
974
                expressionSize.setLiteral(String.valueOf(markerFill.getSize()));
975
                myGraphic.setExpressionSize(expressionSize);
976

    
977
                if(hasOpacity) {
978

    
979
                        FExpression expressionOpacity = new FExpression();
980
                        expressionOpacity.setLiteral(String.valueOf(opacity));
981
                        myGraphic.setExpressionOpacity(expressionOpacity);
982

    
983
                }
984
                return myGraphic;
985
        }
986

    
987

    
988

    
989
        private static SLDExternalGraphic createPictureFill(URL img_url) {
990
                
991
                SLDExternalGraphic extGra = new SLDExternalGraphic1_0_0();
992
                if (img_url != null) {
993
                        extGra.addOnlineResource(img_url);
994
                        String fpath = img_url.getFile();
995
                        String ext = fpath.substring(fpath.lastIndexOf(".")+1,fpath.length());
996
                        extGra.setFormat("image/" + ext);
997
                }
998
                return extGra;
999
        }
1000

    
1001
        private static SLDStroke createStroke(ILineSymbol myLine) {
1002
                
1003
                SLDStroke stroke = new SLDStroke1_0_0();
1004
                SLDLineSymbolizer mySldLine = new SLDLineSymbolizer1_0_0();
1005
                //If the outline symbol is a multilayer symbol the line is made up of the first
1006
                //layer of the multilayer symbol
1007
                if (myLine instanceof IMultiLayerLineSymbol) {
1008
                        IMultiLayerLineSymbol multiLine = (IMultiLayerLineSymbol) myLine;
1009
                        for (int i = 0; i < multiLine.getLayerCount(); i++) {
1010
                                ILineSymbol myNewLine =  (ILineSymbol) multiLine.getLayer(i);
1011
                                if (myLine.getLineWidth() < myNewLine.getLineWidth())
1012
                                        myLine = myNewLine;
1013
                        }
1014
                        myLine = (ILineSymbol) multiLine.getLayer(0);
1015
                }
1016

    
1017
                BasicStroke str= (BasicStroke) myLine.getLineStyle().getStroke();
1018

    
1019
                if (myLine.getColor() != null) {
1020
                        stroke.setExpressionColor(SLDUtils.convertColorToHexString(myLine.getColor()));
1021
                        float opacity =(float ) myLine.getColor().getAlpha() / (float) 255;
1022
                        stroke.setExpressionOpacity(Double.toString(opacity));
1023
                }
1024
                if(str != null) {
1025
                        stroke.setFloatDashArray(str.getDashArray());
1026
                        stroke.setExpressionLineCap(SLDUtils.convertLineCapToString(str.getEndCap()));
1027
                        stroke.setExpressionLineJoin(SLDUtils.convertLineJoinToString(str.getLineJoin()));
1028
                        stroke.setExpressionWidth(Double.toString(myLine.getLineWidth()));
1029
                        stroke.setExpressionDashOffset(Double.toString(str.getDashPhase()));
1030
                }
1031
                /*
1032
                if(myLine instanceof IMarkerLineSymbol) {
1033
                        MarkerLineSymbol myMarkerLine = (MarkerLineSymbol) myLine;
1034
                        float[] dash =  {Float.valueOf(String.valueOf(myLine.getLineWidth())),
1035
                                        Float.valueOf(String.valueOf(myMarkerLine.getSeparation()))};
1036
                        stroke.setFloatDashArray(dash);
1037
                        stroke.setGraphic(createGraphic(myMarkerLine.getMarker(), sldVersion));
1038
                }
1039
                else
1040
                */
1041
                if (myLine instanceof IPictureLineSymbol) {
1042
                        IPictureLineSymbol picLine = (IPictureLineSymbol)myLine;
1043
                        SLDGraphic graphic = new SLDGraphic1_0_0();
1044
                        graphic.addExternalGraphic(createPictureFill(
1045
                                        picLine.getSource()));
1046
                        stroke.setGraphic(graphic);
1047
                }
1048

    
1049
                return stroke;
1050
        }
1051

    
1052

    
1053

    
1054
        private static SLDGraphic createGraphic(IMarkerSymbol myMarker) {
1055
                
1056
                SLDGraphic graphic = new SLDGraphic1_0_0();
1057
                graphic.getExpressionSize().setLiteral(String.valueOf(myMarker.getSize()));
1058
                graphic.getExpressionRotation().setLiteral(String.valueOf(myMarker.getRotation()));
1059
                graphic.getExpressionOpacity().setLiteral(String.valueOf(myMarker.getColor().getAlpha()/255));
1060

    
1061
                if (myMarker instanceof ISimpleMarkerSymbol) {
1062
                        ISimpleMarkerSymbol marker = (ISimpleMarkerSymbol) myMarker;
1063
                        SLDStroke str = new SLDStroke1_0_0();
1064
                        SLDMark mark = new SLDMark1_0_0();
1065
                        if (marker.getOutlineColor() != null)
1066
                                str.setExpressionColor(SLDUtils.convertColorToHexString(
1067
                                                marker.getOutlineColor()));
1068

    
1069
                        str.setExpressionWidth(String.valueOf(marker.getOutlineSize()));
1070
                        mark.setStroke(str);
1071
                        if (marker.getColor() != null) {
1072

    
1073
                                SLDFill fill = new SLDFill1_0_0();
1074
                                FExpression expressionColor = new FExpression();
1075
                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(
1076
                                                marker.getColor()));
1077
                                fill.setExpressionColor(expressionColor);
1078

    
1079
                                FExpression expressionOpacity = new FExpression();
1080
                                float opacity =(float ) marker.getColor().getAlpha() / (float) 255;
1081
                                expressionOpacity.setLiteral(String.valueOf(opacity));
1082
                                fill.setExpressionOpacity(expressionOpacity);
1083

    
1084
                                mark.setFill(fill);
1085

    
1086
                        }
1087
                        FExpression wellKnownName = new FExpression();
1088
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(marker.getStyle()));
1089
                        mark.setWellKnownName(wellKnownName);
1090
                        graphic.addMark(mark);
1091
                }
1092
                else if (myMarker instanceof IPictureMarkerSymbol) {
1093
                        IPictureMarkerSymbol marker = (IPictureMarkerSymbol) myMarker;
1094
                        SLDExternalGraphic ext = new SLDExternalGraphic1_0_0();
1095
                        if (marker.getSource() != null) {
1096
                                ext.addOnlineResource(marker.getSource());
1097
                        }
1098
                        graphic.addExternalGraphic(ext);
1099
                }
1100
                /*
1101
                else if (myMarker instanceof CharacterMarkerSymbol) {
1102
                        SLDMark mark = (SLDMark) Class.forName(
1103
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
1104
                        FExpression wellKnownName = new FExpression();
1105
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
1106
                        mark.setWellKnownName(wellKnownName);
1107

1108
                        if (myMarker.getColor() != null) {
1109
                                SLDFill fill = (SLDFill) Class.forName(
1110
                                                classNameForVersion(SLDFill.class, sldVersion)).newInstance();
1111

1112
                                FExpression expressionColor = new FExpression();
1113
                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(myMarker.getColor()));
1114
                                fill.setExpressionColor(expressionColor);
1115

1116
                                FExpression expressionOpacity = new FExpression();
1117
                                float opacity =(float ) myMarker.getColor().getAlpha() / (float) 255;
1118
                                expressionOpacity.setLiteral(String.valueOf(opacity));
1119
                                fill.setExpressionOpacity(expressionOpacity);
1120

1121
                                mark.setFill(fill);
1122
                        }
1123
                        graphic.addMark(mark);
1124
                }
1125
                */
1126

    
1127
                return graphic;
1128
        }
1129
        
1130
}