Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / remoteClient / sld / SLDUtils.java @ 20768

History | View | Annotate | Download (5.52 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.remoteClient.sld;
42

    
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45
import java.util.StringTokenizer;
46

    
47
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
48
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException;
49

    
50
/**
51
 * Implements an utility class for SLD functionality
52
 * 
53
 * @author Pepe Vidal Salvador  jose.vidal.salvador@iver.es
54
 *
55
 */
56
public class SLDUtils {
57

    
58

    
59
        public static Color convertHexStringToColor(String str) throws NumberFormatException, LegendDriverException{
60
                int multiplier = 1;
61
                StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+");
62
                while (tokenizer.hasMoreTokens()) {
63
                        multiplier = 1;
64
                        String token = tokenizer.nextToken();
65
                        if (null == token) {
66
                                throw new NumberFormatException(str);
67
                        }
68
                        if (token.startsWith("-")) {
69
                                multiplier = -1;
70
                                token = token.substring(1);
71
                        }
72
                        int point_index = token.indexOf(".");
73
                        if (point_index > 0) {
74
                                token = token.substring(0, point_index);
75
                        } else if (point_index == 0) {
76
                                return new Color(0);
77
                        }
78
                        try {
79
                                if (token.startsWith("0x")) {
80
                                        return new Color(multiplier
81
                                                        * Integer.parseInt(token.substring(2), 16));
82
                                } else if (token.startsWith("#")) {
83
                                        return new Color(multiplier
84
                                                        * Integer.parseInt(token.substring(1), 16));
85
                                } else if (token.startsWith("0") && !token.equals("0")) {
86
                                        return new Color(multiplier * Integer.parseInt(token.substring(1), 8));
87
                                } else {
88
                                        return new Color(multiplier * Integer.parseInt(token));
89
                                }
90
                        } catch (NumberFormatException e) {
91
                                continue;
92
                        }
93
                }
94
                throw new LegendDriverException (LegendDriverException.PARSE_LEGEND_FILE_ERROR);
95

    
96
        }
97

    
98
        
99
        public static String convertOpacityToString(float alpha) {
100
                return String.valueOf((float)(alpha/255));
101
        }
102

    
103
        public static String convertLineJoinToString(int lineJoin) {
104
                if (lineJoin == BasicStroke.JOIN_BEVEL)
105
                        return "bevel";
106
                else if (lineJoin == BasicStroke.JOIN_MITER)
107
                        return "miter";
108
                else if (lineJoin == BasicStroke.JOIN_ROUND)
109
                        return "round";
110
                return null;
111
        }
112

    
113
        public static String convertLineCapToString(int endCap) {
114
                if (endCap == BasicStroke.CAP_BUTT)
115
                        return "butt";
116
                else if (endCap == BasicStroke.CAP_ROUND)
117
                        return "round";
118
                else if (endCap == BasicStroke.CAP_SQUARE)
119
                        return "square";
120

    
121
                return null;        
122
        }
123

    
124
        
125
        public static int setMarkerStyle(String name) {
126
                if (name == null )
127
                        return SimpleMarkerSymbol.SQUARE_STYLE;
128
                else if (name.compareTo(SLDTags.CIRCLE) == 0)
129
                        return SimpleMarkerSymbol.CIRCLE_STYLE;
130
                else if (name.compareTo(SLDTags.TRIANGLE) == 0)
131
                        return SimpleMarkerSymbol.TRIANGLE_STYLE;
132
                else if (name.compareTo(SLDTags.STAR) == 0)
133
                        return SimpleMarkerSymbol.STAR_STYLE;
134
                else if (name.compareTo(SLDTags.CROSS) == 0)        
135
                        return SimpleMarkerSymbol.CROSS_STYLE;
136

    
137
                return SimpleMarkerSymbol.SQUARE_STYLE;
138
        }
139

    
140
        public static String convertColorToHexString(java.awt.Color c)
141
        {
142
                String str = Integer.toHexString( c.getRGB() & 0xFFFFFF );
143
                return ( "#" + "000000".substring( str.length() ) + str.toUpperCase() );
144
        }
145

    
146

    
147
        public static boolean isANumber(String s) {
148
                final String digit = "([0-9])+" + ".?" + "([0-9])*";
149
                if (s.matches(digit))
150
                        return true;
151
                return false;
152
        }
153
        public static boolean isColor(String s) {
154
                final String alpha = "[0-9a-fA-F]";
155
                final String color = "#"+alpha+"{6}";
156
                if (s.matches(color)) 
157
                        return true;
158
                return false;
159
        }
160

    
161

    
162
        public static String getMarkWellKnownName(int style) {
163
                
164
                if (style == SimpleMarkerSymbol.CIRCLE_STYLE)
165
                        return "circle";
166
                else if (style == SimpleMarkerSymbol.CROSS_STYLE)
167
                        return "cross";
168
                else if (style == SimpleMarkerSymbol.SQUARE_STYLE)
169
                        return "square";
170
                else if (style == SimpleMarkerSymbol.TRIANGLE_STYLE)
171
                        return "triangle";
172
                else if (style == SimpleMarkerSymbol.STAR_STYLE)
173
                        return "star";
174
                
175
                return "square";        
176
        }
177

    
178

    
179
        public static boolean isLineJoin(String literal) {
180
                if (literal.compareTo("bevel")== 0 || literal.compareTo("miter")== 0 || literal.compareTo("round")== 0)
181
                        return true;
182
                return false;
183
        }
184
        
185
        public static boolean isLineCap(String literal) {
186
                if (literal.compareTo("butt")== 0 || literal.compareTo("round")== 0 || literal.compareTo("square")== 0)
187
                        return true;
188
                return false;
189
        }
190
}