001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: /**
027: * <a href="UnicodeFormatter.java.html"><b><i>View Source</i></b></a>
028: *
029: * @author Brian Wing Shun Chan
030: *
031: */
032: public class UnicodeFormatter {
033:
034: public static char HEX_DIGIT[] = { '0', '1', '2', '3', '4', '5',
035: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
036:
037: public static String byteToHex(byte b) {
038: char[] array = { HEX_DIGIT[(b >> 4) & 0x0f],
039: HEX_DIGIT[b & 0x0f] };
040:
041: return new String(array);
042: }
043:
044: public static String charToHex(char c) {
045: byte hi = (byte) (c >>> 8);
046: byte lo = (byte) (c & 0xff);
047:
048: return byteToHex(hi) + byteToHex(lo);
049: }
050:
051: public static String parseString(String hexString) {
052: StringMaker sm = new StringMaker();
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: sm.append(c);
069: } catch (Exception e) {
070: _log.error(e, e);
071:
072: return hexString;
073: }
074: }
075:
076: return sm.toString();
077: }
078:
079: public static String toString(char[] array) {
080: StringMaker sm = new StringMaker();
081:
082: for (int i = 0; i < array.length; i++) {
083: sm.append("\\u");
084: sm.append(charToHex(array[i]));
085: }
086:
087: return sm.toString();
088: }
089:
090: public static String toString(String s) {
091: if (s == null) {
092: return null;
093: }
094:
095: return toString(s.toCharArray());
096: }
097:
098: private static Log _log = LogFactoryUtil
099: .getLog(UnicodeFormatter.class);
100:
101: }
|