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