1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  
25  /**
26   * <a href="UnicodeFormatter.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   *
30   */
31  public class UnicodeFormatter {
32  
33      public static char HEX_DIGIT[] = {
34          '0', '1', '2', '3', '4', '5', '6', '7',
35          '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
36      };
37  
38      public static String byteToHex(byte b) {
39          char[] array = {HEX_DIGIT[(b >> 4) & 0x0f], HEX_DIGIT[b & 0x0f]};
40  
41          return new String(array);
42      }
43  
44      public static String charToHex(char c) {
45          byte hi = (byte)(c >>> 8);
46          byte lo = (byte)(c & 0xff);
47  
48          return byteToHex(hi) + byteToHex(lo);
49      }
50  
51      public static String parseString(String hexString) {
52          StringBuilder sb = new StringBuilder();
53  
54          char[] array = hexString.toCharArray();
55  
56          if ((array.length % 6) != 0) {
57              _log.error("String is not in hex format");
58  
59              return hexString;
60          }
61  
62          for (int i = 2; i < hexString.length(); i = i + 6) {
63              String s = hexString.substring(i, i + 4);
64  
65              try {
66                  char c = (char)Integer.parseInt(s, 16);
67  
68                  sb.append(c);
69              }
70              catch (Exception e) {
71                  _log.error(e, e);
72  
73                  return hexString;
74              }
75          }
76  
77          return sb.toString();
78      }
79  
80      public static String toString(char[] array) {
81          StringBuilder sb = new StringBuilder();
82  
83          for (int i = 0; i < array.length; i++) {
84              sb.append("\\u");
85              sb.append(charToHex(array[i]));
86          }
87  
88          return sb.toString();
89      }
90  
91      public static String toString(String s) {
92          if (s == null) {
93              return null;
94          }
95  
96          return toString(s.toCharArray());
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
100 
101 }