svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / StringUtilities.java @ 40561
History | View | Annotate | Download (15.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
import java.util.Iterator; |
29 |
import java.util.Random; |
30 |
import java.util.TreeSet; |
31 |
|
32 |
|
33 |
/**
|
34 |
* Clase de utilidad para Strings
|
35 |
*
|
36 |
* @author Fernando Gonz?lez Cort?s
|
37 |
*/
|
38 |
public class StringUtilities { |
39 |
/**
|
40 |
* Inserta una string en otra en la posici?n indicada
|
41 |
*
|
42 |
* @param base String donde se inserta
|
43 |
* @param position posici?n de "base" donde se inserta la String
|
44 |
* @param injerto String que se inserta en "base"
|
45 |
*
|
46 |
* @return String con la inserci?n hecha
|
47 |
*/
|
48 |
public static String insert(String base, int position, String injerto) { |
49 |
return base.substring(0, position) + injerto + |
50 |
base.substring(position); |
51 |
} |
52 |
|
53 |
/**
|
54 |
* Busca en la cadena si la posici?n se encuentra entre un s?mbolo de
|
55 |
* apertura y su correspondiente s?mbolo de clausura
|
56 |
*
|
57 |
* @param string Cadena donde se busca
|
58 |
* @param position posici?n que se est? evaluando
|
59 |
* @param startSymbol s?mbolo de apertura
|
60 |
* @param endSymbol s?mbolo de clausura
|
61 |
*
|
62 |
* @return true si la posici?n dada est? entre un s?mbolo de apertura y
|
63 |
* otro de clausura
|
64 |
*/
|
65 |
public static boolean isBetweenSymbols(String string, int position, |
66 |
String startSymbol, String endSymbol) { |
67 |
TreeSet startSymbolIndexes = new TreeSet(); |
68 |
TreeSet endSymbolIndexes = new TreeSet(); |
69 |
boolean sameSymbolOpen = true; |
70 |
|
71 |
int pos = 0; |
72 |
|
73 |
//Itera sobre la cadena almacenando las posiciones de los s?mbolos de apertura hasta posici?n
|
74 |
while ((pos = string.indexOf(startSymbol, pos)) < position) {
|
75 |
if (pos == -1) { |
76 |
break;
|
77 |
} |
78 |
|
79 |
startSymbolIndexes.add(new Integer(pos)); |
80 |
pos++; |
81 |
} |
82 |
|
83 |
pos = 0;
|
84 |
|
85 |
//Itera sobre la cadena almacenando las posiciones de los s?mbolos de clausura hasta posici?n
|
86 |
while ((pos = string.indexOf(endSymbol, pos)) < position) {
|
87 |
if (pos == -1) { |
88 |
break;
|
89 |
} |
90 |
|
91 |
endSymbolIndexes.add(new Integer(pos)); |
92 |
pos++; |
93 |
} |
94 |
|
95 |
Iterator startIndexesIterator = startSymbolIndexes.iterator();
|
96 |
Iterator endIndexesIterator = endSymbolIndexes.iterator();
|
97 |
Integer startIndex = null; |
98 |
Integer endIndex = null; |
99 |
int count = 0; |
100 |
|
101 |
while (startIndexesIterator.hasNext() || endIndexesIterator.hasNext()) {
|
102 |
if (startIndexesIterator.hasNext()) {
|
103 |
startIndex = (Integer) startIndexesIterator.next();
|
104 |
} |
105 |
|
106 |
if (endIndexesIterator.hasNext()) {
|
107 |
endIndex = (Integer) endIndexesIterator.next();
|
108 |
} |
109 |
|
110 |
if (startIndex == null) { |
111 |
if (endIndexesIterator.hasNext()) {
|
112 |
endIndex = (Integer) startIndexesIterator.next();
|
113 |
} |
114 |
|
115 |
count--; |
116 |
} else if (endIndex == null) { |
117 |
if (startIndexesIterator.hasNext()) {
|
118 |
startIndex = (Integer) startIndexesIterator.next();
|
119 |
} |
120 |
|
121 |
count++; |
122 |
} else {
|
123 |
if (endIndex.intValue() < startIndex.intValue()) {
|
124 |
if (endIndexesIterator.hasNext()) {
|
125 |
endIndex = (Integer) startIndexesIterator.next();
|
126 |
} else {
|
127 |
endIndex = null;
|
128 |
} |
129 |
|
130 |
count--; |
131 |
} else {
|
132 |
if (startIndexesIterator.hasNext()) {
|
133 |
startIndex = (Integer) startIndexesIterator.next();
|
134 |
} else {
|
135 |
startIndex = null;
|
136 |
} |
137 |
|
138 |
count++; |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
if (count == 0) { |
144 |
return false; |
145 |
} else {
|
146 |
return true; |
147 |
} |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Busca en la cadena si la posici?n tiene un n?mero par de symbol delante
|
152 |
* de ella o impar.
|
153 |
*
|
154 |
* @param string Cadena donde se busca
|
155 |
* @param position posici?n que se est? evaluando
|
156 |
* @param symbol s?mbolo que se toma para la comprobaci?n
|
157 |
*
|
158 |
* @return true si hay un n?mero impar de s?mbolos delante de la posici?n
|
159 |
* pos y false en caso contrario
|
160 |
*/
|
161 |
public static boolean isBetweenSymbols(String string, int position, |
162 |
String symbol) {
|
163 |
int pos = 0; |
164 |
|
165 |
int count = 0; |
166 |
|
167 |
while ((pos = string.indexOf(symbol, pos)) < position) {
|
168 |
if (pos == -1) { |
169 |
break;
|
170 |
} |
171 |
|
172 |
count = 1 - count;
|
173 |
pos++; |
174 |
} |
175 |
|
176 |
if (count == 0) { |
177 |
return false; |
178 |
} else {
|
179 |
return true; |
180 |
} |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Obtiene una cadena delimitada por s?mbolos
|
185 |
*
|
186 |
* @param string Cadena de la que se obtendr? la subcadena
|
187 |
* @param symbolSet Conjunto de s?mbolos delimitadores
|
188 |
* @param position Posici?n a partir de la cual se busca la subcadena
|
189 |
*
|
190 |
* @return Cadena delimitada por cualquier combinaci?n de los s?mbolos
|
191 |
* definidos en el SimbolSet, o null si no se encuentra ninguna
|
192 |
*/
|
193 |
public static String substringWithSymbols(String string, |
194 |
SymbolSet symbolSet, int position) {
|
195 |
char[] characters = new char[string.length() - position]; |
196 |
string.getChars(position, string.length(), characters, 0);
|
197 |
|
198 |
for (int i = position; i < characters.length; i++) { |
199 |
if (symbolSet.contains(characters[i])) {
|
200 |
char[] buff = new char[string.length() - i]; |
201 |
int j = 0; |
202 |
|
203 |
while (symbolSet.contains(characters[i])) {
|
204 |
buff[j] = characters[i]; |
205 |
j++; |
206 |
i++; |
207 |
|
208 |
if (i == characters.length) {
|
209 |
break;
|
210 |
} |
211 |
} |
212 |
|
213 |
char[] ret = new char[j]; |
214 |
System.arraycopy(buff, 0, ret, 0, j); |
215 |
|
216 |
return new String(ret); |
217 |
} |
218 |
} |
219 |
|
220 |
return null; |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Encuentra una cadena delimitada por otras dos
|
225 |
*
|
226 |
* @param string Cadena en la que se busca
|
227 |
* @param start Cadena de inicio de la delimitaci?n
|
228 |
* @param end Cadena de final de la delimitaci?n
|
229 |
* @param startingPosition Posici?n en la que se empieza a buscar
|
230 |
*
|
231 |
* @return String cadena delimitada por start y por end
|
232 |
*/
|
233 |
public static String substringDelimited(String string, String start, |
234 |
String end, int startingPosition) { |
235 |
int startIndex = string.indexOf(start, startingPosition);
|
236 |
|
237 |
if (startIndex == -1) { |
238 |
return null; |
239 |
} |
240 |
|
241 |
startIndex += start.length(); |
242 |
|
243 |
int endIndex = string.indexOf(end, startIndex);
|
244 |
|
245 |
if ((startIndex < endIndex) && (endIndex != -1) && (startIndex != -1)) { |
246 |
return string.substring(startIndex, endIndex);
|
247 |
} |
248 |
|
249 |
return null; |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtiene una rect?ngulo como String
|
254 |
*
|
255 |
* @param rect Rect?ngulo a transformar
|
256 |
*
|
257 |
* @return String
|
258 |
*/
|
259 |
public static String rect2String(Rectangle2D rect) { |
260 |
return rect.getMinX() + "," + rect.getMinY() + "," + rect.getWidth() + |
261 |
"," + rect.getHeight();
|
262 |
} |
263 |
|
264 |
/**
|
265 |
* Convierte un String en un rect?ngulo. El string ha de haber sido
|
266 |
* convertida a previamente desde un rectangulo mediante el m?todo
|
267 |
* rect2String
|
268 |
*
|
269 |
* @param rect String
|
270 |
*
|
271 |
* @return Rectangle2D
|
272 |
*/
|
273 |
public static Rectangle2D string2Rect(String rect) { |
274 |
String[] coords = new String[4]; |
275 |
coords = rect.split(",");
|
276 |
|
277 |
Rectangle2D.Double ret = new Rectangle2D.Double(new Double(coords[0]).doubleValue(), |
278 |
new Double(coords[1]).doubleValue(), |
279 |
new Double(coords[2]).doubleValue(), |
280 |
new Double(coords[3]).doubleValue()); |
281 |
|
282 |
return ret;
|
283 |
} |
284 |
|
285 |
/**
|
286 |
* Obtiene la representaci?n de un color como String
|
287 |
*
|
288 |
* @param c Color
|
289 |
*
|
290 |
* @return String
|
291 |
*/
|
292 |
public static String color2String(Color c) { |
293 |
if (c == null) return null; |
294 |
return c.getRed() + "," + c.getGreen() + "," + c.getBlue() + "," + |
295 |
c.getAlpha(); |
296 |
} |
297 |
|
298 |
/**
|
299 |
* Obtiene el color de un string generado con color2String
|
300 |
*
|
301 |
* @param stringColor string
|
302 |
*
|
303 |
* @return Color
|
304 |
*/
|
305 |
public static Color string2Color(String stringColor) { |
306 |
if (stringColor == null || stringColor.equals("null")) return null; |
307 |
String[] ints = new String[4]; |
308 |
ints = stringColor.split(",");
|
309 |
|
310 |
int[] ret = new int[4]; |
311 |
|
312 |
for (int i = 0; i < ret.length; i++) { |
313 |
ret[i] = new Integer(ints[i]).intValue(); |
314 |
} |
315 |
|
316 |
return new Color(ret[0], ret[1], ret[2], ret[3]); |
317 |
|
318 |
/*
|
319 |
long color = new Long(stringColor).longValue();
|
320 |
long alpha = color / 16777216;
|
321 |
color = color % 16777216;
|
322 |
|
323 |
long red = color / 65536;
|
324 |
color = color % 65536;
|
325 |
long green = color / 256;
|
326 |
color = color % 256;
|
327 |
long blue = color;
|
328 |
return new Color(red, green, blue, alpha);*/
|
329 |
} |
330 |
|
331 |
/*
|
332 |
* DOCUMENT ME!
|
333 |
*
|
334 |
* @param array DOCUMENT ME!
|
335 |
*
|
336 |
* @return DOCUMENT ME!
|
337 |
*/
|
338 |
public static String floatArray2String(float[] array) { |
339 |
String aux = "" + array[0]; |
340 |
|
341 |
for (int i = 1; i < array.length; i++) { |
342 |
aux += ("," + array[i]);
|
343 |
} |
344 |
|
345 |
return aux;
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* DOCUMENT ME!
|
350 |
*
|
351 |
* @param array DOCUMENT ME!
|
352 |
*
|
353 |
* @return DOCUMENT ME!
|
354 |
*/
|
355 |
public static float[] string2FloatArray(String array) { |
356 |
String[] floats = new String[4]; |
357 |
floats = array.split(",");
|
358 |
|
359 |
float[] ret = new float[floats.length]; |
360 |
|
361 |
for (int i = 0; i < ret.length; i++) { |
362 |
ret[i] = new Float(floats[i]).floatValue(); |
363 |
} |
364 |
|
365 |
return ret;
|
366 |
} |
367 |
|
368 |
/**
|
369 |
* returns a list (comma-separated) in one unique string
|
370 |
*
|
371 |
*
|
372 |
* @return
|
373 |
* @param input
|
374 |
*/
|
375 |
public static String getComaSeparated(String[] input) { |
376 |
return getXSeparated("," , input); |
377 |
} |
378 |
|
379 |
/**
|
380 |
* returns a list (blanck-separated) in one unique string
|
381 |
*
|
382 |
*
|
383 |
* @return
|
384 |
* @param input
|
385 |
*/
|
386 |
public static String getBlankSeparated(String[] input) { |
387 |
return getXSeparated(" " , input); |
388 |
} |
389 |
|
390 |
/**
|
391 |
* returns a list (X-Character-separated) in one unique string
|
392 |
*
|
393 |
*
|
394 |
* @return
|
395 |
* @param X
|
396 |
* @param input
|
397 |
*/
|
398 |
private static String getXSeparated(String X, String[] input) { |
399 |
String output = ""; |
400 |
if (input.length == 0) { |
401 |
return ""; |
402 |
} |
403 |
output = input[0];
|
404 |
for (int i = 1; i < input.length; i++) |
405 |
output = output + X + input[i]; |
406 |
return output;
|
407 |
} |
408 |
|
409 |
/**
|
410 |
* Replace a part of a String
|
411 |
*
|
412 |
*
|
413 |
* @return
|
414 |
* @param str String to find the pattern
|
415 |
* @param pattern Pattern to find
|
416 |
* @param replace String to replace
|
417 |
*/
|
418 |
public static String replace(String str, String pattern, String replace) { |
419 |
int s = 0; |
420 |
int e = 0; |
421 |
StringBuffer result = new StringBuffer(); |
422 |
while ((e = str.indexOf(pattern, s)) >= 0) { |
423 |
result.append(str.substring(s, e)); |
424 |
result.append(replace); |
425 |
s = e + pattern.length(); |
426 |
} |
427 |
result.append(str.substring(s)); |
428 |
return result.toString();
|
429 |
} |
430 |
|
431 |
/**
|
432 |
* Generates one random string
|
433 |
* @param length
|
434 |
* String length
|
435 |
* @return
|
436 |
*/
|
437 |
public static String generateRandomString(int length) { |
438 |
int rnd;
|
439 |
final Random r = new Random(); |
440 |
final String sorigen = |
441 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
442 |
"abcdefghijklmnopqrstuvwxyz";
|
443 |
final char fuente[] = sorigen.toCharArray(); |
444 |
char buffer[] = new char[length]; |
445 |
|
446 |
for( int i=0; i < length; i++ ) { |
447 |
rnd = Math.abs( r.nextInt() ) % fuente.length;
|
448 |
buffer[i] = fuente[rnd]; |
449 |
} |
450 |
return( new String( buffer ) ); |
451 |
} |
452 |
|
453 |
/**
|
454 |
* Returns the number of occurrences of a chain of characters in an String
|
455 |
*
|
456 |
* @param str The string where find into
|
457 |
* @param subStr The chain of characters that we want to know how many times occurs
|
458 |
* @param caseSensitive <code>true</code> if has to do case sensitive each search or <code>false</code> if not
|
459 |
*
|
460 |
* @return An integer value >= 0
|
461 |
*
|
462 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
463 |
*/
|
464 |
public static int numberOfOccurrencesOfSubStringInString(String str, String subStr, boolean caseSensitive) { |
465 |
int aux = 0; |
466 |
int numberOfOccurrences = 0; |
467 |
|
468 |
if (!caseSensitive) {
|
469 |
str = str.toLowerCase(); |
470 |
subStr = subStr.toLowerCase(); |
471 |
} |
472 |
|
473 |
while (((aux = str.indexOf(subStr, aux)) != -1)) { |
474 |
aux = aux + subStr.length(); |
475 |
numberOfOccurrences++; |
476 |
} |
477 |
|
478 |
return numberOfOccurrences;
|
479 |
} |
480 |
|
481 |
|
482 |
/**
|
483 |
* Returns the number of occurrences of a chain of characters in an String, between a rank of positions
|
484 |
* (If the subString finishes before 'end_position' -> don't count that occurrence)
|
485 |
*
|
486 |
* @param str The string where find into
|
487 |
* @param subStr The chain of characters that we want to know how many times occurs
|
488 |
* @param caseSensitive <code>true<code> if has to do case sensitive at each search or <code>false</code> if not
|
489 |
* @param start_position the initial position used by the search
|
490 |
* @param end_position the final position used by the search
|
491 |
*
|
492 |
* @return An integer value >= 0
|
493 |
*
|
494 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
495 |
*/
|
496 |
public static int numberOfOccurrencesOfSubStringInStringBetweenPositions(String str, String subStr, boolean caseSensitive, int start_position, int end_position) { |
497 |
int aux = start_position;
|
498 |
int numberOfOccurrences = 0; |
499 |
boolean finish = false; |
500 |
|
501 |
if (!caseSensitive) {
|
502 |
str = str.toLowerCase(); |
503 |
subStr = subStr.toLowerCase(); |
504 |
} |
505 |
|
506 |
while ((!finish) && ((aux = str.indexOf(subStr, aux)) != -1)) { |
507 |
aux = aux + subStr.length(); |
508 |
|
509 |
if (aux > end_position)
|
510 |
finish = true;
|
511 |
else
|
512 |
numberOfOccurrences++; |
513 |
} |
514 |
|
515 |
return numberOfOccurrences;
|
516 |
} |
517 |
} |