01: /*
02: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/TKUrlConverter.java,v 1.5.20.1 2002/05/16 09:51:29 alex Exp $
03: *
04: */
05: package com.teamkonzept.lib;
06:
07: /**
08: Konvertierungsklasse fuer URL-Codierung
09: */
10: public class TKUrlConverter extends TKConverter {
11: public final static String CONV_ID = "URL";
12: public final static String CONV_NAME = "URL-ISO-8859_1";
13:
14: public String getName() {
15: return CONV_NAME;
16: }
17:
18: public int getMaxBytesPerChar() {
19: return 3;
20: }
21:
22: public int minCharSize(int byteCount) {
23: return byteCount;
24: }
25:
26: public int charsToBytes(char src[], byte dst[], int srcBegin,
27: int length, int dstBegin) {
28: int lastPos = srcBegin + length;
29: int firstPos = dstBegin;
30: for (int i = srcBegin; i < lastPos; i++) {
31: char c = src[i];
32: if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
33: || (c >= '0' && c <= '9') || c == '@' || c == '*'
34: || c == ':' || c == '-' || c == '_') {
35: dst[dstBegin++] = (byte) c;
36: }
37: /*
38: else if( c == ' ' ) {
39: dst[ dstBegin++ ] = (byte) '+';
40: } */
41: else if (c <= '\u00FF') {
42: int code = (int) c;
43: dst[dstBegin++] = (byte) '%';
44: dst[dstBegin++] = (byte) Character.forDigit(
45: (code / 16), 16);
46: dst[dstBegin++] = (byte) Character.forDigit(
47: (code % 16), 16);
48: }
49: }
50:
51: return dstBegin - firstPos;
52: }
53:
54: final static byte PERCENT = (byte) '%';
55: final static byte PLUS = (byte) '+';
56:
57: public int bytesToChars(byte src[], char dst[], int srcBegin,
58: int length, int dstBegin) {
59: int lastPos = srcBegin + length;
60: int firstPos = dstBegin;
61: for (int i = srcBegin; i < lastPos; i++) {
62: byte b = src[i];
63: if (b == PERCENT) {
64: int high = Character.digit((char) src[++i], 16);
65: int code = 16 * high
66: + Character.digit((char) src[++i], 16);
67: dst[dstBegin++] = (char) code;
68: } else if (b == PLUS) {
69: dst[dstBegin++] = ' ';
70: } else {
71: dst[dstBegin++] = (char) b;
72: }
73: }
74: return dstBegin - firstPos;
75: }
76: }
|