1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="UnicodeFormatter.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class UnicodeFormatter {
26  
27      public static String byteToHex(byte b) {
28          char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
29  
30          return new String(array);
31      }
32  
33      public static String charToHex(char c) {
34          byte hi = (byte)(c >>> 8);
35          byte lo = (byte)(c & 0xff);
36  
37          char[] array = {
38              _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
39              _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
40          };
41  
42          return new String(array);
43      }
44  
45      public static String parseString(String hexString) {
46          StringBuilder sb = new StringBuilder();
47  
48          char[] array = hexString.toCharArray();
49  
50          if ((array.length % 6) != 0) {
51              _log.error("String is not in hex format");
52  
53              return hexString;
54          }
55  
56          for (int i = 2; i < hexString.length(); i = i + 6) {
57              String s = hexString.substring(i, i + 4);
58  
59              try {
60                  char c = (char)Integer.parseInt(s, 16);
61  
62                  sb.append(c);
63              }
64              catch (Exception e) {
65                  _log.error(e, e);
66  
67                  return hexString;
68              }
69          }
70  
71          return sb.toString();
72      }
73  
74      public static String toString(char[] array) {
75          StringBuilder sb = new StringBuilder(array.length * 6);
76  
77          char[] hexes = new char[4];
78  
79          for (int i = 0; i < array.length; i++) {
80              sb.append(_UNICODE_PREFIX);
81              sb.append(_charToHex(array[i], hexes));
82          }
83  
84          return sb.toString();
85      }
86  
87      public static String toString(String s) {
88          if (s == null) {
89              return null;
90          }
91  
92          StringBuilder sb = new StringBuilder(s.length() * 6);
93  
94          char[] hexes = new char[4];
95  
96          for (int i = 0; i < s.length(); i++) {
97              sb.append(_UNICODE_PREFIX);
98              sb.append(_charToHex(s.charAt(i), hexes));
99          }
100 
101         return sb.toString();
102     }
103 
104     private static char[] _charToHex(char c, char[] hexes) {
105         byte hi = (byte)(c >>> 8);
106         byte lo = (byte)(c & 0xff);
107 
108         hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
109         hexes[1] = _HEX_DIGITS[hi & 0x0f];
110         hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
111         hexes[3] = _HEX_DIGITS[lo & 0x0f];
112 
113         return hexes;
114     }
115 
116     private static final char[] _HEX_DIGITS = {
117         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
118         'e', 'f'
119     };
120 
121     private static final String _UNICODE_PREFIX = "\\u";
122 
123     private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
124 
125 }