/*
* Copyright (C) 2003-2011 eXo Platform SAS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.etk.common.utils;
import org.etk.common.logging.Logger;
/**
* Created by The eXo Platform SAS
* Author : eXoPlatform
* thanhvucong.78@google.com
* Aug 4, 2011
*/
public class UnicodeFormatter {
public static String byteToHex(byte b) {
char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]};
return new String(array);
}
public static String charToHex(char c) {
byte hi = (byte)(c >>> 8);
byte lo = (byte)(c & 0xff);
char[] array = {
_HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f],
_HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f]
};
return new String(array);
}
public static String parseString(String hexString) {
StringBuilder sb = new StringBuilder();
char[] array = hexString.toCharArray();
if ((array.length % 6) != 0) {
_log.error("String is not in hex format");
return hexString;
}
for (int i = 2; i < hexString.length(); i = i + 6) {
String s = hexString.substring(i, i + 4);
try {
char c = (char)Integer.parseInt(s, 16);
sb.append(c);
}
catch (Exception e) {
_log.error(e.getMessage(), e);
return hexString;
}
}
return sb.toString();
}
public static String toString(char[] array) {
StringBuilder sb = new StringBuilder(array.length * 6);
char[] hexes = new char[4];
for (int i = 0; i < array.length; i++) {
sb.append(_UNICODE_PREFIX);
sb.append(_charToHex(array[i], hexes));
}
return sb.toString();
}
public static String toString(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder(s.length() * 6);
char[] hexes = new char[4];
for (int i = 0; i < s.length(); i++) {
sb.append(_UNICODE_PREFIX);
sb.append(_charToHex(s.charAt(i), hexes));
}
return sb.toString();
}
private static char[] _charToHex(char c, char[] hexes) {
byte hi = (byte)(c >>> 8);
byte lo = (byte)(c & 0xff);
hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f];
hexes[1] = _HEX_DIGITS[hi & 0x0f];
hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f];
hexes[3] = _HEX_DIGITS[lo & 0x0f];
return hexes;
}
private static final char[] _HEX_DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'
};
private static final String _UNICODE_PREFIX = "\\u";
private static Logger _log = Logger.getLogger(UnicodeFormatter.class);
}