KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > xml > TextUtils


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.xml;
19
20
21 /**
22  * @author fdietz
23  */

24 public class TextUtils {
25     public static String JavaDoc escapeText(String JavaDoc txt) {
26         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(txt);
27
28         // Important!!
29
// -> replacing this character must happen first
30
stringReplaceAll(buffer, '&', "&");
31
32         stringReplaceAll(buffer, '<', "&lt;");
33         stringReplaceAll(buffer, '>', "&gt;");
34         stringReplaceAll(buffer, '"', "&quot;"); // *20030621, karlpeder*
35
stringReplaceAll(buffer, '\'', "&apos;");
36
37         return buffer.toString();
38     }
39
40     public static StringBuffer JavaDoc stringReplaceAll(StringBuffer JavaDoc orig, char token,
41         String JavaDoc replacement) {
42         for (int i = 0; i < orig.length(); i++) {
43             if (orig.charAt(i) == token) {
44                 orig = orig.replace(i, ++i, replacement);
45             }
46         }
47
48         return orig;
49     }
50 }
51
Popular Tags