KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > util > UnicodeEncoder


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html.util;
17
18
19
20
21 /**
22  * Converts Strings so that they can be used within HTML-Code.
23  */

24 public abstract class UnicodeEncoder
25 {
26     /**
27      * Variant of {@link #encode} where encodeNewline is false and encodeNbsp is true.
28      */

29     public static String JavaDoc encode (String JavaDoc string)
30     {
31         return encode(string, false, true);
32     }
33
34     /**
35      * Variant of {@link #encode} where encodeNbsp is true.
36      */

37     public static String JavaDoc encode (String JavaDoc string, boolean encodeNewline)
38     {
39         return encode(string, encodeNewline, true);
40     }
41
42     /**
43      * Encodes the given string, so that it can be used within a html page.
44      * @param string the string to convert
45      * @param encodeNewline if true newline characters are converted to <br>'s
46      * @param encodeSubsequentBlanksToNbsp if true subsequent blanks are converted to  's
47      */

48     public static String JavaDoc encode (String JavaDoc string,
49                                  boolean encodeNewline,
50                                  boolean encodeSubsequentBlanksToNbsp)
51     {
52         if (string == null)
53         {
54             return "";
55         }
56
57         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(); //create later on demand
58
char c;
59         for (int i = 0; i < string.length (); ++i)
60         {
61             c = string.charAt(i);
62       if (((int)c) >= 0x80)
63       {
64         //encode all non basic latin characters
65
sb.append("&#" + ((int)c) + ";");
66       }
67       else
68       {
69                 sb.append(c);
70       }
71         }
72
73         return sb.toString();
74     }
75
76
77 }
78
Popular Tags