Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / drivers / sld / FMapSLDDriver.java @ 20768

History | View | Annotate | Download (45.4 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.symbology.fmap.drivers.sld;
42

    
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45
import java.io.File;
46
import java.io.FileWriter;
47
import java.io.IOException;
48
import java.net.MalformedURLException;
49
import java.net.URL;
50
//import java.net.UnknownHostException;
51
import java.text.DecimalFormat;
52
import java.text.DecimalFormatSymbols;
53
import java.text.NumberFormat;
54
import java.util.ArrayList;
55
import java.util.HashMap;
56
import java.util.Hashtable;
57
import java.util.Iterator;
58

    
59
import org.gvsig.remoteClient.sld.SLDExternalGraphic;
60
import org.gvsig.remoteClient.sld.SLDGraphic;
61
import org.gvsig.remoteClient.sld.SLDMark;
62
import org.gvsig.remoteClient.sld.SLDProtocolHandler;
63
import org.gvsig.remoteClient.sld.SLDProtocolHandlerFactory;
64
import org.gvsig.remoteClient.sld.SLDStroke;
65
import org.gvsig.remoteClient.sld.SLDTags;
66
import org.gvsig.remoteClient.sld.SLDUtils;
67
import org.gvsig.remoteClient.sld.UnsupportedSLDVersionException;
68
import org.gvsig.remoteClient.sld.filterEncoding.FExpression;
69
import org.gvsig.remoteClient.sld.filterEncoding.Filter;
70
import org.gvsig.remoteClient.sld.filterEncoding.FilterFactory;
71
import org.gvsig.remoteClient.sld.filterEncoding.FilterTags;
72
import org.gvsig.remoteClient.sld.layers.ISLDLayer;
73
import org.gvsig.remoteClient.sld.SLDExternalGraphic;
74
import org.gvsig.remoteClient.sld.SLDFill;
75
import org.gvsig.remoteClient.sld.SLDGraphic;
76
import org.gvsig.remoteClient.sld.SLDMark;
77
import org.gvsig.remoteClient.sld.SLDStroke;
78
import org.gvsig.remoteClient.sld.symbolizers.SLDLineSymbolizer;
79
import org.gvsig.remoteClient.sld.symbolizers.SLDMultiLineSymbolizer;
80
import org.gvsig.remoteClient.sld.symbolizers.SLDMultiPointSymbolizer;
81
import org.gvsig.remoteClient.sld.symbolizers.SLDMultiPolygonSymbolizer;
82
import org.gvsig.remoteClient.sld.symbolizers.SLDPointSymbolizer;
83
import org.gvsig.remoteClient.sld.symbolizers.SLDPolygonSymbolizer;
84
import org.gvsig.remoteClient.sld.symbolizers.ISLDSymbolizer;
85
import org.gvsig.remoteClient.sld.symbolizers.SLDLineSymbolizer;
86
import org.gvsig.remoteClient.sld.symbolizers.SLDPointSymbolizer;
87
import org.gvsig.remoteClient.sld.symbolizers.SLDPolygonSymbolizer;
88
import org.gvsig.symbology.fmap.rendering.VectorialFilterExpressionLegend;
89
import org.gvsig.symbology.fmap.rendering.filter.operations.Expression;
90
import org.gvsig.symbology.fmap.symbols.CharacterMarkerSymbol;
91
import org.gvsig.symbology.fmap.symbols.MarkerFillSymbol;
92
import org.gvsig.symbology.fmap.symbols.MarkerLineSymbol;
93
import org.gvsig.symbology.fmap.symbols.PictureFillSymbol;
94
import org.gvsig.symbology.fmap.symbols.PictureLineSymbol;
95
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
96
import org.xmlpull.v1.XmlPullParserException;
97

    
98
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
99
import com.hardcode.gdbms.engine.values.Value;
100
import com.iver.cit.gvsig.fmap.core.FShape;
101
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
102
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
103
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
104
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
105
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
106
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
107
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
108
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
109
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
110
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
111
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
112
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
114
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
115
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
116
import com.iver.cit.gvsig.fmap.drivers.legend.IFMapLegendDriver;
117
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException;
118
import com.iver.cit.gvsig.fmap.layers.FLayer;
119
import com.iver.cit.gvsig.fmap.layers.FLayers;
120
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
121
import com.iver.cit.gvsig.fmap.rendering.FInterval;
122
import com.iver.cit.gvsig.fmap.rendering.ILegend;
123
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
124
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
125
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
126
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder;
127
/**
128
 * Implements the driver for Styled Layer Descriptor documents in order
129
 * to allow gvSIG to read or write XML documents of this OGC Standard.
130
 * 
131
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
132
 */
133
public class FMapSLDDriver implements IFMapLegendDriver{
134

    
135
        private static final String DESCRIPTION = "Styled Layer Descriptor";
136
        private static final String FILE_EXTENSION = "sld";
137
        private FLayer layer;
138
        private String[] fieldNames;
139
        private int[] fieldTypes;
140

    
141
        public boolean accept(File f) {
142
                if (f.isDirectory()) return true;
143
                String fName = f.getAbsolutePath();
144
                if (fName!=null) {
145
                        fName = fName.toLowerCase();
146
                        return fName.endsWith(".sld");
147
                }
148
                return false;
149
        }
150

    
151
        public String getDescription() {return DESCRIPTION;}
152
        public String getFileExtension() {return FILE_EXTENSION;}
153
        /**
154
         * Obtains the classifying field names that are used in the .sld
155
         * document
156
         * 
157
         * @return Array of Strings with the classifying field names
158
         */
159
        public String[] getClassifyingFieldNames() { return fieldNames; }
160
        /**
161
         * Returns the classifying field types of the classifying field names
162
         * that are used in the .sld document
163
         * 
164
         * @return Array of integers with the types
165
         */
166
        public int[] getClassifyingFieldTypes() { return fieldTypes; }        
167

    
168
        /**
169
         * Returns true if any layer contained in the TOC menu of gvSIG has the
170
         * same name that is passed as an argument. Otherwise,false. 
171
         * 
172
         * @param layer
173
         * @param name 
174
         * 
175
         * @return boolean
176
         */
177
        private boolean checkIsUsableSldLyr(FLayer layer, String name) {
178
                if (layer instanceof FLayers) {
179
                        FLayers layers = (FLayers) layer;
180
                        for (int i = 0; i < layers.getLayersCount(); i++) {
181
                                if (checkIsUsableSldLyr(layers.getLayer(i), name)) {
182
                                        return true;
183
                                }
184
                        }
185
                } 
186
                return layer.getName().equals(name);
187
        }
188

    
189
        public Hashtable<FLayer, ILegend> read(FLayers layers,FLayer layer, File file) throws LegendDriverException {
190
                this.layer = layer;
191
                SLDProtocolHandler my_sld = null;
192

    
193
                try {
194
 
195
                        my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(file);                
196
                        ((SLDProtocolHandler) my_sld).parse(file);
197

    
198
                } catch (LegendDriverException e) {
199
                        throw new LegendDriverException(e.getType());
200
                } catch (IOException e) {
201
                        throw new LegendDriverException(LegendDriverException.READ_DRIVER_EXCEPTION);
202
                } catch (XmlPullParserException e) {
203
                        throw new LegendDriverException(LegendDriverException.PARSE_LEGEND_FILE_ERROR);
204
                } catch (UnsupportedSLDVersionException e) {
205
                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION);
206
                }
207

    
208
                ArrayList<ISLDLayer> sldLayers = my_sld.getLayers();
209
                Hashtable<FLayer, ILegend> table = new Hashtable<FLayer, ILegend>();
210
                ArrayList<ISLDLayer> usableLayers = new ArrayList<ISLDLayer>();
211

    
212

    
213
                for (Iterator<ISLDLayer> iterator = sldLayers.iterator(); iterator.hasNext();) {
214
                        ISLDLayer aLayer = (ISLDLayer) iterator.next();
215
                        if (checkIsUsableSldLyr(layers, aLayer.getName())){
216
                                usableLayers.add(aLayer);
217
                        }
218
                }
219

    
220
                
221
                for (int i = 0; i < usableLayers.size(); i++) {
222
                        if(usableLayers.get(i).getName().compareTo(layer.getName()) == 0) {
223
                                try {
224
                                        ILegend legend = createLegend(usableLayers.get(i));;
225
                                        if (legend != null) {
226
                                                table.put(layer, legend);
227

    
228
                                        }
229
                                }catch (ReadDriverException e) {
230
                                        throw new LegendDriverException(LegendDriverException.READ_DRIVER_EXCEPTION);
231
                                } catch (NumberFormatException e) {
232
                                        throw new LegendDriverException(LegendDriverException.READ_DRIVER_EXCEPTION);
233
                                }
234
                        }
235
                }
236

    
237
                if (table.isEmpty()) {
238
                        throw new LegendDriverException(LegendDriverException.LAYER_NAME_NOT_FOUND);
239
                }
240
                return table;
241

    
242
        }
243
        /**
244
         * Takes a SLDLayer and with its information, creates a gvSIG legend. Depending
245
         * on if the layer has filters or not, it will be created a single symbol legend
246
         * or a filter expression legend 
247
         * 
248
         * @param sldLayer layer contained in a .sld document
249
         * @return a gvSIG legend
250
         * @throws ReadDriverException
251
         * @throws NumberFormatException
252
         * @throws LegendDriverException
253
         * @throws UnknownHostException
254
         */
255
        private ILegend createLegend(ISLDLayer sldLayer) throws ReadDriverException, NumberFormatException, LegendDriverException {
256
                FLyrVect lyrVect = (FLyrVect)this.layer;
257
                ArrayList<Filter> filters = sldLayer.getLayerFilters();
258
                fieldNames = sldLayer.getFieldNames();
259

    
260
                if (!checkFieldNames(fieldNames))
261
                        throw new LegendDriverException(LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND);
262
                
263
                
264
                //Multishape not yet supported
265
                if(lyrVect.getShapeType() == FShape.MULTI )
266
                        throw new LegendDriverException(LegendDriverException.READ_DRIVER_EXCEPTION);
267
                
268
                //SingleSymbolLegend
269
                else if(lyrVect.getShapeType() != FShape.MULTI && !sldLayer.layerHasFilterForSymbolizers(lyrVect.getShapeType())) {
270
                        SingleSymbolLegend createdLegend = new SingleSymbolLegend();
271
                        ArrayList<ISLDSymbolizer> symbolizers;
272
                        symbolizers = sldLayer.getSymbolizersByShapeType(lyrVect.getShapeType());
273
                        IMultiLayerSymbol multiLayerSymbol = SymbologyFactory.createEmptyMultiLayerSymbol(lyrVect.getShapeType());
274

    
275
                        if (multiLayerSymbol != null) {
276
                                for (int i = 0; i < symbolizers.size(); i++) {
277
                                        ISymbol sym = SLDSymbolizer2ISymbol(symbolizers.get(i));
278
                                        multiLayerSymbol.addLayer(sym);
279
                                }
280
                                ISymbol defaultSym = (multiLayerSymbol.getLayerCount() > 1) ?
281
                                                multiLayerSymbol :
282
                                                        multiLayerSymbol.getLayer(0);
283

    
284
                                defaultSym.setDescription("Default");
285
                                createdLegend.setDefaultSymbol(defaultSym);
286
                                return createdLegend;
287
                        }
288
                        return null;
289
                }
290
                //VectorialFilterExpressionLegend
291
                else  {
292
                        VectorialFilterExpressionLegend legend = new VectorialFilterExpressionLegend(lyrVect.getShapeType(),fieldNames);
293
                        for (int i = 0; i < filters.size(); i++) {
294
                                for (int j = 0; j < filters.get(i).getSymbolizers().size(); j++) {
295

    
296
                                        ISLDSymbolizer sldSym = filters.get(i).getSymbolizers().get(j);
297
                                        ISymbol sym = SLDSymbolizer2ISymbol(sldSym);
298
                                        sym.setDescription(filters.get(i).getExpression().toString());
299
                                        legend.addSymbol(filters.get(i).getExpression().toString(), sym);
300
                                }
301
                        }
302
                        return legend;
303
                } 
304
        }
305
        /**
306
         * Checks if the field names contained in an sld document appear in the information
307
         * of the layer that we are using. If all the field names are correct the method
308
         * returns true.Otherwise, false.
309
         * 
310
         * @param fieldNames
311
         * @return
312
         * @throws ReadDriverException
313
         */
314
        private boolean checkFieldNames(String[] fieldNames) throws ReadDriverException {
315

    
316
                for (int i = 0; i < fieldNames.length; i++) {
317
                        if (((FLyrVect) this.layer).getSource().getRecordset().getFieldIndexByName(fieldNames[i]) == -1)
318
                                return false;
319
                }
320
                return true;
321
        }
322
        /**
323
         * Transforms a SLD Symbolizer into an ISymbol.
324
         * 
325
         * @param symbolizer to be transfomed into an ISymbol
326
         * @return ISymbol
327
         * @throws NumberFormatException
328
         * @throws LegendDriverException
329
         * @throws UnknownHostException
330
         */
331
        public static ISymbol SLDSymbolizer2ISymbol(ISLDSymbolizer symbolizer) throws NumberFormatException, LegendDriverException {
332
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
333
                //LINE//
334
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////        
335
                if (symbolizer instanceof SLDLineSymbolizer) {
336
                        SLDLineSymbolizer myLine = (SLDLineSymbolizer) symbolizer;
337

    
338
                        SimpleLineStyle simplLine= new SimpleLineStyle();
339
                        int lineCap;
340
                        if (myLine.getStroke().getExpressionLineCap().getLiteral() != null)
341
                                lineCap = myLine.getStroke().getLineCap();
342
                        else lineCap = BasicStroke.CAP_SQUARE;
343

    
344
                        int lineJoin;
345
                        if (myLine.getStroke().getExpressionLineJoin().getLiteral() != null)
346
                                lineJoin = myLine.getStroke().getLineJoin();
347
                        else lineJoin = BasicStroke.JOIN_MITER;
348

    
349
                        Float dashOffset;
350
                        if (myLine.getStroke().getExpressionDashOffset().getLiteral() != null)
351
                                dashOffset = Float.valueOf(myLine.getStroke().getExpressionDashOffset().getLiteral());
352
                        else dashOffset = 0f;
353

    
354
                        Float width;
355
                        if (myLine.getStroke().getExpressionWidth().getLiteral()!= null)
356
                                width = myLine.getStroke().getStrokeWidth();
357
                        else width = 0f;
358

    
359
                        float[] dash;
360
                        if(myLine.getStroke().getMyDashArray().length == 0) {
361
                                dash = null;
362
                        }
363
                        else dash = myLine.getStroke().getMyDashArray(); 
364

    
365
                        BasicStroke str = new BasicStroke(width,
366
                                        lineCap,
367
                                        lineJoin,
368
                                        10,
369
                                        dash,
370
                                        dashOffset);
371

    
372

    
373
                        simplLine.setStroke(str);
374

    
375
                        //SimpleLineSymbol
376
                        if ( !myLine.getStroke().isHasGraphicFill() && !myLine.getStroke().isHasGraphicStroke() ) {
377
                                SimpleLineSymbol line = new SimpleLineSymbol();
378
                                line.setLineStyle(simplLine);
379
                                line.setUnit(-1);
380
                                line.setLineColor(myLine.getStroke().getStrokeColor());
381
                                if(myLine.getStroke().getExpressionOpacity().getLiteral() != null)
382
                                        line.setAlpha((int)(255 * Float.valueOf(myLine.getStroke().getExpressionOpacity().getLiteral())));
383
                                if (myLine.getStroke().getExpressionWidth().getLiteral() != null)
384
                                        line.setLineWidth(myLine.getStroke().getStrokeWidth());
385
                                return line;
386
                        }
387
                        else {
388
                                //MarkerLineSymbol
389
                                MarkerLineSymbol line = new MarkerLineSymbol();
390
                                line.setLineStyle(simplLine);
391
                                line.setUnit(-1);
392
                                //If the marker is an instance of SimpleMarkerSymbol
393
                                if(myLine.getStroke().getGraphic().getMarks().size() > 0) {
394
                                        SLDMark myMark = myLine.getStroke().getGraphic().getMarks().get(0);
395
                                        SimpleMarkerSymbol marker = new SimpleMarkerSymbol();
396
                                        FExpression size = myLine.getStroke().getGraphic().getExpressionSize();
397
                                        FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation();
398
                                        marker = createMarker(myMark,size,rotation);
399
                                        line.setLineWidth(marker.getSize());
400
                                        marker.setUnit(-1);
401
                                        marker.setStyle(SLDUtils.setMarkerStyle(myMark.getWellKnownName().getLiteral()));                                
402
                                        line.setMarker(marker);
403
                                        line.setAlpha(marker.getColor().getAlpha());
404
                                        if(str.getDashArray() != null)
405
                                                line.setSeparation(str.getDashArray()[1]);
406
                                        return line;
407
                                }
408
                                //If the marker is an instance of PictureMarkerSymbol
409
                                else if (myLine.getStroke().getGraphic().getExternalGraphics().size() > 0) {
410
                                        SLDExternalGraphic myExtGraphic = myLine.getStroke().getGraphic().getExternalGraphics().get(0);
411
                                        PictureMarkerSymbol marker = new PictureMarkerSymbol();
412
                                        FExpression size = myLine.getStroke().getGraphic().getExpressionSize();
413
                                        FExpression rotation = myLine.getStroke().getGraphic().getExpressionRotation();
414
                                        marker = createMarker(myExtGraphic, size, rotation);
415
                                        if (marker != null) {
416
                                                line.setLineWidth(marker.getSize());
417
                                                marker.setUnit(-1);
418
                                                line.setMarker(marker);
419
                                                line.setAlpha(marker.getColor().getAlpha());
420
                                        }
421
                                        return line;
422
                                }
423
                        }
424
                }
425

    
426

    
427
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
428
                //POLYGON//
429
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////                                
430
                else if (symbolizer instanceof SLDPolygonSymbolizer) {
431
                        SLDPolygonSymbolizer myPolygon = (SLDPolygonSymbolizer)symbolizer;
432
                        //SimpleFillSymbol
433
                        if ( myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() == null) {
434
                                SimpleFillSymbol simpleFill = new SimpleFillSymbol();
435

    
436
                                if( myPolygon.getFill().getExpressionColor().getLiteral() != null) {
437
                                        Color color = myPolygon.getFill().getFillColor();
438
                                        if(myPolygon.getFill().getExpressionOpacity().getLiteral() != null) {
439
                                                simpleFill.setHasFill(true);
440
                                                int alpha = (int) ((myPolygon.getFill().getFillOpacity()) * 255);
441
                                                Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
442
                                                simpleFill.setFillColor(myColor);
443
                                        }
444
                                        else 
445
                                                simpleFill.setFillColor(color);
446
                                }
447

    
448
                                //Outline of the symbol
449
                                if(myPolygon.getStroke() != null) {
450
                                        simpleFill.setHasOutline(true);
451
                                        simpleFill.setOutline(createOutline(myPolygon.getStroke()));
452
                                }
453
                                return simpleFill;
454
                        }        
455
                        else if (myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() != null) {
456
                                SLDGraphic myFill = myPolygon.getFill().getFillGraphic();
457

    
458

    
459

    
460
                                //MarkerFillSymbol
461
                                if(myFill.getMarks().size() > 0 || myFill.getExternalGraphics().size() > 0) {
462
                                        MarkerFillSymbol markerFill = new MarkerFillSymbol();
463
                                        IMarkerFillPropertiesStyle markerProperties = markerFill.getMarkerFillProperties();
464
                                        markerProperties.setFillStyle(markerFill.GRID_FILL);
465
                                        markerFill.setMarkerFillProperties(markerProperties);
466

    
467
                                        FExpression size = myFill.getExpressionSize();
468
                                        FExpression rotation = myFill.getExpressionRotation();
469

    
470

    
471
                                        //Outline of the symbol
472
                                        if(myPolygon.getStroke() != null) {
473
                                                markerFill.setHasOutline(true);
474
                                                markerFill.setOutline(createOutline(myPolygon.getStroke()));
475
                                        }
476
                                        //Marker->SimpleMarkerSymbol
477
                                        if(myFill.getMarks().size() > 0) {
478
                                                SimpleMarkerSymbol marker = new SimpleMarkerSymbol();
479
                                                SLDMark myMark = myFill.getMarks().get(0);
480
                                                marker = createMarker(myMark, size, rotation);
481
                                                if (marker != null) {
482
                                                        markerFill.setMarker(marker);
483
                                                        if(marker.getColor() != null )
484
                                                                markerFill.setFillColor(marker.getColor());
485
                                                }
486
                                                return markerFill;
487
                                        }
488
                                        //Marker->PictureMarkerSymbol
489
                                        else if (myFill.getExternalGraphics().size() > 0) {
490
                                                PictureMarkerSymbol marker = new PictureMarkerSymbol();
491
                                                SLDExternalGraphic myExtGraphic = myFill.getExternalGraphics().get(0);
492
                                                marker = createMarker(myExtGraphic, size, rotation);
493
                                                if (marker != null) {
494
                                                        markerFill.setMarker(marker);
495
                                                        if(marker.getColor() != null)
496
                                                                markerFill.setFillColor(marker.getColor());
497
                                                }
498
                                                return markerFill;
499
                                        }
500
                                }
501
                        }
502
                }
503
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
504
                //POINT//
505
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////        
506
                else if (symbolizer instanceof SLDPointSymbolizer) {
507
                        SLDPointSymbolizer myPoint = (SLDPointSymbolizer)symbolizer;
508
                        //Marker->SimpleMarkerSymbol
509
                        if(myPoint.getGraphic().getMarks().size() > 0) {
510
                                SLDMark myMark;
511
                                try {
512
                                        myMark = (SLDMark) Class.forName(
513
                                                        classNameForVersion(SLDMark.class, SLDProtocolHandlerFactory.getLatestVersion())).newInstance();
514
                                } catch (Exception e) {
515
                                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION);
516
                                }
517
                                myMark = myPoint.getGraphic().getMarks().get(0);
518
                                FExpression size = myPoint.getGraphic().getExpressionSize();
519
                                FExpression rotation = myPoint.getGraphic().getExpressionRotation();
520
                                SimpleMarkerSymbol marker = createMarker(myMark,size,rotation);
521
                                marker.setUnit(-1);
522
                                return marker;
523
                        }
524
                        //Marker->PictureMarkerSymbol
525
                        else if (myPoint.getGraphic().getExternalGraphics().size() > 0) {
526
                                SLDExternalGraphic myExtGraphic = myPoint.getGraphic().getExternalGraphics().get(0);
527
                                FExpression size = myPoint.getGraphic().getExpressionSize();
528
                                FExpression rotation = myPoint.getGraphic().getExpressionRotation();
529
                                PictureMarkerSymbol marker = createMarker(myExtGraphic, size, rotation);
530
                                marker.setUnit(-1);
531
                                return marker;
532
                        }
533
                }
534
                return null;
535
        }
536

    
537
        public static SimpleMarkerSymbol createMarker(SLDMark myMark, FExpression size, FExpression rotation) throws NumberFormatException, LegendDriverException {
538

    
539
                SimpleMarkerSymbol marker = new SimpleMarkerSymbol();
540
                String name = myMark.getWellKnownName().getLiteral();
541
                marker.setStyle(SLDUtils.setMarkerStyle(name));
542

    
543

    
544
                if(myMark.getFill() != null) {
545
                        if( myMark.getFill().getExpressionColor().getLiteral() != null) {
546
                                Color color = myMark.getFill().getFillColor();
547
                                if(myMark.getFill().getExpressionOpacity().getLiteral() != null) {
548
                                        int alpha = (int) ((myMark.getFill().getFillOpacity()) * 255);
549
                                        Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
550
                                        marker.setColor(myColor);
551
                                }
552
                                else 
553
                                        marker.setColor(color);
554
                        }
555
                }
556
                if (myMark.getStroke() != null) {
557
                        marker.setOutlined(false);
558
                        if( myMark.getStroke().getExpressionColor().getLiteral() != null &&
559
                                        myMark.getStroke().getExpressionWidth().getLiteral() != null ) {
560
                                marker.setOutlineColor(myMark.getStroke().getStrokeColor());
561
                                marker.setOutlineSize(myMark.getStroke().getStrokeWidth());
562
                                marker.setOutlined(true);
563
                        }        
564
                }
565
                if( size.getLiteral() != null) {
566
                        marker.setSize(Float.valueOf(size.getLiteral()));        
567
                }
568
                if( rotation.getLiteral() != null) 
569
                        marker.setRotation(Float.valueOf(rotation.getLiteral()));
570

    
571
                return marker;
572
        }
573

    
574

    
575

    
576
        public static PictureMarkerSymbol createMarker(SLDExternalGraphic myExtGraphic,FExpression size, FExpression rotation) throws LegendDriverException {
577
                PictureMarkerSymbol marker = new PictureMarkerSymbol() ;
578

    
579
                //We take the first URL by default
580
                URL url = myExtGraphic.getOnlineResource().get(0);
581
                try {
582
                        if (url != null) {
583
                                marker = new PictureMarkerSymbol(url,url);
584
                                if( size.getLiteral() != null) {
585
                                        marker.setUnit(-1);
586
                                        marker.setSize(Float.valueOf(size.getLiteral()));
587
                                }
588
                                if( rotation.getLiteral() != null) 
589
                                        marker.setRotation(Float.valueOf(rotation.getLiteral()));
590
                        }
591
                } catch (IOException e1) {
592
                        throw new LegendDriverException(LegendDriverException.READ_DRIVER_EXCEPTION);
593
                }
594

    
595

    
596
                return marker;
597
        }
598

    
599
        public static ILineSymbol createOutline(SLDStroke stroke) throws NumberFormatException, LegendDriverException {
600
                SLDLineSymbolizer line;
601
                try {
602
                        line = (SLDLineSymbolizer) Class.forName(
603
                                        classNameForVersion(SLDLineSymbolizer.class, SLDProtocolHandlerFactory.getLatestVersion())).newInstance();
604

    
605
                        line.setStroke(stroke);
606
                        return (ILineSymbol) SLDSymbolizer2ISymbol(line);
607
                } catch (Exception e) {
608
                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION);
609
                }
610
        }
611

    
612

    
613

    
614
        public void write(FLayers layers,FLayer layer, ILegend legend, File file)throws LegendDriverException  {
615

    
616
                DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols ();
617
                dformater_rules.setDecimalSeparator ('.');
618
                DecimalFormat df = new DecimalFormat("##########.###########",dformater_rules);
619

    
620

    
621
                String sldVersion = SLDProtocolHandlerFactory.getLatestVersion();
622

    
623
                HashMap<String, String> attributes = new HashMap<String, String>();
624
                attributes.put("xsi:schemaLocation","http://www.opengis.net/sld StyledLayerDescriptor.xsd");
625
                attributes.put("xmlns","http://www.opengis.net/sld");
626
                attributes.put("xmlns:ogc","http://www.opengis.net/ogc");
627
                attributes.put("xmlns:xlink","http://www.w3.org/1999/xlink");
628
                attributes.put("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
629
                attributes.put(SLDTags.VERSION_ATTR, sldVersion);
630

    
631

    
632
                XmlBuilder xmlBuilder = new XmlBuilder();
633
                xmlBuilder.writeHeader();
634
                xmlBuilder.openTag(SLDTags.SLD_ROOT, attributes);
635
                xmlBuilder.openTag(SLDTags.NAMEDLAYER);
636
                xmlBuilder.writeTag(SLDTags.NAME,layer.getName());
637
                xmlBuilder.openTag(SLDTags.USERSTYLE);
638
                xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
639
                try {
640
                        
641
                        //VectorialIntervalLegend
642
                        if (legend instanceof VectorialIntervalLegend ) {
643
                                VectorialIntervalLegend myLegend = (VectorialIntervalLegend)legend;
644
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
645

    
646
                                ISymbol[] symbols = myLegend.getSymbols();
647
                                Object[] values = myLegend.getValues();
648

    
649
                                NumberFormat nf=NumberFormat.getInstance();
650

    
651
                                FInterval interval;
652
                                if(symbols[0] != null && symbols[0] instanceof MultiShapeSymbol)
653
                                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
654

    
655
                                for(int i = 0; i < symbols.length; i++ )
656
                                {
657
                                        interval = (FInterval)values[i];
658
                                        xmlBuilder.openTag(SLDTags.RULE);
659
                                        xmlBuilder.writeTag(FilterTags.NAME, ""+interval.getMin() +" - " +interval.getMax());
660
                                        xmlBuilder.openTag(FilterTags.FILTER);
661
                                        xmlBuilder.openTag(FilterTags.AND);
662
                                        xmlBuilder.openTag(FilterTags.PROPERTYISGREATEROREQUALTHAN);
663
                                        xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
664
                                        double f = (double) interval.getMin();
665

    
666
                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
667
                                        xmlBuilder.closeTag();
668
                                        xmlBuilder.openTag(FilterTags.PROPERTYISLESSOREQUALTHAN);
669
                                        xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
670
                                        f= (double)interval.getMax();
671
                                        xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(f));
672
                                        xmlBuilder.closeTag();
673
                                        xmlBuilder.closeTag();
674
                                        xmlBuilder.closeTag();
675

    
676
                                        ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(symbols[i], sldVersion);
677
                                        xmlBuilder.writeRaw(symSLD.toXML());
678

    
679
                                        xmlBuilder.closeTag();
680
                                }
681
                        }
682
                        else if (legend instanceof VectorialUniqueValueLegend) {
683

    
684
                                VectorialUniqueValueLegend myLegend = (VectorialUniqueValueLegend)legend;
685
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
686
                                ISymbol defSym = null;
687
                                ISymbol[] symbols = myLegend.getSymbols();
688
                                Object[] values = myLegend.getValues();
689

    
690
                                Value myVal;
691

    
692
                                if(symbols[0] != null && symbols[0] instanceof MultiShapeSymbol)
693
                                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
694

    
695
                                for(int i = 0; i < symbols.length; i++ )
696
                                {
697
                                        myVal = (Value) values[i];
698

    
699
                                        if (myVal.toString().compareTo("Default") == 0) {
700
                                                defSym = symbols[i];
701
                                        }
702
                                        else {
703
                                                xmlBuilder.openTag(SLDTags.RULE);
704
                                                xmlBuilder.writeTag(FilterTags.NAME, ""+df.format(Double.valueOf(myVal.toString())));
705
                                                xmlBuilder.openTag(FilterTags.FILTER);
706

    
707
                                                xmlBuilder.openTag(FilterTags.PROPERTYISEQUALTO);
708
                                                xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
709
                                                xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(Double.valueOf(myVal.toString())));
710
                                                xmlBuilder.closeTag();
711
                                                xmlBuilder.closeTag();
712

    
713
                                                ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(symbols[i], sldVersion);
714
                                                xmlBuilder.writeRaw(symSLD.toXML());
715

    
716
                                                xmlBuilder.closeTag();
717
                                        }
718
                                }
719
                        }
720
                        else if (legend instanceof VectorialFilterExpressionLegend) {
721
                                VectorialFilterExpressionLegend myLegend = (VectorialFilterExpressionLegend)legend;
722
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
723

    
724
                                ISymbol[] symbols = myLegend.getSymbols();
725
                                Object[] values = myLegend.getValues();
726
                                String[] descriptions = myLegend.getDescriptions();
727
                                for(int i = 0; i < symbols.length; i++ ){
728
                                        Expression filter4Symbol = (Expression) values[i]; 
729
                                        FilterFactory filterFactory = new FilterFactory();
730

    
731
                                        xmlBuilder.openTag(SLDTags.RULE);
732
                                        xmlBuilder.writeTag(FilterTags.NAME, descriptions[i]);
733
                                        xmlBuilder.openTag(FilterTags.FILTER);
734
                                        xmlBuilder.writeRaw(filterFactory.createXMLFromExpression(filter4Symbol));
735
                                        xmlBuilder.closeTag();
736

    
737
                                        ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(symbols[i], sldVersion);
738
                                        xmlBuilder.writeRaw(symSLD.toXML());
739
                                        xmlBuilder.closeTag();
740

    
741
                                }
742
                        }
743
                        //SingleSymbolLegend
744
                        else if (legend instanceof SingleSymbolLegend ) {
745
                                SingleSymbolLegend myLegend = (SingleSymbolLegend)legend;
746
                                xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"Feature");
747
                                xmlBuilder.openTag(SLDTags.RULE);
748
                                ISLDSymbolizer symSLD = (ISLDSymbolizer)ISymbol2SLDSymbolizer(myLegend.getDefaultSymbol(), sldVersion);
749
                                xmlBuilder.writeRaw(symSLD.toXML());
750
                                xmlBuilder.closeTag();
751
                        }
752
                        else {
753
                                throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
754
                        }
755
                        xmlBuilder.closeTag();
756
                        xmlBuilder.closeTag();
757
                        xmlBuilder.closeTag();
758
                        xmlBuilder.closeTag();
759
                        
760
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
761
                        writer.write( xmlBuilder.getXML());
762
                        writer.close();
763

    
764
                } catch (LegendDriverException e) {
765
                        throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
766
                } catch (IOException e) {
767
                        throw new LegendDriverException(LegendDriverException.SAVE_LEGEND_ERROR);
768
                } catch (InstantiationException e) {
769
                        throw new LegendDriverException(LegendDriverException.SAVE_LEGEND_ERROR);
770
                } catch (IllegalAccessException e) {
771
                        throw new LegendDriverException(LegendDriverException.SAVE_LEGEND_ERROR);
772
                } catch (ClassNotFoundException e) {
773
                        throw new LegendDriverException(LegendDriverException.SAVE_LEGEND_ERROR);
774
                }
775
        }
776
        
777
        private static String classNameForVersion(Class sldClass, String sldVersion) {
778
                String versionString = sldVersion.replaceAll("\\.", "_");
779
                String packageName = sldClass.getPackage().getName();
780
                String subPackage = packageName.substring(packageName.lastIndexOf(".")+1, packageName.length());
781
                packageName = packageName.substring(0, packageName.lastIndexOf('.')+1);
782
                String cName = packageName + "sld" + versionString + "." +
783
                        subPackage + sldClass.getName().substring(
784
                                sldClass.getName().lastIndexOf(".", sldClass.getName().length())) + versionString;
785

    
786
                return cName;
787
        }
788
        /**
789
         * Transforms an ISymbol into a SLD Symbolizer
790
         * 
791
         * @param symbol ISymbol to be transformed
792
         * @return SLD Symbolizer
793
         * @throws LegendDriverException 
794
         * @throws ClassNotFoundException 
795
         * @throws IllegalAccessException 
796
         * @throws InstantiationException 
797
         */
798
        private ISLDSymbolizer ISymbol2SLDSymbolizer(ISymbol symbol, String sldVersion) 
799
        throws LegendDriverException, InstantiationException, IllegalAccessException, ClassNotFoundException {
800
                //MultiLayer Symbol
801
                if(symbol instanceof IMultiLayerSymbol) {
802
                        //Made up of  lines
803
                        if(symbol instanceof MultiLayerLineSymbol) {
804
                                MultiLayerLineSymbol multiLine = (MultiLayerLineSymbol) symbol;
805
                                SLDMultiLineSymbolizer multiSldLine = 
806
                                        (SLDMultiLineSymbolizer) Class.forName(
807
                                                        classNameForVersion(SLDMultiLineSymbolizer.class, sldVersion)).newInstance();
808
                                for (int i = 0; i < multiLine.getLayerCount(); i++) {
809
                                        multiSldLine.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiLine.getLayer(i), sldVersion));        
810
                                }
811
                                return (ISLDSymbolizer) multiSldLine;
812
                        }
813
                        //Made up of polygons
814
                        else if (symbol instanceof MultiLayerFillSymbol) {
815
                                MultiLayerFillSymbol multiFill = (MultiLayerFillSymbol) symbol;
816
                                SLDMultiPolygonSymbolizer multiSldPolygon = 
817
                                        (SLDMultiPolygonSymbolizer) Class.forName(
818
                                                        classNameForVersion(SLDMultiPolygonSymbolizer.class, sldVersion)).newInstance();
819
                                for (int i = 0; i < multiFill.getLayerCount(); i++) {
820
                                        multiSldPolygon.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiFill.getLayer(i), sldVersion));        
821
                                }
822
                                return (ISLDSymbolizer) multiSldPolygon;
823
                        }
824
                        //Made up of markers
825
                        else if (symbol instanceof MultiLayerMarkerSymbol) {
826
                                MultiLayerMarkerSymbol multiMarker = (MultiLayerMarkerSymbol) symbol;
827
                                SLDMultiPointSymbolizer multiSldPoint = (SLDMultiPointSymbolizer) Class.forName(
828
                                                classNameForVersion(SLDMultiPointSymbolizer.class, sldVersion)).newInstance();
829
                                for (int i = 0; i < multiMarker.getLayerCount(); i++) {
830
                                        multiSldPoint.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiMarker.getLayer(i), sldVersion));        
831
                                }
832
                                return (ISLDSymbolizer) multiSldPoint;
833
                        }
834
                }
835
//      MultishapeSymbol not yet supported                
836
//                else if(symbol instanceof MultiShapeSymbol) {
837
//                        MultiShapeSymbol multiShape = (MultiShapeSymbol) symbol;
838
//                        SLDMultiShapeSymbolizer sldMultiShape = new SLDMultiShapeSymbolizer();
839
//
840
//                        sldMultiShape.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiShape.getLineSymbol()));
841
//                        sldMultiShape.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiShape.getFillSymbol()));
842
//                        sldMultiShape.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiShape.getMarkerSymbol()));
843
//
844
//                        return sldMultiShape;        
845
//                }
846
                //Single symbol
847
                else {
848
                        //Made up of a line
849
                        if (symbol instanceof ILineSymbol) {
850
                                ILineSymbol myLine = (ILineSymbol)symbol;
851
                                SLDLineSymbolizer mySldLine = (SLDLineSymbolizer) Class.forName(
852
                                                classNameForVersion(SLDLineSymbolizer.class, sldVersion)).newInstance();
853
                                if (myLine.getLineStyle() != null)
854
                                        mySldLine.setStroke(createStroke(myLine, sldVersion));
855
                                return mySldLine;
856
                        }
857
                        //Made up of a polygon
858
                        else if (symbol instanceof IFillSymbol) {
859
                                IFillSymbol myFill = (IFillSymbol)symbol;
860
                                SLDPolygonSymbolizer mySldPolygon =(SLDPolygonSymbolizer) Class.forName(
861
                                                classNameForVersion(SLDPolygonSymbolizer.class, sldVersion)).newInstance();
862
                                SLDFill fill = (SLDFill) Class.forName(
863
                                                classNameForVersion(SLDFill.class, sldVersion)).newInstance();
864
                                SLDGraphic graphic = (SLDGraphic) Class.forName(
865
                                                classNameForVersion(SLDGraphic.class, sldVersion)).newInstance();
866

    
867
                                if (myFill.getOutline() != null)
868
                                        mySldPolygon.setStroke(createStroke(myFill.getOutline(), sldVersion));
869

    
870
                                if (myFill instanceof MarkerFillSymbol) {
871
                                        MarkerFillSymbol markerFill = (MarkerFillSymbol) symbol;
872
                                        fill.setFillGraphic(createMarkerFill(markerFill.getMarker(), graphic, sldVersion));
873
                                }
874
                                else if (myFill instanceof PictureFillSymbol) {
875
                                        PictureFillSymbol picFill = (PictureFillSymbol) symbol;
876
                                        graphic.addExternalGraphic(createPictureFill(picFill.getImagePath(), sldVersion));
877
                                        fill.setFillGraphic(graphic);
878
                                }
879
                                else if (!(myFill instanceof MarkerFillSymbol) && !(myFill instanceof PictureFillSymbol)){
880
                                        if (myFill.getFillColor() != null && myFill.hasFill()){
881

    
882
                                                FExpression expressionColor = new FExpression();
883
                                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(myFill.getFillColor()));
884
                                                fill.setExpressionColor(expressionColor);
885

    
886
                                                FExpression expressionOpacity = new FExpression();
887
                                                float opacity =(float ) myFill.getFillColor().getAlpha() / (float) 255;
888
                                                expressionOpacity.setLiteral(String.valueOf(opacity));
889
                                                fill.setExpressionOpacity(expressionOpacity);
890

    
891
                                        }
892
                                        else if (myFill.getFillColor() != null && !myFill.hasFill()){
893

    
894
                                                FExpression expressionColor = new FExpression();
895
                                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(Color.white));
896
                                                fill.setExpressionColor(expressionColor);
897

    
898
                                                FExpression expressionOpacity = new FExpression();
899
                                                expressionOpacity.setLiteral(String.valueOf(255));
900
                                                fill.setExpressionOpacity(expressionOpacity);
901
                                        }
902
                                }
903

    
904
                                mySldPolygon.setFill(fill);
905

    
906
                                return mySldPolygon;
907
                        }
908
                        //Made up of a marker
909
                        else if (symbol instanceof IMarkerSymbol) {
910
                                IMarkerSymbol myMarker = (IMarkerSymbol)symbol;
911
                                SLDPointSymbolizer mySldPoint = (SLDPointSymbolizer) Class.forName(
912
                                                classNameForVersion(SLDPointSymbolizer.class, sldVersion)).newInstance();
913
                                mySldPoint.setGraphic(createGraphic(myMarker, sldVersion));
914
                                return mySldPoint;
915
                        }
916
                }
917
                throw new LegendDriverException(LegendDriverException.UNSUPPORTED_LEGEND_CREATION);
918
        }
919

    
920
        private SLDGraphic createMarkerFill(IMarkerSymbol markerFill, SLDGraphic graphic, String sldVersion)
921
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
922
                SLDGraphic myGraphic = (SLDGraphic) Class.forName(
923
                                classNameForVersion(SLDGraphic.class, sldVersion)).newInstance();
924
                SLDFill fill = (SLDFill) Class.forName(
925
                                classNameForVersion(SLDFill.class, sldVersion)).newInstance();
926
                boolean hasOpacity = false;
927
                float opacity = 0 ;
928

    
929
                myGraphic = graphic;
930

    
931
                if (markerFill.getColor() != null) {
932

    
933
                        FExpression expressionColor = new FExpression();
934
                        expressionColor.setLiteral(SLDUtils.convertColorToHexString(markerFill.getColor()));
935
                        fill.setExpressionColor(expressionColor);
936

    
937
                        FExpression expressionOpacity = new FExpression();
938
                        opacity =(float ) markerFill.getColor().getAlpha() / (float) 255;
939
                        expressionOpacity.setLiteral(String.valueOf(opacity));
940

    
941
                        hasOpacity = true;
942
                }
943
                if(markerFill instanceof MultiLayerMarkerSymbol) {
944
                        MultiLayerMarkerSymbol multi = (MultiLayerMarkerSymbol)markerFill;
945
                        IMarkerSymbol marker = null;
946
                        double max = 0;
947
                        for (int i = 0; i < multi.getLayerCount(); i++) {
948
                                if (max < ((IMarkerSymbol)multi.getLayer(i)).getSize()) {
949
                                        marker =  (IMarkerSymbol) multi.getLayer(i);
950
                                        max = ((IMarkerSymbol)multi.getLayer(i)).getSize(); 
951
                                }
952
                        }
953
                        myGraphic = createMarkerFill(marker, myGraphic, sldVersion);
954
                }
955
                else if (markerFill instanceof SimpleMarkerSymbol) {
956
                        SimpleMarkerSymbol myMarker = (SimpleMarkerSymbol) markerFill;
957
                        SLDMark mark = (SLDMark) Class.forName(
958
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
959
                        FExpression wellKnownName = new FExpression();
960
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(myMarker.getStyle()));
961
                        mark.setWellKnownName(wellKnownName);
962
                        mark.setFill(fill);
963
                        myGraphic.addMark(mark);
964
                }
965
                else if (markerFill instanceof PictureMarkerSymbol) {
966
                        PictureMarkerSymbol myMarkerSymbol = (PictureMarkerSymbol) markerFill;
967
                        myGraphic.addExternalGraphic(createPictureFill(myMarkerSymbol.getImagePath(), sldVersion));
968
                }
969
                else if (markerFill instanceof CharacterMarkerSymbol) {
970
                        SLDMark mark = (SLDMark) Class.forName(
971
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
972
                        FExpression wellKnownName = new FExpression();
973
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
974
                        mark.setWellKnownName(wellKnownName);
975
                        mark.setFill(fill);
976
                        myGraphic.addMark(mark);        
977
                }
978

    
979
                FExpression expressionSize = new FExpression();
980
                expressionSize.setLiteral(String.valueOf(markerFill.getSize()));
981
                myGraphic.setExpressionSize(expressionSize);
982

    
983
                if(hasOpacity) {
984

    
985
                        FExpression expressionOpacity = new FExpression();
986
                        expressionOpacity.setLiteral(String.valueOf(opacity));
987
                        myGraphic.setExpressionOpacity(expressionOpacity);
988

    
989
                }
990
                return myGraphic;
991
        }
992

    
993

    
994

    
995
        private SLDExternalGraphic createPictureFill(String imagePath, String sldVersion) 
996
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
997
                SLDExternalGraphic extGra = (SLDExternalGraphic) Class.forName(
998
                                classNameForVersion(SLDExternalGraphic.class, sldVersion)).newInstance();
999
                try {
1000
                        if (imagePath != null)
1001
                                extGra.addOnlineResource(new URL(imagePath));
1002
                } catch (MalformedURLException e) {
1003
                        e.printStackTrace();
1004
                }
1005
                return extGra;
1006
        }
1007

    
1008
        private SLDStroke createStroke(ILineSymbol myLine, String sldVersion)
1009
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
1010
                SLDStroke stroke = (SLDStroke) Class.forName(
1011
                                classNameForVersion(SLDStroke.class, sldVersion)).newInstance();
1012

    
1013
                //If the outline symbol is a multilayer symbol the line is made up of the first
1014
                //layer of the multilayer symbol
1015
                if (myLine instanceof MultiLayerLineSymbol) {
1016
                        MultiLayerLineSymbol multiLine = (MultiLayerLineSymbol) myLine;
1017
                        for (int i = 0; i < multiLine.getLayerCount(); i++) {
1018
                                ILineSymbol myNewLine =  (ILineSymbol) multiLine.getLayer(i);
1019
                                if (myLine.getLineWidth() < myNewLine.getLineWidth())
1020
                                        myLine = myNewLine;        
1021
                        }
1022
                        myLine = (ILineSymbol) multiLine.getLayer(0);
1023
                }
1024

    
1025
                BasicStroke str= (BasicStroke) myLine.getLineStyle().getStroke();
1026

    
1027
                if (myLine.getColor() != null) {
1028
                        stroke.setExpressionColor(SLDUtils.convertColorToHexString(myLine.getColor()));
1029
                        float opacity =(float ) myLine.getColor().getAlpha() / (float) 255;
1030
                        stroke.setExpressionOpacity(Double.toString(opacity));
1031
                }
1032
                if(str != null) {
1033
                        stroke.setMyDashArray(str.getDashArray());
1034
                        stroke.setExpressionLineCap(SLDUtils.convertLineCapToString(str.getEndCap()));
1035
                        stroke.setExpressionLineJoin(SLDUtils.convertLineJoinToString(str.getLineJoin()));
1036
                        stroke.setExpressionWidth(Double.toString(myLine.getLineWidth()));                        
1037
                        stroke.setExpressionDashOffset(Double.toString(str.getDashPhase()));
1038
                }
1039
                if(myLine instanceof MarkerLineSymbol) {
1040
                        MarkerLineSymbol myMarkerLine = (MarkerLineSymbol) myLine;
1041
                        float[] dash =  {Float.valueOf(String.valueOf(myLine.getLineWidth())),
1042
                                        Float.valueOf(String.valueOf(myMarkerLine.getSeparation()))};
1043
                        stroke.setMyDashArray(dash);
1044
                        stroke.setGraphic(createGraphic(myMarkerLine.getMarker(), sldVersion));
1045
                }
1046
                else if (myLine instanceof PictureLineSymbol) {
1047
                        PictureLineSymbol picLine = (PictureLineSymbol)myLine;
1048
                        SLDGraphic graphic = (SLDGraphic) Class.forName(
1049
                                        classNameForVersion(SLDGraphic.class, sldVersion)).newInstance();
1050
                        graphic.addExternalGraphic(createPictureFill(picLine.getImagePath(), sldVersion));
1051
                        stroke.setGraphic(graphic);
1052
                }
1053

    
1054
                return stroke;
1055
        }
1056

    
1057

    
1058

    
1059
        private SLDGraphic createGraphic(IMarkerSymbol myMarker, String sldVersion)
1060
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
1061
                SLDGraphic graphic = (SLDGraphic) Class.forName(
1062
                                classNameForVersion(SLDGraphic.class, sldVersion)).newInstance();
1063

    
1064
                graphic.getExpressionSize().setLiteral(String.valueOf(myMarker.getSize()));
1065
                graphic.getExpressionRotation().setLiteral(String.valueOf(myMarker.getRotation()));
1066
                graphic.getExpressionOpacity().setLiteral(String.valueOf(myMarker.getColor().getAlpha()/255));
1067

    
1068
                if (myMarker instanceof SimpleMarkerSymbol) {
1069
                        SimpleMarkerSymbol marker = (SimpleMarkerSymbol) myMarker;
1070
                        SLDStroke str = (SLDStroke) Class.forName(
1071
                                        classNameForVersion(SLDStroke.class, sldVersion)).newInstance();
1072
                        SLDMark mark = (SLDMark) Class.forName(
1073
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
1074

    
1075
                        if (marker.getOutlineColor() != null)
1076
                                str.setExpressionColor(SLDUtils.convertColorToHexString(marker.getOutlineColor()));
1077

    
1078
                        str.setExpressionWidth(String.valueOf(marker.getOutlineSize()));
1079
                        mark.setStroke(str);        
1080
                        if (marker.getColor() != null) {
1081

    
1082
                                SLDFill fill = (SLDFill) Class.forName(
1083
                                                classNameForVersion(SLDFill.class, sldVersion)).newInstance();
1084

    
1085

    
1086
                                FExpression expressionColor = new FExpression();
1087
                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(marker.getColor()));
1088
                                fill.setExpressionColor(expressionColor);
1089

    
1090
                                FExpression expressionOpacity = new FExpression();
1091
                                float opacity =(float ) marker.getColor().getAlpha() / (float) 255;
1092
                                expressionOpacity.setLiteral(String.valueOf(opacity));
1093
                                fill.setExpressionOpacity(expressionOpacity);
1094

    
1095
                                mark.setFill(fill);
1096

    
1097
                        }
1098
                        FExpression wellKnownName = new FExpression();
1099
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(marker.getStyle()));
1100
                        mark.setWellKnownName(wellKnownName);
1101
                        graphic.addMark(mark);
1102
                }
1103
                else if (myMarker instanceof PictureMarkerSymbol) {
1104
                        PictureMarkerSymbol marker =  (PictureMarkerSymbol)myMarker;
1105
                        SLDExternalGraphic ext = (SLDExternalGraphic) Class.forName(
1106
                                        classNameForVersion(SLDExternalGraphic.class, sldVersion)).newInstance();
1107
                        try {
1108
                                if (marker.getImagePath() != null)
1109
                                        ext.addOnlineResource(new URL(marker.getImagePath()));
1110
                        } catch (MalformedURLException e) {
1111
                                e.printStackTrace();
1112
                        }
1113
                        graphic.addExternalGraphic(ext);        
1114
                }
1115
                else if (myMarker instanceof CharacterMarkerSymbol) {
1116
                        SLDMark mark = (SLDMark) Class.forName(
1117
                                        classNameForVersion(SLDMark.class, sldVersion)).newInstance();
1118
                        FExpression wellKnownName = new FExpression();
1119
                        wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
1120
                        mark.setWellKnownName(wellKnownName);
1121

    
1122
                        if (myMarker.getColor() != null) {
1123
                                SLDFill fill = (SLDFill) Class.forName(
1124
                                                classNameForVersion(SLDFill.class, sldVersion)).newInstance();
1125

    
1126
                                FExpression expressionColor = new FExpression();
1127
                                expressionColor.setLiteral(SLDUtils.convertColorToHexString(myMarker.getColor()));
1128
                                fill.setExpressionColor(expressionColor);
1129

    
1130
                                FExpression expressionOpacity = new FExpression();
1131
                                float opacity =(float ) myMarker.getColor().getAlpha() / (float) 255;
1132
                                expressionOpacity.setLiteral(String.valueOf(opacity));
1133
                                fill.setExpressionOpacity(expressionOpacity);
1134

    
1135
                                mark.setFill(fill);
1136
                        }
1137
                        graphic.addMark(mark);
1138
                }
1139

    
1140
                return graphic;
1141
        }
1142

    
1143
}
1144