Revision 8948

View differences:

trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/SLDUtils.java
1 1
package com.iver.cit.gvsig.fmap.core;
2 2

  
3
import java.awt.Color;
4
import java.util.StringTokenizer;
5

  
3 6
import org.geotools.styling.Fill;
4 7
import org.geotools.styling.GraphicImpl;
5 8
import org.geotools.styling.LineSymbolizer;
......
19 22
 * @author laura
20 23
 */
21 24
public class SLDUtils {
22
	
25

  
26
	public static Color convertHexStringToColor(String str) throws NumberFormatException{
27
		          int multiplier = 1;
28
		StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+");
29
		while (tokenizer.hasMoreTokens()) {
30
			multiplier = 1;
31
			String token = tokenizer.nextToken();
32
			if (null == token) {
33
				throw new NumberFormatException(str);
34
			}
35
			if (token.startsWith("-")) {
36
				multiplier = -1;
37
				token = token.substring(1);
38
			}
39
			int point_index = token.indexOf(".");
40
			if (point_index > 0) {
41
				token = token.substring(0, point_index);
42
			} else if (point_index == 0) {
43
				return new Color(0);
44
			}
45
			try {
46
				if (token.startsWith("0x")) {
47
					return new Color(multiplier
48
							* Integer.parseInt(token.substring(2), 16));
49
				} else if (token.startsWith("#")) {
50
					return new Color(multiplier
51
							* Integer.parseInt(token.substring(1), 16));
52
				} else if (token.startsWith("0") && !token.equals("0")) {
53
					return new Color(multiplier * Integer.parseInt(token.substring(1), 8));
54
				} else {
55
					return new Color(multiplier * Integer.parseInt(token));
56
				}
57
			} catch (NumberFormatException e) {
58
				continue;
59
			}
60
		}
61
		throw new NumberFormatException(str);
62

  
63
	}
23 64
	public static String convertColorToHexString(java.awt.Color c)
24 65
	{
25 66
		String str = Integer.toHexString( c.getRGB() & 0xFFFFFF );
......
29 70
	{
30 71
		return "";
31 72
	}
32
	
73

  
33 74
    public static Symbolizer toGeotoolsSymbol(ISymbol sym)
34 75
    {
35 76
    	FSymbol symbol = (FSymbol) sym;
36 77
    	StyleFactory styleFactory = StyleFactory.createStyleFactory();
37
		StyleBuilder styleBuilder = new StyleBuilder();			
78
		StyleBuilder styleBuilder = new StyleBuilder();
38 79
		StrokeImpl theStroke = (StrokeImpl)styleBuilder.createStroke();
39
		GraphicImpl graphic = (GraphicImpl)styleBuilder.createGraphic(); 
80
		GraphicImpl graphic = (GraphicImpl)styleBuilder.createGraphic();
40 81
    	try
41 82
    	{
42
    		switch (symbol.getSymbolType()) 
43
    		{    		
44
				case FConstant.SYMBOL_TYPE_POINT:								
83
    		switch (symbol.getSymbolType())
84
    		{
85
				case FConstant.SYMBOL_TYPE_POINT:
45 86
					PointSymbolizer point = styleFactory.createPointSymbolizer();
46
					graphic.setSize(symbol.getSize());					
87
					graphic.setSize(symbol.getSize());
47 88
					Mark[] mark = new Mark[1];
48 89
					mark[0] = styleFactory.createMark();
49 90
					//
50
					//mark[0].setWellKnownName( getWellKnownName(symbol.getStyle()));
91
					// mark[0].setWellKnownName(
92
					// getWellKnownName(symbol.getStyle()));
51 93
					graphic.setMarks(mark);
52
					
94

  
53 95
					// add, color, graphic, size, ...
54
					return point;					
96
					return point;
55 97
				case FConstant.SYMBOL_TYPE_LINE:
56
					LineSymbolizer line = styleFactory.createLineSymbolizer();					
57
					theStroke.setColor(convertColorToHexString(symbol.getColor()));					
98
					LineSymbolizer line = styleFactory.createLineSymbolizer();
99
					theStroke.setColor(convertColorToHexString(symbol.getColor()));
58 100
					line.setStroke(theStroke);
59 101
					return line;
60 102
				case FConstant.SYMBOL_TYPE_FILL:
61 103
					PolygonSymbolizer polygon = styleFactory.createPolygonSymbolizer();
62 104
					Fill theFill = styleBuilder.createFill(symbol.getColor());
63 105
					//theFill.setOpacity();
64
					polygon.setFill( theFill );										
106
					polygon.setFill( theFill );
65 107
					//theStroke.setColor(symbol.getOutlineColor());
66 108
					return polygon;
67 109
				case FShape.MULTI:
68
					// To implement: ver que clase de simbolo corresponde					
110
					// To implement: ver que clase de simbolo corresponde
69 111
					return null;
70
    		}    
112
    		}
71 113
    		return null;
72 114
    	}
73 115
    	catch(Exception e)
74 116
    	{
75 117
    		e.printStackTrace();
76 118
    		return null;
77
    	}    	
78
    }	
119
    	}
120
    }
79 121
}

Also available in: Unified diff