Statistics
| Revision:

svn-gvsig-desktop / tags / CatalogYNomenclator_v1_1_1005_Build_0 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / utils / Strings.java @ 12766

History | View | Annotate | Download (7.74 KB)

1

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

    
44
import java.util.TreeMap;
45
import java.util.Vector;
46

    
47
/**
48
 * String Functions
49
 * 
50
 * 
51
 * @author Jorge Piera Llodra (piera_jor@gva.es)
52
 */
53
public class Strings {
54

    
55
/**
56
 * Replace a part of a String
57
 * 
58
 * 
59
 * @return 
60
 * @param str String to find the pattern
61
 * @param pattern Pattern to find
62
 * @param replace String to replace
63
 */
64
    public static String replace(String str, String pattern, String replace) {        
65
        int s = 0;
66
        int e = 0;
67
        StringBuffer result = new StringBuffer();
68
        while ((e = str.indexOf(pattern, s)) >= 0) {
69
            result.append(str.substring(s, e));
70
            result.append(replace);
71
            s = e + pattern.length();
72
        }
73
        result.append(str.substring(s));
74
        return result.toString();
75
    } 
76

    
77
/**
78
 * Find a pattern into one array
79
 * 
80
 * 
81
 * @return 
82
 * @param pattern 
83
 * @param array 
84
 */
85
    public static boolean find(String pattern, String[] array) {        
86
        if (array != null) {
87
            for (int i = 0; i < array.length; i++)
88
                if (pattern.equals(array[i])) {
89
                    return true;
90
                }
91
        }
92
        return false;
93
    } 
94

    
95
/**
96
 * This function joins two strings
97
 * 
98
 * 
99
 * @return 
100
 * @param s1 
101
 * @param s2 
102
 */
103
    public static String[] join(String[] s1, String[] s2) {        
104
        if (s1 == null) {
105
            return s2;
106
        }
107
        if (s2 == null) {
108
            return s1;
109
        }
110
        String[] out = new String[s1.length + s2.length];
111
        for (int i = 0; i < s1.length; i++)
112
            out[i] = s1[i];
113
        for (int i = 0; i < s2.length; i++)
114
            out[s1.length + i] = s2[i];
115
        return out;
116
    } 
117

    
118
/**
119
 * returns a list (comma-separated) in one unique string
120
 * 
121
 * 
122
 * @return 
123
 * @param input 
124
 */
125
    public static String getComaSeparated(String[] input) {        
126
        return getXSeparated("," , input);
127
    } 
128

    
129
/**
130
 * returns a list (blanck-separated) in one unique string
131
 * 
132
 * 
133
 * @return 
134
 * @param input 
135
 */
136
    public static String getBlankSeparated(String[] input) {        
137
      return getXSeparated(" " , input);
138
    } 
139

    
140
/**
141
 * returns a list (X-Character-separated) in one unique string
142
 * 
143
 * 
144
 * @return 
145
 * @param X 
146
 * @param input 
147
 */
148
    private static String getXSeparated(String X, String[] input) {        
149
        String output = "";
150
        if (input.length == 0) {
151
            return "";
152
        }
153
        output = input[0];
154
        for (int i = 1; i < input.length; i++)
155
            output = output + X + input[i];
156
        return output;
157
    } 
158

    
159
/**
160
 * This method creates an String of words (blanck separated)
161
 * enveloped by asteriscs: Example
162
 * "This is an example" returns
163
 * "*This* *is* *an* *example*"
164
 * 
165
 * 
166
 * @return String
167
 * @param array Input String
168
 */
169
    public static String addAsteriscsFromAnArray(String array) {        
170
        String[] s = array.split(" ");
171
        String output = "";
172
        for (int i=0 ; i<s.length ; i++){
173
            output = output + " *" + s[i] + "*";
174
        }
175
        return output;
176
    } 
177

    
178
/**
179
 * This function is used to try if a string is a number.
180
 * 
181
 * 
182
 * @return The numbers of the String
183
 * @param str Input String
184
 * @param charNumber Number of chars that must be tried
185
 */
186
    public static String tryIfIsNumber(String str, int charNumber) {        
187
        char[] source = str.toCharArray();
188
        if (source.length < charNumber)
189
            charNumber = source.length;
190
        char[] resultado = new char[charNumber];
191
        int j = 0;
192
        for (int i = 0; i < resultado.length; i++)
193
            if (i < (source.length) && (source[i] >= '0') && (source[i] <= '9'))
194
                resultado[j++] = source[i];
195
        return new String(resultado,0,j);
196
    } 
197
    
198
    /**
199
     * This method remove all the string accents
200
     * @param str
201
     * Input String
202
     * @return
203
     * String withOut accents
204
     */
205
    public static String removeAccents(String str){
206
            str = str.replace ('?','a');
207
            str = str.replace ('?','e');
208
            str = str.replace ('?','i');
209
            str = str.replace ('?','o');
210
            str = str.replace ('?','u'); 
211
            str = str.replace ('?','A');
212
            str = str.replace ('?','E');
213
            str = str.replace ('?','I');
214
            str = str.replace ('?','O');
215
            str = str.replace ('?','a');
216
            str = str.replace ('?','e');
217
            str = str.replace ('?','i');
218
            str = str.replace ('?','o');
219
            str = str.replace ('?','u'); 
220
            str = str.replace ('?','A');
221
            str = str.replace ('?','E');
222
            str = str.replace ('?','I');
223
            str = str.replace ('?','O');
224
            str = str.replace ('?','U'); 
225
            str = str.replace ('?','a');
226
            str = str.replace ('?','e');
227
            str = str.replace ('?','i');
228
            str = str.replace ('?','o');
229
            str = str.replace ('?','u'); 
230
            str = str.replace ('?','A');
231
            str = str.replace ('?','E');
232
            str = str.replace ('?','I');
233
            str = str.replace ('?','O');
234
            str = str.replace ('?','U'); 
235
            return str;
236
    }    
237
    
238
    /**
239
     * This method returns all the forms than can have a 
240
     * word to do an advanced search. A form could be
241
     * to change all the characters to lower case, other one
242
     * could be to change only the first character to
243
     * upper case, ...
244
     * @param str
245
     * Input string
246
     * @return
247
     * A vector of strings
248
     */
249
    public static Vector allWordForms(String str,boolean withAccents){
250
            Vector v = new Vector();
251
            
252
            if (!withAccents){
253
                    str = str = Strings.removeAccents(str);
254
            }            
255
            v.add(str);
256
            v.add(str.toLowerCase());
257
            v.add(str.toUpperCase());
258
            v.add(Strings.getUperCaseFirst(str));
259
            if (withAccents){
260
                    str = Strings.removeAccents(str);
261
                    v.add(str.toLowerCase());
262
                    v.add(str.toUpperCase());
263
                    v.add(Strings.getUperCaseFirst(str));
264
            }    
265
            return v;
266
    }
267
    
268
    /**
269
     * It returns an string with the firs chacarter in UperCase
270
     * @param str
271
     * Input str
272
     * @return
273
     */
274
    public static String getUperCaseFirst(String str){
275
            if (str.length() == 0){
276
                    return str;
277
            }
278
            
279
            if (str.length() == 1){
280
                    return str.toUpperCase();
281
            }
282
            
283
            return str.substring(0,1).toUpperCase() + str.substring(1,str.length()).toLowerCase();
284
    }
285
    
286
    /**
287
     * Creates a TreeMap froma KVP
288
     * @param pairValues
289
     * KVP string
290
     * @return
291
     * TreeMap
292
     */
293
    public static TreeMap separateParams(String pairValues){
294
        TreeMap map = new TreeMap(); 
295
                String[] params = pairValues.split("&");
296
                for (int i = 0; i < params.length; i++) {
297
                        String[] nameValue = params[i].split("=");
298
                        map.put(nameValue[0].toUpperCase(), nameValue[1]);
299
                }
300
                return map;
301
    }
302
    
303
    
304
 }