Revision 23205

View differences:

trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/drivers/sld/FMapSLDDriver.java
89 89
import org.xmlpull.v1.XmlPullParserException;
90 90

  
91 91
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
92
import com.hardcode.gdbms.engine.values.StringValue;
92 93
import com.hardcode.gdbms.engine.values.Value;
93 94
import com.iver.cit.gvsig.fmap.core.FShape;
94 95
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
......
120 121
/**
121 122
 * Implements the driver for Styled Layer Descriptor documents in order
122 123
 * to allow gvSIG to read or write XML documents of this OGC Standard.
123
 * 
124
 *
124 125
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
125 126
 */
126 127
public class FMapSLDDriver implements IFMapLegendDriver{
......
131 132
	private FLayer layer;
132 133
	private String[] fieldNames;
133 134
	private int[] fieldTypes;
134
	
135

  
135 136
	private static String sldReadVersion;
136 137

  
137 138
	public boolean accept(File f) {
......
149 150
	/**
150 151
	 * Obtains the classifying field names that are used in the .sld
151 152
	 * document
152
	 * 
153
	 *
153 154
	 * @return Array of Strings with the classifying field names
154 155
	 */
155 156
	public String[] getClassifyingFieldNames() { return fieldNames; }
156 157
	/**
157 158
	 * Returns the classifying field types of the classifying field names
158 159
	 * that are used in the .sld document
159
	 * 
160
	 *
160 161
	 * @return Array of integers with the types
161 162
	 */
162
	public int[] getClassifyingFieldTypes() { return fieldTypes; }	
163
	public int[] getClassifyingFieldTypes() { return fieldTypes; }
163 164

  
164 165
	/**
165 166
	 * Returns true if any layer contained in the TOC menu of gvSIG has the
166
	 * same name that is passed as an argument. Otherwise,false. 
167
	 * 
167
	 * same name that is passed as an argument. Otherwise,false.
168
	 *
168 169
	 * @param layer
169
	 * @param name 
170
	 * 
170
	 * @param name
171
	 *
171 172
	 * @return boolean
172 173
	 */
173 174
	private boolean checkIsUsableSldLyr(FLayer layer, String name) {
......
178 179
					return true;
179 180
				}
180 181
			}
181
		} 
182
		}
182 183
		return layer.getName().equals(name);
183 184
	}
184 185

  
......
188 189

  
189 190
		try {
190 191

  
191
			my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(file);		
192
			my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(file);
192 193
			sldReadVersion = my_sld.getVersion();
193 194
			((SLDProtocolHandler) my_sld).parse(file);
194 195

  
......
240 241
	/**
241 242
	 * Takes a SLDLayer and with its information, creates a gvSIG legend. Depending
242 243
	 * on if the layer has filters or not, it will be created a single symbol legend
243
	 * or a filter expression legend 
244
	 * 
244
	 * or a filter expression legend
245
	 *
245 246
	 * @param sldLayer layer contained in a .sld document
246 247
	 * @return a gvSIG legend
247 248
	 * @throws ReadDriverException
......
297 298
				}
298 299
			}
299 300
			return legend;
300
		} 
301
		}
301 302
	}
302 303
	/**
303 304
	 * Checks if the field names contained in an sld document appear in the information
304 305
	 * of the layer that we are using. If all the field names are correct the method
305 306
	 * returns true.Otherwise, false.
306
	 * 
307
	 *
307 308
	 * @param fieldNames
308 309
	 * @return
309 310
	 * @throws ReadDriverException
......
318 319
	}
319 320
	/**
320 321
	 * Transforms a SLD Symbolizer into an ISymbol.
321
	 * 
322
	 *
322 323
	 * @param symbolizer to be transfomed into an ISymbol
323 324
	 * @return ISymbol
324 325
	 * @throws NumberFormatException
......
328 329
	public static ISymbol SLDSymbolizer2ISymbol(ISLDSymbolizer symbolizer) throws NumberFormatException, LegendDriverException {
329 330
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
330 331
		//LINE//
331
		///////////////////////////////////////////////////////////////////////////////////////////////////////////	
332
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
332 333
		if (symbolizer instanceof SLDLineSymbolizer) {
333 334
			SLDLineSymbolizer myLine = (SLDLineSymbolizer) symbolizer;
334 335

  
......
357 358
			if(myLine.getStroke().getFloatDashArray().length == 0) {
358 359
				dash = null;
359 360
			}
360
			else dash = myLine.getStroke().getFloatDashArray(); 
361
			else dash = myLine.getStroke().getFloatDashArray();
361 362

  
362 363
			BasicStroke str = new BasicStroke(width,
363 364
					lineCap,
......
395 396
					marker = createMarker(myMark,size,rotation);
396 397
					line.setLineWidth(marker.getSize());
397 398
					marker.setUnit(-1);
398
					marker.setStyle(SLDUtils.setMarkerStyle(myMark.getWellKnownName().getLiteral()));				
399
					marker.setStyle(SLDUtils.setMarkerStyle(myMark.getWellKnownName().getLiteral()));
399 400
					line.setMarker(marker);
400 401
					line.setAlpha(marker.getColor().getAlpha());
401 402
					if(str.getDashArray() != null)
......
423 424

  
424 425
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
425 426
		//POLYGON//
426
		///////////////////////////////////////////////////////////////////////////////////////////////////////////				
427
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
427 428
		else if (symbolizer instanceof SLDPolygonSymbolizer) {
428 429
			SLDPolygonSymbolizer myPolygon = (SLDPolygonSymbolizer)symbolizer;
429 430
			//SimpleFillSymbol
......
438 439
						Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
439 440
						simpleFill.setFillColor(myColor);
440 441
					}
441
					else 
442
					else
442 443
						simpleFill.setFillColor(color);
443 444
				}
444 445

  
......
448 449
					simpleFill.setOutline(createOutline(myPolygon.getStroke()));
449 450
				}
450 451
				return simpleFill;
451
			}	
452
			}
452 453
			else if (myPolygon.getFill() != null && myPolygon.getFill().getFillGraphic() != null) {
453 454
				SLDGraphic myFill = myPolygon.getFill().getFillGraphic();
454 455

  
......
499 500
		}
500 501
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
501 502
		//POINT//
502
		///////////////////////////////////////////////////////////////////////////////////////////////////////////	
503
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
503 504
		else if (symbolizer instanceof SLDPointSymbolizer) {
504 505
			SLDPointSymbolizer myPoint = (SLDPointSymbolizer)symbolizer;
505 506
			//Marker->SimpleMarkerSymbol
......
540 541
					Color myColor = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
541 542
					marker.setColor(myColor);
542 543
				}
543
				else 
544
				else
544 545
					marker.setColor(color);
545 546
			}
546 547
		}
......
551 552
				marker.setOutlineColor(myMark.getStroke().getStrokeColor());
552 553
				marker.setOutlineSize(myMark.getStroke().getStrokeWidth());
553 554
				marker.setOutlined(true);
554
			}	
555
			}
555 556
		}
556 557
		if( size.getLiteral() != null) {
557
			marker.setSize(Float.valueOf(size.getLiteral()));	
558
			marker.setSize(Float.valueOf(size.getLiteral()));
558 559
		}
559
		if( rotation.getLiteral() != null) 
560
		if( rotation.getLiteral() != null)
560 561
			marker.setRotation(Float.valueOf(rotation.getLiteral()));
561 562

  
562 563
		return marker;
......
576 577
					marker.setUnit(-1);
577 578
					marker.setSize(Float.valueOf(size.getLiteral()));
578 579
				}
579
				if( rotation.getLiteral() != null) 
580
				if( rotation.getLiteral() != null)
580 581
					marker.setRotation(Float.valueOf(rotation.getLiteral()));
581 582
			}
582 583
		} catch (IOException e1) {
......
589 590

  
590 591
	public static ILineSymbol createOutline(SLDStroke stroke) throws NumberFormatException, LegendDriverException {
591 592
		SLDLineSymbolizer line ;
592
		
593

  
593 594
		try {
594 595
			line = (SLDLineSymbolizer) Class.forName(
595 596
					classNameForVersion(SLDLineSymbolizer.class,sldReadVersion )).newInstance();
......
692 693
					}
693 694
					else {
694 695
						xmlBuilder.openTag(SLDTags.RULE);
695
						xmlBuilder.writeTag(FilterTags.NAME, ""+df.format(Double.valueOf(myVal.toString())));
696
						if(myVal instanceof StringValue)
697
							xmlBuilder.writeTag(FilterTags.NAME, ""+myVal.toString());
698
						else
699
							xmlBuilder.writeTag(FilterTags.NAME, ""+df.format(Double.valueOf(myVal.toString())));
700

  
696 701
						xmlBuilder.openTag(FilterTags.FILTER);
697 702

  
698 703
						xmlBuilder.openTag(FilterTags.PROPERTYISEQUALTO);
699 704
						xmlBuilder.writeTag(FilterTags.PROPERTYNAME,myLegend.getClassifyingFieldNames()[0]);
700
						xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(Double.valueOf(myVal.toString())));
705
						if(myVal instanceof StringValue)
706
							xmlBuilder.writeTag(FilterTags.LITERAL, ""+myVal.toString());
707
						else
708
							xmlBuilder.writeTag(FilterTags.LITERAL, ""+df.format(Double.valueOf(myVal.toString())));
709

  
701 710
						xmlBuilder.closeTag();
702 711
						xmlBuilder.closeTag();
703 712

  
......
716 725
				Object[] values = myLegend.getValues();
717 726
				String[] descriptions = myLegend.getDescriptions();
718 727
				for(int i = 0; i < symbols.length; i++ ){
719
					Expression filter4Symbol = (Expression) values[i]; 
728
					Expression filter4Symbol = (Expression) values[i];
720 729
					FilterFactory filterFactory = new FilterFactory();
721 730

  
722 731
					xmlBuilder.openTag(SLDTags.RULE);
......
774 783
//		String cName = packageName + "sld" + versionString + "." +
775 784
//		subPackage + sldClass.getName().substring(
776 785
//				sldClass.getName().lastIndexOf(".", sldClass.getName().length())) + versionString;
777
		
786

  
778 787
		String path = sldClass.toString();
779 788
		path = sldClass.toString().substring("class ".length(),path.length());
780 789
		path = path.replace(".sld.", ".sld.sld"+versionString+".");
781 790
		String cName = path+versionString;
782
		
791

  
783 792
		return cName;
784 793
	}
785 794
	/**
786 795
	 * Transforms an ISymbol into a SLD Symbolizer
787
	 * 
796
	 *
788 797
	 * @param symbol ISymbol to be transformed
789 798
	 * @return SLD Symbolizer
790
	 * @throws LegendDriverException 
791
	 * @throws ClassNotFoundException 
792
	 * @throws IllegalAccessException 
793
	 * @throws InstantiationException 
799
	 * @throws LegendDriverException
800
	 * @throws ClassNotFoundException
801
	 * @throws IllegalAccessException
802
	 * @throws InstantiationException
794 803
	 */
795
	private ISLDSymbolizer ISymbol2SLDSymbolizer(ISymbol symbol, String sldVersion) 
804
	private ISLDSymbolizer ISymbol2SLDSymbolizer(ISymbol symbol, String sldVersion)
796 805
	throws LegendDriverException, InstantiationException, IllegalAccessException, ClassNotFoundException {
797 806
		//MultiLayer Symbol
798 807
		if(symbol instanceof IMultiLayerSymbol) {
799 808
			//Made up of  lines
800 809
			if(symbol instanceof MultiLayerLineSymbol) {
801 810
				MultiLayerLineSymbol multiLine = (MultiLayerLineSymbol) symbol;
802
				SLDMultiLineSymbolizer multiSldLine = 
811
				SLDMultiLineSymbolizer multiSldLine =
803 812
					(SLDMultiLineSymbolizer) Class.forName(
804 813
							classNameForVersion(SLDMultiLineSymbolizer.class, sldVersion)).newInstance();
805 814
				for (int i = 0; i < multiLine.getLayerCount(); i++) {
806
					multiSldLine.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiLine.getLayer(i), sldVersion));	
815
					multiSldLine.addSldLine((SLDLineSymbolizer)ISymbol2SLDSymbolizer(multiLine.getLayer(i), sldVersion));
807 816
				}
808 817
				return (ISLDSymbolizer) multiSldLine;
809 818
			}
810 819
			//Made up of polygons
811 820
			else if (symbol instanceof MultiLayerFillSymbol) {
812 821
				MultiLayerFillSymbol multiFill = (MultiLayerFillSymbol) symbol;
813
				SLDMultiPolygonSymbolizer multiSldPolygon = 
822
				SLDMultiPolygonSymbolizer multiSldPolygon =
814 823
					(SLDMultiPolygonSymbolizer) Class.forName(
815 824
							classNameForVersion(SLDMultiPolygonSymbolizer.class, sldVersion)).newInstance();
816 825
				for (int i = 0; i < multiFill.getLayerCount(); i++) {
817
					multiSldPolygon.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiFill.getLayer(i), sldVersion));	
826
					multiSldPolygon.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiFill.getLayer(i), sldVersion));
818 827
				}
819 828
				return (ISLDSymbolizer) multiSldPolygon;
820 829
			}
......
824 833
				SLDMultiPointSymbolizer multiSldPoint = (SLDMultiPointSymbolizer) Class.forName(
825 834
						classNameForVersion(SLDMultiPointSymbolizer.class, sldVersion)).newInstance();
826 835
				for (int i = 0; i < multiMarker.getLayerCount(); i++) {
827
					multiSldPoint.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiMarker.getLayer(i), sldVersion));	
836
					multiSldPoint.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiMarker.getLayer(i), sldVersion));
828 837
				}
829 838
				return (ISLDSymbolizer) multiSldPoint;
830 839
			}
831 840
		}
832
//		MultishapeSymbol not yet supported		
841
//		MultishapeSymbol not yet supported
833 842
//		else if(symbol instanceof MultiShapeSymbol) {
834 843
//		MultiShapeSymbol multiShape = (MultiShapeSymbol) symbol;
835 844
//		SLDMultiShapeSymbolizer sldMultiShape = new SLDMultiShapeSymbolizer();
......
838 847
//		sldMultiShape.addSldPolygon((SLDPolygonSymbolizer)ISymbol2SLDSymbolizer(multiShape.getFillSymbol()));
839 848
//		sldMultiShape.addSldPoint((SLDPointSymbolizer)ISymbol2SLDSymbolizer(multiShape.getMarkerSymbol()));
840 849

  
841
//		return sldMultiShape;	
850
//		return sldMultiShape;
842 851
//		}
843 852
		//Single symbol
844 853
		else {
......
944 953
			for (int i = 0; i < multi.getLayerCount(); i++) {
945 954
				if (max < ((IMarkerSymbol)multi.getLayer(i)).getSize()) {
946 955
					marker =  (IMarkerSymbol) multi.getLayer(i);
947
					max = ((IMarkerSymbol)multi.getLayer(i)).getSize(); 
956
					max = ((IMarkerSymbol)multi.getLayer(i)).getSize();
948 957
				}
949 958
			}
950 959
			myGraphic = createMarkerFill(marker, myGraphic, sldVersion);
......
970 979
			wellKnownName.setLiteral(SLDUtils.getMarkWellKnownName(SimpleMarkerSymbol.SQUARE_STYLE));
971 980
			mark.setWellKnownName(wellKnownName);
972 981
			mark.setFill(fill);
973
			myGraphic.addMark(mark);	
982
			myGraphic.addMark(mark);
974 983
		}
975 984

  
976 985
		FExpression expressionSize = new FExpression();
......
989 998

  
990 999

  
991 1000

  
992
	private SLDExternalGraphic createPictureFill(String imagePath, String sldVersion) 
1001
	private SLDExternalGraphic createPictureFill(String imagePath, String sldVersion)
993 1002
	throws InstantiationException, IllegalAccessException, ClassNotFoundException {
994 1003
		SLDExternalGraphic extGra = (SLDExternalGraphic) Class.forName(
995 1004
				classNameForVersion(SLDExternalGraphic.class, sldVersion)).newInstance();
......
1018 1027
			for (int i = 0; i < multiLine.getLayerCount(); i++) {
1019 1028
				ILineSymbol myNewLine =  (ILineSymbol) multiLine.getLayer(i);
1020 1029
				if (myLine.getLineWidth() < myNewLine.getLineWidth())
1021
					myLine = myNewLine;	
1030
					myLine = myNewLine;
1022 1031
			}
1023 1032
			myLine = (ILineSymbol) multiLine.getLayer(0);
1024 1033
		}
......
1034 1043
			stroke.setFloatDashArray(str.getDashArray());
1035 1044
			stroke.setExpressionLineCap(SLDUtils.convertLineCapToString(str.getEndCap()));
1036 1045
			stroke.setExpressionLineJoin(SLDUtils.convertLineJoinToString(str.getLineJoin()));
1037
			stroke.setExpressionWidth(Double.toString(myLine.getLineWidth()));			
1046
			stroke.setExpressionWidth(Double.toString(myLine.getLineWidth()));
1038 1047
			stroke.setExpressionDashOffset(Double.toString(str.getDashPhase()));
1039 1048
		}
1040 1049
		if(myLine instanceof MarkerLineSymbol) {
......
1077 1086
				str.setExpressionColor(SLDUtils.convertColorToHexString(marker.getOutlineColor()));
1078 1087

  
1079 1088
			str.setExpressionWidth(String.valueOf(marker.getOutlineSize()));
1080
			mark.setStroke(str);	
1089
			mark.setStroke(str);
1081 1090
			if (marker.getColor() != null) {
1082 1091

  
1083 1092
				SLDFill fill = (SLDFill) Class.forName(
......
1111 1120
			} catch (MalformedURLException e) {
1112 1121
				e.printStackTrace();
1113 1122
			}
1114
			graphic.addExternalGraphic(ext);	
1123
			graphic.addExternalGraphic(ext);
1115 1124
		}
1116 1125
		else if (myMarker instanceof CharacterMarkerSymbol) {
1117 1126
			SLDMark mark = (SLDMark) Class.forName(

Also available in: Unified diff