01: /*
02: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/TKJavaScriptConverter.java,v 1.2 2000/07/27 10:00:25 alex Exp $
03: *
04: */
05: package com.teamkonzept.lib;
06:
07: /**
08: Konvertierungsklasse fuer ANSI-Codierung
09: */
10: public class TKJavaScriptConverter extends TKConverter {
11:
12: public final static String CONV_ID = "JS";
13: public final static String CONV_NAME = "JAVASCRIPT";
14:
15: public String getName() {
16: return CONV_NAME;
17: }
18:
19: public int getMaxBytesPerChar() {
20: return 4;
21: }
22:
23: public int minCharSize(int byteCount) {
24: return byteCount;
25: }
26:
27: public int charsToBytes(char src[], byte dst[], int srcBegin,
28: int length, int dstBegin) {
29: String back = encodeJavaScript(src, srcBegin, length);
30: back.getBytes(0, back.length(), dst, dstBegin);
31: return back.length();
32: }
33:
34: public int bytesToChars(byte src[], char dst[], int srcBegin,
35: int length, int dstBegin) {
36: String srcIn = new String(src);
37: String back = encodeJavaScript(srcIn.toCharArray(), srcBegin,
38: length);
39: back.getChars(0, back.length(), dst, dstBegin);
40: return back.length();
41: }
42:
43: public static String encodeJavaScript(char[] in, int start,
44: int length) {
45: if (in == null)
46: return null;
47:
48: StringBuffer buf = new StringBuffer();
49: int end = start + length;
50: for (int i = start; i < end; i++) {
51: char ch = in[i];
52: switch (ch) {
53: case '\'':
54: case '\"':
55: case '\\':
56: int eins = conv(ch / 16);
57: int zwei = conv(ch % 16);
58: buf.append("\\x" + new Character((char) eins)
59: + new Character((char) zwei));
60: break;
61: default:
62: if (ch < 32) {
63: eins = conv(ch / 16);
64: zwei = conv(ch % 16);
65: buf.append("\\x" + new Character((char) eins)
66: + new Character((char) zwei));
67: } else
68: buf.append(ch);
69: break;
70: }
71: }
72: return buf.toString();
73: }
74:
75: private static int conv(int in) {
76: if (in >= 10)
77: return 55 + in;
78: else
79: return 48 + in;
80: }
81:
82: }
|