01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.shared_impl.renderkit.html.util;
17:
18: /**
19: * Converts characters outside of latin-1 set in a string to numeric character references.
20: *
21: */
22: public abstract class UnicodeEncoder {
23: /**
24: * Encodes the given string, so that it can be used within a html page.
25: * @param string the string to convert
26: */
27: public static String encode(String string) {
28: if (string == null) {
29: return "";
30: }
31:
32: StringBuffer sb = null;
33: char c;
34: for (int i = 0; i < string.length(); ++i) {
35: c = string.charAt(i);
36: if (((int) c) >= 0x80) {
37: if (sb == null) {
38: sb = new StringBuffer(string.length() + 4);
39: sb.append(string.substring(0, i));
40: }
41: //encode all non basic latin characters
42: sb.append("&#" + ((int) c) + ";");
43: } else if (sb != null) {
44: sb.append(c);
45: }
46: }
47:
48: return sb != null ? sb.toString() : string;
49: }
50:
51: }
|