1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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   * @author Shuyang Zhou
25   */
26  public class UnicodeFormatter {
27  
28      public static String byteToHex(byte b) {
29          char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
30  
31          return new String(array);
32      }
33  
34      public static char[] byteToHex(byte b, char[] hexes) {
35          hexes[0] = _HEX_DIGITS[(b >> 4) & 0x0f];
36          hexes[1] = _HEX_DIGITS[b & 0x0f];
37  
38          return hexes;
39      }
40  
41      public static String charToHex(char c) {
42          byte hi = (byte)(c >>> 8);
43          byte lo = (byte)(c & 0xff);
44  
45          char[] array = {
46              _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
47              _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
48          };
49  
50          return new String(array);
51      }
52  
53      public static String parseString(String hexString) {
54          StringBuilder sb = new StringBuilder();
55  
56          char[] array = hexString.toCharArray();
57  
58          if ((array.length % 6) != 0) {
59              _log.error("String is not in hex format");
60  
61              return hexString;
62          }
63  
64          for (int i = 2; i < hexString.length(); i = i + 6) {
65              String s = hexString.substring(i, i + 4);
66  
67              try {
68                  char c = (char)Integer.parseInt(s, 16);
69  
70                  sb.append(c);
71              }
72              catch (Exception e) {
73                  _log.error(e, e);
74  
75                  return hexString;
76              }
77          }
78  
79          return sb.toString();
80      }
81  
82      public static String toString(char[] array) {
83          StringBuilder sb = new StringBuilder(array.length * 6);
84  
85          char[] hexes = new char[4];
86  
87          for (int i = 0; i < array.length; i++) {
88              sb.append(_UNICODE_PREFIX);
89              sb.append(_charToHex(array[i], hexes));
90          }
91  
92          return sb.toString();
93      }
94  
95      public static String toString(String s) {
96          if (s == null) {
97              return null;
98          }
99  
100         StringBuilder sb = new StringBuilder(s.length() * 6);
101 
102         char[] hexes = new char[4];
103 
104         for (int i = 0; i < s.length(); i++) {
105             sb.append(_UNICODE_PREFIX);
106             sb.append(_charToHex(s.charAt(i), hexes));
107         }
108 
109         return sb.toString();
110     }
111 
112     private static char[] _charToHex(char c, char[] hexes) {
113         byte hi = (byte)(c >>> 8);
114         byte lo = (byte)(c & 0xff);
115 
116         hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
117         hexes[1] = _HEX_DIGITS[hi & 0x0f];
118         hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
119         hexes[3] = _HEX_DIGITS[lo & 0x0f];
120 
121         return hexes;
122     }
123 
124     private static final char[] _HEX_DIGITS = {
125         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
126         'e', 'f'
127     };
128 
129     private static final String _UNICODE_PREFIX = "\\u";
130 
131     private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
132 
133 }