Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UnicodeEncoder |
|
| 7.0;7 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.shared_impl.renderkit.html.util; | |
20 | ||
21 | /** | |
22 | * Converts characters outside of latin-1 set in a string to numeric character references. | |
23 | * | |
24 | */ | |
25 | 0 | public abstract class UnicodeEncoder |
26 | { | |
27 | /** | |
28 | * Encodes the given string, so that it can be used within a html page. | |
29 | * @param string the string to convert | |
30 | */ | |
31 | public static String encode (String string) | |
32 | { | |
33 | 0 | if (string == null) |
34 | { | |
35 | 0 | return ""; |
36 | } | |
37 | ||
38 | 0 | StringBuilder sb = null; |
39 | char c; | |
40 | 0 | for (int i = 0; i < string.length (); ++i) |
41 | { | |
42 | 0 | c = string.charAt(i); |
43 | 0 | if (((int)c) >= 0x80) |
44 | { | |
45 | 0 | if( sb == null ){ |
46 | 0 | sb = new StringBuilder( string.length()+4 ); |
47 | 0 | sb.append( string.substring(0,i) ); |
48 | } | |
49 | //encode all non basic latin characters | |
50 | 0 | sb.append("&#"); |
51 | 0 | sb.append((int)c); |
52 | 0 | sb.append(";"); |
53 | } | |
54 | 0 | else if( sb != null ) |
55 | { | |
56 | 0 | sb.append(c); |
57 | } | |
58 | } | |
59 | ||
60 | 0 | return sb != null ? sb.toString() : string; |
61 | } | |
62 | ||
63 | ||
64 | } |