001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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     */
023    public class UnicodeFormatter {
024    
025            public static String byteToHex(byte b) {
026                    char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
027    
028                    return new String(array);
029            }
030    
031            public static String charToHex(char c) {
032                    byte hi = (byte)(c >>> 8);
033                    byte lo = (byte)(c & 0xff);
034    
035                    char[] array = {
036                            _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
037                            _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
038                    };
039    
040                    return new String(array);
041            }
042    
043            public static String parseString(String hexString) {
044                    StringBuilder sb = new StringBuilder();
045    
046                    char[] array = hexString.toCharArray();
047    
048                    if ((array.length % 6) != 0) {
049                            _log.error("String is not in hex format");
050    
051                            return hexString;
052                    }
053    
054                    for (int i = 2; i < hexString.length(); i = i + 6) {
055                            String s = hexString.substring(i, i + 4);
056    
057                            try {
058                                    char c = (char)Integer.parseInt(s, 16);
059    
060                                    sb.append(c);
061                            }
062                            catch (Exception e) {
063                                    _log.error(e, e);
064    
065                                    return hexString;
066                            }
067                    }
068    
069                    return sb.toString();
070            }
071    
072            public static String toString(char[] array) {
073                    StringBuilder sb = new StringBuilder(array.length * 6);
074    
075                    char[] hexes = new char[4];
076    
077                    for (int i = 0; i < array.length; i++) {
078                            sb.append(_UNICODE_PREFIX);
079                            sb.append(_charToHex(array[i], hexes));
080                    }
081    
082                    return sb.toString();
083            }
084    
085            public static String toString(String s) {
086                    if (s == null) {
087                            return null;
088                    }
089    
090                    StringBuilder sb = new StringBuilder(s.length() * 6);
091    
092                    char[] hexes = new char[4];
093    
094                    for (int i = 0; i < s.length(); i++) {
095                            sb.append(_UNICODE_PREFIX);
096                            sb.append(_charToHex(s.charAt(i), hexes));
097                    }
098    
099                    return sb.toString();
100            }
101    
102            private static char[] _charToHex(char c, char[] hexes) {
103                    byte hi = (byte)(c >>> 8);
104                    byte lo = (byte)(c & 0xff);
105    
106                    hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
107                    hexes[1] = _HEX_DIGITS[hi & 0x0f];
108                    hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
109                    hexes[3] = _HEX_DIGITS[lo & 0x0f];
110    
111                    return hexes;
112            }
113    
114            private static final char[] _HEX_DIGITS = {
115                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
116                    'e', 'f'
117            };
118    
119            private static final String _UNICODE_PREFIX = "\\u";
120    
121            private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class);
122    
123    }