001: /*
002: * JavuX - Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.util.code;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.io.UnsupportedEncodingException;
025:
026: public class CodeUrl {
027: public static String toHexString(int i, int len) {
028: String s = "00000000" + Integer.toHexString(i);
029: return s.substring(s.length() - len);
030: }
031:
032: public static byte[] toUTF8Bytes(String s) {
033: try {
034: return s.getBytes("UTF8");
035: } catch (UnsupportedEncodingException e) {
036: }
037: try {
038: return s.getBytes("UTF-8");
039: } catch (UnsupportedEncodingException e) {
040: }
041: return s.getBytes();
042: }
043:
044: public static String toUTF8String(byte[] b) {
045: try {
046: return new String(b, "UTF8");
047: } catch (UnsupportedEncodingException e) {
048: }
049: try {
050: return new String(b, "UTF-8");
051: } catch (UnsupportedEncodingException e) {
052: }
053: return new String(b);
054: }
055:
056: public static String urlDecode(String url) {
057: if (url.indexOf('+') == -1 && url.indexOf('%') == -1)
058: return url;
059: ByteArrayOutputStream s = new ByteArrayOutputStream(url
060: .length());
061: char c;
062: int i, j;
063: for (i = 0; i < url.length(); i++) {
064: c = url.charAt(i);
065: if (c == '+')
066: c = ' ';
067: else if (c == '%') {
068: j = i + 3;
069: if (j > url.length())
070: j = url.length();
071: String hex = url.substring(i + 1, j);
072: i = j - 1;
073: try {
074: c = (char) Integer.parseInt(hex, 16);
075: } catch (NumberFormatException e) {
076: continue;
077: }
078: }
079: s.write(c);
080: }
081: return toUTF8String(s.toByteArray());
082: }
083:
084: public static String urlEncode(String s) {
085: byte[] b = toUTF8Bytes(s);
086: StringBuffer r = new StringBuffer(b.length * 2);
087: int i;
088: char c;
089: for (i = 0; i < b.length; i++) {
090: c = (char) b[i];
091: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
092: || (c >= '0' && c <= '9') || c == '.' || c == '-'
093: || c == '*' || c == '_')
094: r.append(c);
095: else if (c == ' ')
096: r.append('+');
097: else {
098: r.append('%');
099: r.append(toHexString(c, 2));
100: }
101: }
102: return r.toString();
103: }
104: }
|