001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Shuyang Zhou
023     */
024    public class UnicodeFormatter {
025    
026            public static String byteToHex(byte b) {
027                    char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
028    
029                    return new String(array);
030            }
031    
032            public static char[] byteToHex(byte b, char[] hexes) {
033                    hexes[0] = _HEX_DIGITS[(b >> 4) & 0x0f];
034                    hexes[1] = _HEX_DIGITS[b & 0x0f];
035    
036                    return hexes;
037            }
038    
039            public static String charToHex(char c) {
040                    byte hi = (byte)(c >>> 8);
041                    byte lo = (byte)(c & 0xff);
042    
043                    char[] array = {
044                            _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
045                            _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
046                    };
047    
048                    return new String(array);
049            }
050    
051            public static String parseString(String hexString) {
052                    StringBuilder sb = new StringBuilder();
053    
054                    char[] array = hexString.toCharArray();
055    
056                    if ((array.length % 6) != 0) {
057                            _log.error("String is not in hex format");
058    
059                            return hexString;
060                    }
061    
062                    for (int i = 2; i < hexString.length(); i = i + 6) {
063                            String s = hexString.substring(i, i + 4);
064    
065                            try {
066                                    char c = (char)Integer.parseInt(s, 16);
067    
068                                    sb.append(c);
069                            }
070                            catch (Exception e) {
071                                    _log.error(e, e);
072    
073                                    return hexString;
074                            }
075                    }
076    
077                    return sb.toString();
078            }
079    
080            public static String toString(char[] array) {
081                    StringBuilder sb = new StringBuilder(array.length * 6);
082    
083                    char[] hexes = new char[4];
084    
085                    for (int i = 0; i < array.length; i++) {
086                            sb.append(_UNICODE_PREFIX);
087                            sb.append(_charToHex(array[i], hexes));
088                    }
089    
090                    return sb.toString();
091            }
092    
093            public static String toString(String s) {
094                    if (s == null) {
095                            return null;
096                    }
097    
098                    StringBuilder sb = new StringBuilder(s.length() * 6);
099    
100                    char[] hexes = new char[4];
101    
102                    for (int i = 0; i < s.length(); i++) {
103                            sb.append(_UNICODE_PREFIX);
104                            sb.append(_charToHex(s.charAt(i), hexes));
105                    }
106    
107                    return sb.toString();
108            }
109    
110            private static char[] _charToHex(char c, char[] hexes) {
111                    byte hi = (byte)(c >>> 8);
112                    byte lo = (byte)(c & 0xff);
113    
114                    hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
115                    hexes[1] = _HEX_DIGITS[hi & 0x0f];
116                    hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
117                    hexes[3] = _HEX_DIGITS[lo & 0x0f];
118    
119                    return hexes;
120            }
121    
122            private static final char[] _HEX_DIGITS = {
123                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
124                    'e', 'f'
125            };
126    
127            private static final String _UNICODE_PREFIX = "\\u";
128    
129            private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
130    
131    }