KickJava   Java API By Example, From Geeks To Geeks.

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


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 HTMLEncoder
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 = null; //create later on demand
58
String JavaDoc app;
59         char c;
60         for (int i = 0; i < string.length (); ++i)
61         {
62             app = null;
63             c = string.charAt(i);
64             switch (c)
65             {
66                 case '"': app = "&quot;"; break; //"
67
case '&': app = "&amp;"; break; //&
68
case '<': app = "&lt;"; break; //<
69
case '>': app = "&gt;"; break; //>
70
case ' ':
71                     if (encodeSubsequentBlanksToNbsp &&
72                         (i == 0 || (i - 1 >= 0 && string.charAt(i - 1) == ' ')))
73                     {
74                         //Space at beginning or after another space
75
app = "&#160;";
76                     }
77                     break;
78                 case '\n':
79                     if (encodeNewline)
80                     {
81                         app = "<br/>";
82                     }
83                     break;
84
85                 //german umlauts
86
case '\u00E4' : app = "&auml;"; break;
87                 case '\u00C4' : app = "&Auml;"; break;
88                 case '\u00F6' : app = "&ouml;"; break;
89                 case '\u00D6' : app = "&Ouml;"; break;
90                 case '\u00FC' : app = "&uuml;"; break;
91                 case '\u00DC' : app = "&Uuml;"; break;
92                 case '\u00DF' : app = "&szlig;"; break;
93
94                 //misc
95
//case 0x80: app = "&euro;"; break; sometimes euro symbol is ascii 128, should we suport it?
96
case '\u20AC': app = "&euro;"; break;
97                 case '\u00AB': app = "&laquo;"; break;
98                 case '\u00BB': app = "&raquo;"; break;
99                 case '\u00A0': app = "&#160;"; break;
100
101                 default:
102                     if (((int)c) >= 0x80)
103                     {
104                         //encode all non basic latin characters
105
app = "&#" + ((int)c) + ";";
106                     }
107                     break;
108             }
109             if (app != null)
110             {
111                 if (sb == null)
112                 {
113                     sb = new StringBuffer JavaDoc(string.substring(0, i));
114                 }
115                 sb.append(app);
116             } else {
117                 if (sb != null)
118                 {
119                     sb.append(c);
120                 }
121             }
122         }
123
124         if (sb == null)
125         {
126             return string;
127         }
128         else
129         {
130             return sb.toString();
131         }
132     }
133
134
135 }
136
Popular Tags