Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / temp / org.gvsig.sldsupport.lib.api / oldcode / util / SLDUtils.java @ 40781

History | View | Annotate | Download (6.98 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.sldsupport.util;
42

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

    
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.geom.type.GeometryType;
51
import org.gvsig.sldsupport.exception.SLDReadException;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55
import org.xmlpull.v1.XmlPullParser;
56

    
57
/**
58
 * Implements an utility class for SLD functionality
59
 * 
60
 * @author Pepe Vidal Salvador  jose.vidal.salvador@iver.es
61
 *
62
 */
63
public class SLDUtils {
64

    
65
        private static GeometryManager gm = null;
66
        private static Logger logger = LoggerFactory.getLogger(SLDUtils.class);
67

    
68
        public static Color convertHexStringToColor(String str)
69
                        throws NumberFormatException, SLDReadException {
70
                
71
                int multiplier = 1;
72
                StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+");
73
                while (tokenizer.hasMoreTokens()) {
74
                        multiplier = 1;
75
                        String token = tokenizer.nextToken();
76
                        if (null == token) {
77
                                throw new NumberFormatException(str);
78
                        }
79
                        if (token.startsWith("-")) {
80
                                multiplier = -1;
81
                                token = token.substring(1);
82
                        }
83
                        int point_index = token.indexOf(".");
84
                        if (point_index > 0) {
85
                                token = token.substring(0, point_index);
86
                        } else if (point_index == 0) {
87
                                return new Color(0);
88
                        }
89
                        try {
90
                                if (token.startsWith("0x")) {
91
                                        return new Color(multiplier
92
                                                        * Integer.parseInt(token.substring(2), 16));
93
                                } else if (token.startsWith("#")) {
94
                                        return new Color(multiplier
95
                                                        * Integer.parseInt(token.substring(1), 16));
96
                                } else if (token.startsWith("0") && !token.equals("0")) {
97
                                        return new Color(multiplier * Integer.parseInt(token.substring(1), 8));
98
                                } else {
99
                                        return new Color(multiplier * Integer.parseInt(token));
100
                                }
101
                        } catch (NumberFormatException e) {
102
                                continue;
103
                        }
104
                }
105
                throw new SLDReadException(new Exception(
106
                                "Error while parsing '" + str + "'"));
107

    
108
        }
109

    
110
        
111
        public static String convertOpacityToString(float alpha) {
112
                return String.valueOf((float)(alpha/255));
113
        }
114

    
115
        public static String convertLineJoinToString(int lineJoin) {
116
                if (lineJoin == BasicStroke.JOIN_BEVEL)
117
                        return "bevel";
118
                else if (lineJoin == BasicStroke.JOIN_MITER)
119
                        return "miter";
120
                else if (lineJoin == BasicStroke.JOIN_ROUND)
121
                        return "round";
122
                return null;
123
        }
124

    
125
        public static String convertLineCapToString(int endCap) {
126
                if (endCap == BasicStroke.CAP_BUTT)
127
                        return "butt";
128
                else if (endCap == BasicStroke.CAP_ROUND)
129
                        return "round";
130
                else if (endCap == BasicStroke.CAP_SQUARE)
131
                        return "square";
132

    
133
                return null;        
134
        }
135

    
136
        
137
        public static int getMarkerStyle(String name) {
138
                if (name == null )
139
                        return IMarkerSymbol.SQUARE_STYLE;
140
                else if (name.compareTo(SLDTags.CIRCLE) == 0)
141
                        return IMarkerSymbol.CIRCLE_STYLE;
142
                else if (name.compareTo(SLDTags.TRIANGLE) == 0)
143
                        return IMarkerSymbol.TRIANGLE_STYLE;
144
                else if (name.compareTo(SLDTags.STAR) == 0)
145
                        return IMarkerSymbol.STAR_STYLE;
146
                else if (name.compareTo(SLDTags.CROSS) == 0)        
147
                        return IMarkerSymbol.CROSS_STYLE;
148

    
149
                return IMarkerSymbol.SQUARE_STYLE;
150
        }
151

    
152
        public static String convertColorToHexString(java.awt.Color c)
153
        {
154
                String str = Integer.toHexString( c.getRGB() & 0xFFFFFF );
155
                return ( "#" + "000000".substring( str.length() ) + str.toUpperCase() );
156
        }
157

    
158

    
159
        public static boolean isANumber(String s) {
160
                final String digit = "([0-9])+" + ".?" + "([0-9])*";
161
                if (s.matches(digit))
162
                        return true;
163
                return false;
164
        }
165
        public static boolean isColor(String s) {
166
                final String alpha = "[0-9a-fA-F]";
167
                final String color = "#"+alpha+"{6}";
168
                if (s.matches(color)) 
169
                        return true;
170
                return false;
171
        }
172

    
173

    
174
        public static String getMarkWellKnownName(int style) {
175
                
176
                if (style == IMarkerSymbol.CIRCLE_STYLE)
177
                        return "circle";
178
                else if (style == IMarkerSymbol.CROSS_STYLE)
179
                        return "cross";
180
                else if (style == IMarkerSymbol.SQUARE_STYLE)
181
                        return "square";
182
                else if (style == IMarkerSymbol.TRIANGLE_STYLE)
183
                        return "triangle";
184
                else if (style == IMarkerSymbol.STAR_STYLE)
185
                        return "star";
186
                
187
                return "square";        
188
        }
189

    
190

    
191
        public static boolean isLineJoin(String literal) {
192
                if (literal.compareTo("bevel")== 0 || literal.compareTo("miter")== 0 || literal.compareTo("round")== 0)
193
                        return true;
194
                return false;
195
        }
196
        
197
        public static boolean isLineCap(String literal) {
198
                if (literal.compareTo("butt")== 0 || literal.compareTo("round")== 0 || literal.compareTo("square")== 0)
199
                        return true;
200
                return false;
201
        }
202
        
203
        
204
    public static boolean isPolygonal(int ty) {
205
            GeometryType gt = null;
206
            try {
207
                        gt = gm().getGeometryType(ty, Geometry.SUBTYPES.GEOM2D);
208
                } catch (Exception e) {
209
                        logger.error("While getting gtype.", e);
210
                        return false;
211
                }
212
            
213
            return         gt.isTypeOf(Geometry.TYPES.SURFACE)
214
                            || ty == Geometry.TYPES.MULTISURFACE;
215
    }
216

    
217

    
218
    public static boolean isLinear(int ty) {
219
            
220
            GeometryType gt = null;
221
            try {
222
                        gt = gm().getGeometryType(ty, Geometry.SUBTYPES.GEOM2D);
223
                } catch (Exception e) {
224
                        logger.error("While getting gtype.", e);
225
                        return false;
226
                }
227
            
228
            return         gt.isTypeOf(Geometry.TYPES.CURVE)
229
                            || ty == Geometry.TYPES.MULTICURVE;
230
    }
231
    
232
    public static boolean isPoint(int ty) {
233
        if (ty == Geometry.TYPES.POINT
234
            || ty == Geometry.TYPES.MULTIPOINT
235
            ) {
236
            return true;
237
        } else {
238
            return false;
239
        }
240
    }
241
    
242
    private static GeometryManager gm() {
243
            if (gm == null) {
244
                    gm = GeometryLocator.getGeometryManager();
245
            }
246
            return gm;
247
    }
248
    
249
    
250
    
251
}