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