KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > StringUtil


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
68
69 import java.io.PrintWriter JavaDoc;
70 import java.io.StringWriter JavaDoc;
71 import java.math.BigDecimal JavaDoc;
72 import java.math.BigInteger JavaDoc;
73 import java.util.Hashtable JavaDoc;
74 import java.util.List JavaDoc;
75 import java.util.Random JavaDoc;
76 import java.util.StringTokenizer JavaDoc;
77 import java.util.Vector JavaDoc;
78
79
80 /**
81  * This is class that consists of static methods, utilities for
82  * manipulations of <code>java.lang.String</code>s.
83  *
84  * @author Michael Nash, Peter Pilgrim, Henri Yves AMAIZO
85  */

86 public final class StringUtil {
87     public static final String JavaDoc EMPTY_STRING = "";
88
89
90     /**
91      * Make sure a string is not null. This is a convenience method, but it
92      * is not necessarily recommended if you do a lot of processing for theMessage
93      * parameter. It is recommended that you assertNotBlank, catch IllegalArgumentException
94      * and then throw an appropriate exception with your message.
95      *
96      * @param theString Any string, possibly null
97      * @param theMessage The error message to use if the argument is not null.
98      */

99     public static final void assertNotBlank(String JavaDoc theString, String JavaDoc theMessage) {
100         if (theString == null) {
101             throw new IllegalArgumentException JavaDoc("Null argument not allowed: " +
102                     theMessage);
103         }
104         if (theString.trim().equals(EMPTY_STRING)) {
105             throw new IllegalArgumentException JavaDoc("Blank argument not allowed: " +
106                     theMessage);
107         }
108     } /* assertNotBlank(String, String) */
109
110     /**
111      * Make sure a string is not null.
112      *
113      * @param theString Any string, possibly null
114      * @return An empty string if the original was null, else the original
115      */

116     public static final String JavaDoc notNull(String JavaDoc theString) {
117         if (theString == null) {
118             return EMPTY_STRING;
119         }
120
121         return theString;
122     } /* notNull(String) */
123
124
125     /**
126      * Throws an exception if theString can't be mapped to a boolean value.
127      *
128      * @param theString the string to check
129      * @param theMessage the message to have in the IllegalArgumentException
130      * if the conditions aren't met.
131      */

132     public static final void assertBoolean(String JavaDoc theString, String JavaDoc theMessage) {
133         assertNotBlank(theString, theMessage);
134
135         if (!(theString.equalsIgnoreCase("yes") ||
136                 theString.equalsIgnoreCase("true") ||
137                 theString.equalsIgnoreCase("no") ||
138                 theString.equalsIgnoreCase("false") ||
139                 theString.equals("1") ||
140                 theString.equals("0") ||
141                 theString.equalsIgnoreCase("y") ||
142                 theString.equalsIgnoreCase("n"))) {
143             throw new IllegalArgumentException JavaDoc(theMessage);
144         }
145     }
146
147     /**
148      * parse a boolean value
149      *
150      * @param theString the string to parse
151      * @return a boolean value parse from input; if input cannot be parsed, return false
152      */

153     public static final boolean toBoolean(String JavaDoc theString) {
154         boolean result = false;
155         if (theString != null) {
156             // Rearranged with commons conditions first
157
result = theString.equalsIgnoreCase("true")
158                     || theString.equals("1")
159                     || theString.equalsIgnoreCase("yes")
160                     || theString.equalsIgnoreCase("y")
161                     || theString.equalsIgnoreCase("on")
162                     || theString.equalsIgnoreCase("set");
163         }
164
165         return result;
166     }
167
168     /**
169      * Same thing but using a serializable string as the parameter instead
170      *
171      * @param theString The string to check against null
172      * @return theString or an empty string if it is null
173      */

174     public static String JavaDoc notNull(SerializableString theString) {
175         if (theString == null) {
176             return EMPTY_STRING;
177         }
178
179         return theString.toString();
180     }
181
182     /**
183      * This method is useful for creating lists that use letters instead of
184      * numbers, such as a, b, c, d...instead of 1, 2, 3, 4.
185      * Valid numbers are from 1 to 26, corresponding to the 26 letters of the alphabet.
186      * By default, the letter is returned as a lowercase, but if the boolean
187      * upperCaseFlag is true, the letter will be returned as an uppercase.
188      * Creation date: (5/11/00 12:52:23 PM)
189      * author: Adam Rossi
190      *
191      * @param number The number to convert
192      * @param upperCaseFlag True if you want the final data to be uppercase
193      * @return java.lang.String
194      */

195     public static String JavaDoc numberToLetter(int number, boolean upperCaseFlag)
196             throws IllegalArgumentException JavaDoc {
197
198         //add nine to bring the numbers into the right range (in java, a= 10, z = 35)
199
if (number < 1 || number > 26) {
200             throw new IllegalArgumentException JavaDoc("The number is out of the proper range (1 to " +
201                     "26) to be converted to a letter.");
202         }
203
204         int modnumber = number + 9;
205         char thechar = Character.forDigit(modnumber, 36);
206
207         if (upperCaseFlag) {
208             thechar = Character.toUpperCase(thechar);
209         }
210
211         return "" + thechar;
212     } /* numberToLetter(int, boolean) */
213
214
215     /**
216      * replace substrings within string.
217      *
218      * @param s The string to work with
219      * @param sub The string to substitude the occurances of
220      * @param with The string to replace with
221      * @return A processed java.util.String
222      */

223     public static String JavaDoc replace(String JavaDoc s, String JavaDoc sub, String JavaDoc with) {
224         if (s == null) {
225             return null;
226         }
227         int c = 0;
228         int i = s.indexOf(sub, c);
229
230         if (i == -1) {
231             return s;
232         }
233
234         FastStringBuffer buf = new FastStringBuffer(s.length() + with.length());
235
236         do {
237             buf.append(s.substring(c, i));
238             buf.append(with);
239             c = i + sub.length();
240         } while ((i = s.indexOf(sub, c)) != -1);
241
242         if (c < s.length()) {
243             buf.append(s.substring(c, s.length()));
244         }
245
246         return buf.toString();
247     } /* replace(String, String, String) */
248
249     /**
250      * Formats the string to an XML/XHTML escaped character. Useful for &'s, etc
251      *
252      * @param s the String to format
253      * @return The escaped formatted String.
254      * @see org.apache.xml.serialize.BaseMarkupSerializer for example of original
255      * code.
256      */

257     public static String JavaDoc xmlEscape(String JavaDoc s) {
258         int length = s.length();
259         FastStringBuffer fsb = new FastStringBuffer(length);
260
261         for (int i = 0; i < length; i++) {
262             fsb = printEscaped(s.charAt(i), fsb);
263         }
264
265         return fsb.toString();
266     }
267
268     /**
269      * Formats a particular character to something workable in xml Helper to xmlEscape()
270      *
271      * @param ch the character to print.
272      * @param fsb The FastStringBuffer to add this to.
273      * @return a FastStringBuffer that is modified
274      */

275     protected static FastStringBuffer printEscaped(char ch,
276                                                    FastStringBuffer fsb) {
277         String JavaDoc charRef;
278
279         // If there is a suitable entity reference for this
280
// character, print it. The list of available entity
281
// references is almost but not identical between
282
// XML and HTML.
283
charRef = getEntityRef(ch);
284
285         if (charRef != null) {
286             fsb.append('&');
287             fsb.append(charRef);
288             fsb.append(';');
289
290             //ch<0xFF == isPrintable()
291
} else if ((ch >= ' ' && ch < 0xFF && ch != 0xF7) || ch == '\n' ||
292                 ch == '\r' || ch == '\t') {
293
294             // If the character is not printable, print as character reference.
295
// Non printables are below ASCII space but not tab or line
296
// terminator, ASCII delete, or above a certain Unicode threshold.
297
if (ch < 0x10000) {
298                 fsb.append(ch);
299             } else {
300                 fsb.append((char) ((((int) ch - 0x10000) >> 10) + 0xd800));
301                 fsb.append((char) ((((int) ch - 0x10000) & 0x3ff) + 0xdc00));
302             }
303         } else {
304             fsb.append("&#x");
305             fsb.append(Integer.toHexString(ch));
306             fsb.append(';');
307         }
308
309         return fsb;
310     }
311
312     /**
313      * Helper to xmlEscape()
314      *
315      * @param ch the character to escape
316      * @return A modified string representing the tanlation of the character
317      * or null if there is no translation for it.
318      */

319     protected static String JavaDoc getEntityRef(int ch) {
320
321         // Encode special XML characters into the equivalent character references.
322
// These five are defined by default for all XML documents.
323
switch (ch) {
324             case '<':
325                 return "lt";
326
327             case '>':
328                 return "gt";
329
330             case '"':
331                 return "quot";
332
333             case '\'':
334                 return "apos";
335
336             case '&':
337                 return "amp";
338         }
339
340         return null;
341     }
342
343     /**
344      * HTML code for ellipses (3 dots, like '...' as one character)
345      * used for appending to a truncate() line if necessary
346      */

347     public static final String JavaDoc ELLIPSES = "&#133";
348
349     /**
350      * truncate a string at the given length if necessary,
351      * adding an ellipses at the end if truncation occurred;
352      * uses ELLIPSES static String from this class
353      *
354      * @param str The string to process
355      * @param len The maximum length to process the string to.
356      * @return the appropriately trimmed string.
357      */

358     public static String JavaDoc truncate(String JavaDoc str, int len) {
359         String JavaDoc result = str;
360         if (str.length() > len) {
361             result = str.substring(0, len) + ELLIPSES;
362         }
363         return result;
364     }
365
366
367     /**
368      * Map accent characters with equivalent without accent.
369      * <p/>
370      * author Henri Yves AMAIZO
371      *
372      * @return Hashtable Character mapping table.
373      */

374     public static Hashtable JavaDoc characterMap() {
375         Hashtable JavaDoc characterMap = new Hashtable JavaDoc();
376
377         characterMap.put(new Character JavaDoc('à'), new Character JavaDoc('a'));
378         characterMap.put(new Character JavaDoc('â'), new Character JavaDoc('a'));
379         characterMap.put(new Character JavaDoc('ä'), new Character JavaDoc('a'));
380         characterMap.put(new Character JavaDoc('á'), new Character JavaDoc('a'));
381         characterMap.put(new Character JavaDoc('À'), new Character JavaDoc('A'));
382         characterMap.put(new Character JavaDoc('Á'), new Character JavaDoc('A'));
383         characterMap.put(new Character JavaDoc('Â'), new Character JavaDoc('A'));
384         characterMap.put(new Character JavaDoc('Ä'), new Character JavaDoc('A'));
385         characterMap.put(new Character JavaDoc('è'), new Character JavaDoc('e'));
386         characterMap.put(new Character JavaDoc('é'), new Character JavaDoc('e'));
387         characterMap.put(new Character JavaDoc('ê'), new Character JavaDoc('e'));
388         characterMap.put(new Character JavaDoc('ë'), new Character JavaDoc('e'));
389         characterMap.put(new Character JavaDoc('È'), new Character JavaDoc('E'));
390         characterMap.put(new Character JavaDoc('É'), new Character JavaDoc('E'));
391         characterMap.put(new Character JavaDoc('Ê'), new Character JavaDoc('E'));
392         characterMap.put(new Character JavaDoc('Ë'), new Character JavaDoc('E'));
393         characterMap.put(new Character JavaDoc('î'), new Character JavaDoc('i'));
394         characterMap.put(new Character JavaDoc('ï'), new Character JavaDoc('i'));
395         characterMap.put(new Character JavaDoc('Î'), new Character JavaDoc('I'));
396         characterMap.put(new Character JavaDoc('Ï'), new Character JavaDoc('I'));
397         characterMap.put(new Character JavaDoc('ô'), new Character JavaDoc('o'));
398         characterMap.put(new Character JavaDoc('ö'), new Character JavaDoc('o'));
399         characterMap.put(new Character JavaDoc('Ô'), new Character JavaDoc('O'));
400         characterMap.put(new Character JavaDoc('Ö'), new Character JavaDoc('O'));
401         characterMap.put(new Character JavaDoc('û'), new Character JavaDoc('u'));
402         characterMap.put(new Character JavaDoc('ü'), new Character JavaDoc('u'));
403         characterMap.put(new Character JavaDoc('Û'), new Character JavaDoc('U'));
404         characterMap.put(new Character JavaDoc('Ü'), new Character JavaDoc('U'));
405         characterMap.put(new Character JavaDoc('ç'), new Character JavaDoc('c'));
406         characterMap.put(new Character JavaDoc('Ç'), new Character JavaDoc('C'));
407
408         return characterMap;
409     }
410
411     /**
412      * Remove from the parameter the accent characters and
413      * return the remain string or null if empty
414      * <p/>
415      * author Henri Yves AMAIZO
416      *
417      * @param s the string to remove the accent characters
418      * @return String
419      */

420     public static String JavaDoc removeAccents(String JavaDoc s) {
421         String JavaDoc out = null;
422         if (s != null) {
423             Hashtable JavaDoc charRemove = characterMap();
424             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
425             for (int i = 0; i < s.length(); i++) {
426                 Character JavaDoc c = new Character JavaDoc(s.charAt(i));
427                 if (charRemove.containsKey(c)) {
428                     c = (Character JavaDoc) charRemove.get(c);
429                 }
430                 sb.append(c.charValue());
431             }
432             out = sb.toString();
433         }
434         return out;
435     }
436
437
438     /**
439      * Replace all comma by dot
440      * <p/>
441      * author Henri Yves AMAIZO
442      *
443      * @param str String to change
444      * @return String The result of substitution
445      */

446     public static String JavaDoc convertCommaToDot(String JavaDoc str) {
447         if (str == null) {
448             return null;
449         }
450
451         return str.replace(',', '.');
452     }
453
454     /**
455      * Compare 2 decimals string
456      * <p/>
457      * author Henri Yves AMAIZO
458      *
459      * @param decim1 First string to compare
460      * @param decim2 Second string to compare
461      * @return int return 1 if decim1 > decim2<BR> retourne 0 if decim1 == decim2<BR> return -1 if decim1 < decim2
462      */

463     public static int compareDecimals(String JavaDoc decim1, String JavaDoc decim2) {
464         BigDecimal JavaDoc dec1 = new BigDecimal JavaDoc(decim1);
465         BigDecimal JavaDoc dec2 = new BigDecimal JavaDoc(decim2);
466         return dec1.compareTo(dec2);
467     }
468
469     /**
470      * Compare 2 integers string.
471      * <p/>
472      * author Henri Yves AMAIZO
473      *
474      * @param int1 first string to compare
475      * @param int2 second string to compare
476      * @return int return 1 if decim1 > decim2<BR> return 0 if decim1 == decim2<BR> return -1 if decim1 < decim2
477      */

478     public static int compareIntegers(String JavaDoc int1, String JavaDoc int2) {
479         BigInteger JavaDoc dec1 = new BigInteger JavaDoc(int1);
480         BigInteger JavaDoc dec2 = new BigInteger JavaDoc(int2);
481         return dec1.compareTo(dec2);
482     }
483
484     /**
485      * Check if string is alphanumeric or not.
486      * <p/>
487      * author Henri Yves AMAIZO
488      *
489      * @param s String to check.
490      * @return boolean true if alphanumeric, false if not.
491      */

492     public static boolean isAlphaNumeric(String JavaDoc s) {
493         return isAlphaNumeric(s, "");
494     }
495
496     /**
497      * Check if string is alphanumeric with addons chararcters or not.
498      * <p/>
499      * author Henri Yves AMAIZO
500      *
501      * @param str string to check
502      * @param otherChars extra characters to check with
503      * @return boolean true if parameter string contains only alpha numerics,<BR> plus addons characters and false if not.
504      */

505     public static boolean isAlphaNumeric(String JavaDoc str, String JavaDoc otherChars) {
506         String JavaDoc alphaNum =
507                 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + otherChars;
508         for (int i = 0; i < str.length(); i++) {
509             if (alphaNum.indexOf(str.charAt(i)) == -1) {
510                 return false;
511             }
512         }
513         return true;
514     }
515
516     /**
517      * Check if an input string is an empty string, just contains
518      * whitespace characters, or is <code>null</code>. If it is then
519      * return <code>true</code>
520      *
521      * @param str the input string
522      * @return boolean true if the string is blank or null
523      */

524     public static boolean isBlankOrNull(String JavaDoc str) {
525         if (str == null) {
526             return true;
527         }
528         if (str.trim().length() < 1) {
529             return true;
530         } else {
531             return false;
532         }
533     }
534
535     /**
536      * Check if decimal number
537      * <p/>
538      * author Henri Yves AMAIZO
539      *
540      * @param s string to check.
541      * @return boolean true if the value is decimal number false if not
542      */

543     public static boolean isDecimal(String JavaDoc s) {
544         try {
545             new BigDecimal JavaDoc(s);
546             return true;
547         } catch (NumberFormatException JavaDoc e) {
548             return false;
549         }
550     }
551
552     /**
553      * Check if string pass is a vlaid email address
554      * <p/>
555      * author Henri Yves AMAIZO
556      *
557      * @param email string to check
558      * @return int 0 if valid address, 1 more than 2 tokens (".", "@")<BR> and 2 if the second token is not "." .
559      */

560     public static int isEmail(String JavaDoc email) {
561         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(email, "@");
562
563         if (st.countTokens() != 2) {
564             return 1;
565         }
566
567         st.nextToken();
568         if (st.nextToken().indexOf(".") == -1) {
569             return 2;
570         }
571
572         return 0;
573     }
574
575     /**
576      * Check if integer string
577      * <p/>
578      * author Henri Yves AMAIZO
579      *
580      * @param str string to check
581      * @return boolean true if all characters is digit
582      */

583     public static boolean isInteger(String JavaDoc str) {
584         try {
585             new BigInteger JavaDoc(str);
586             return true;
587         } catch (NumberFormatException JavaDoc e) {
588             return false;
589         }
590     }
591
592     /**
593      * Check if number
594      * author Henri Yves AMAIZO
595      *
596      * @param str to check
597      * @return boolean
598      */

599     public static boolean isNumber(String JavaDoc str) {
600         try {
601             Double.parseDouble(str);
602             return true;
603         } catch (NumberFormatException JavaDoc ex) {
604             return false;
605         }
606     }
607
608     /**
609      * Check French company siren number.
610      * <p/>
611      * author Henri Yves AMAIZO
612      *
613      * @param siren the company number
614      * @return boolean true if ok.
615      */

616     public static boolean isSiren(String JavaDoc siren) {
617         StringBuffer JavaDoc temp = new StringBuffer JavaDoc("");
618         int value = 0;
619
620         // Check if numeric digit.
621
try {
622             Double.parseDouble(siren);
623         } catch (NumberFormatException JavaDoc nbe) {
624             return false;
625         }
626
627         // string length must be 9
628
if (siren.length() != 9) {
629             return false;
630         }
631
632         //
633
for (int i = 0; i < 9; i++) {
634             int n =
635                     Integer.parseInt(siren.substring(i, i + 1)) * (((i % 2) != 0) ? 2 : 1);
636             temp.append(n);
637         }
638
639         // sum of all digits
640
for (int i = 0; i < (temp.length()); i++) {
641             value += Integer.parseInt(temp.substring(i, i + 1));
642         }
643
644         //value must divide by 10
645
if ((value % 10) != 0) {
646             return false;
647         }
648
649         return true;
650     }
651
652     /**
653      * Check French company siret number
654      * <p/>
655      * author Henri Yves AMAIZO
656      *
657      * @param siret The siret number
658      * @return boolean true if equivalent to siret number false if not.
659      */

660     public static boolean isSiret(String JavaDoc siret) {
661         // Check the length
662
if (siret.length() != 14) {
663             return false;
664         }
665
666         //
667
try {
668             Double.parseDouble(siret);
669             return isSiren(siret.substring(0, 9));
670         } catch (NumberFormatException JavaDoc nfe) {
671             return false;
672         }
673     }
674
675     /**
676      * Return empty string if parameter is null
677      * <p/>
678      * author Henri Yves AMAIZO
679      *
680      * @param s the parameter to check
681      * @return String return the parameter if empty or if the string == null
682      */

683     public static String JavaDoc nullToEmpty(String JavaDoc s) {
684         if (s == null) {
685             return "";
686         } else {
687             return s;
688         }
689     }
690
691     /**
692      * Reformat string by converting "," by "."
693      * <p/>
694      * author Henri Yves AMAIZO
695      *
696      * @param str string to reformat
697      * @return String return reformatted string.
698      */

699     public static String JavaDoc reformatDecimalString(String JavaDoc str) {
700         if (str == null) {
701             return null;
702         }
703
704         // replace all ',' by '.'
705
str = str.replace(',', '.');
706         try {
707             Double.parseDouble(str);
708             return str;
709         } catch (NumberFormatException JavaDoc nbe) {
710             return "";
711         }
712     }
713
714     /**
715      * Replace all dots byt comma
716      * <p/>
717      * author Henri Yves AMAIZO
718      *
719      * @param str string to change
720      * @return String reformatted
721      */

722     public static String JavaDoc convertDotToComma(String JavaDoc str) {
723         return str.replace('.', ',');
724     }
725
726     /**
727      * Replace all occurences of key by value in text.
728      * <p/>
729      * author Henri Yves AMAIZO
730      *
731      * @param text string in
732      * @param key occurence to replace
733      * @param value string to use
734      * @return String with the replace value
735      */

736     public static String JavaDoc replaceAll(String JavaDoc text, String JavaDoc key, String JavaDoc value) {
737         if (text == null) {
738             return null;
739         }
740
741         String JavaDoc buffer = text;
742         if (buffer != null && key != null && value != null) {
743             int length = key.length();
744             for (int start = buffer.indexOf(key);
745                  start != -1;
746                  start = buffer.indexOf(key, start + value.length())) {
747                 buffer = buffer.substring(0, start) + value + buffer.substring(start + length);
748             }
749         }
750         return buffer;
751     }
752
753     /**
754      * Substituate once str1 by str2 in text
755      * Commentaire Anglais
756      * <p/>
757      * author Henri Yves AMAIZO
758      *
759      * @param text search and replace in
760      * @param str1 to search for
761      * @param str2 to replace with
762      * @return String replaced
763      */

764     static public String JavaDoc replaceStringOnce(String JavaDoc text, String JavaDoc str1, String JavaDoc str2) {
765         return replaceString(text, str1, str2, 1);
766     }
767
768     /**
769      * Substituate all occurence of str1 by str2 in text
770      * <p/>
771      * author Henri Yves AMAIZO
772      *
773      * @param text search and replace in
774      * @param str1 search for
775      * @param str2 replace with
776      * @return String with all values replaced
777      */

778     static public String JavaDoc replaceString(String JavaDoc text, String JavaDoc str1, String JavaDoc str2) {
779         return replaceString(text, str1, str2, -1);
780     }
781
782     /**
783      * Replace n occurences of str1 in text by str2.
784      * if n = -1 all occurrences are replaced
785      * <p/>
786      * author Henri Yves AMAIZO
787      *
788      * @param text search and replace in
789 <