001: /*
002: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/TKUppercaseConverter.java,v 1.5 2000/05/22 15:01:22 careck Exp $
003: *
004: */
005: package com.teamkonzept.lib;
006:
007: import java.util.*;
008:
009: /**
010: Konvertierungsklasse fuer HTML-Text-Codierung
011: */
012: public class TKUppercaseConverter extends TKConverter {
013: public final static String CONV_ID = "UPPERCASE";
014: public final static String CONV_NAME = "UPPERCASE-ISO-8859_1";
015:
016: public String getName() {
017: return CONV_NAME;
018: }
019:
020: public final static String[] HTML_CODES = {
021: /* 0*/null, null, null, null, null, null, null, null, null, null,
022: /* 10*/null, null, null, null, null, null, null, null, null, null,
023: /* 20*/null, null, null, null, null, null, null, null, null, null,
024: /* 30*/null, null, null, null, "quot", null, null, null, "amp",
025: null,
026: /* 40*/null, null, null, null, null, null, null, null,
027: null, null,
028: /* 50*/null, null, null, null, null, null, null, null,
029: null, null,
030: /* 60*/"lt", null, "gt", null, null, null, null, null,
031: null, null,
032: /* 70*/null, null, null, null, null, null, null, null,
033: null, null,
034: /* 80*/null, null, null, null, null, null, null, null,
035: null, null,
036: /* 90*/null, null, null, null, null, null, null, "*A",
037: "*B", "*C",
038: /*100*/"*D", "*E", "*F", "*G", "*H", "*I", "*J", "*K",
039: "*L", "*M",
040: /*110*/"*N", "*O", "*P", "*Q", "*R", "*S", "*T", "*U",
041: "*V", "*W",
042: /*120*/"*X", "*Y", "*Z", null, null, null, null, null,
043: null, null,
044: /*130*/null, null, /*"quot"*/null, null, null, null,
045: null, null, null, null,
046: /*140*/null, null, null, null, null, /*"#39"*/null, /*"#39*/
047: null, null, null, null,
048: /*150*/null, null, null, null, null, null, null, null,
049: null, null,
050: /*160*/"nbsp", null, null, null, null, null, null, null,
051: null, null,
052: /*170*/null, null, null, "shy", null, null, null, null,
053: null, null,
054: /*180*/null, null, null, null, null, null, null, null,
055: null, null,
056: /*190*/null, null, "Agrave", "Aacute", "Acirc", "Atilde",
057: "Auml", "Aring", "AElig", "Ccedil",
058: /*200*/"Egrave", "Eacute", "Ecirc", "Euml", "Igrave",
059: "Iacute", "Icirc", "Iuml", "ETH", "Ntilde",
060: /*210*/"Ograve", "Oacute", "Ocirc", "Otilde", "Ouml",
061: null, "Oslash", "Ugrave", "Uacute", "Ucirc",
062: /*220*/"Uuml", "Yacute", "THORN", "*SS", "Agrave",
063: "Aacute", "Acirc", "Atilde", "Auml", "Aring",
064: /*230*/"Aelig", "Ccedil", "Egrave", "Eacute", "Ecirc",
065: "Euml", "Igrave", "Iacute", "Icirc", "Iuml",
066: /*240*/"ETH", "Ntilde", "Ograve", "Oacute", "Ocirc",
067: "Otilde", "Ouml", null, "Oslash", "Ugrave",
068: /*250*/"Uacute", "Ucirc", "Uuml", "Yacute", "THORN", "*Y" };
069:
070: public final static byte AMPERCENT = (byte) '&';
071: public final static byte HASH = (byte) '#';
072: public final static byte SEMICOLON = (byte) ';';
073: public static Hashtable NAME_HASH = null;
074:
075: public int getMaxBytesPerChar() {
076: return 8;
077: }
078:
079: public int minCharSize(int byteCount) {
080: return byteCount;
081: }
082:
083: public synchronized Hashtable getNameHash() {
084: if (NAME_HASH != null)
085: return NAME_HASH;
086: NAME_HASH = new Hashtable(38);
087: String html;
088: for (int i = 0; i <= 255; i++) {
089: if ((html = HTML_CODES[i]) != null && html.charAt(0) != '*')
090: NAME_HASH.put(html, new Integer(i));
091: }
092: return NAME_HASH;
093: }
094:
095: public int charsToBytes(char src[], byte dst[], int srcBegin,
096: int length, int dstBegin) {
097: int lastPos = srcBegin + length;
098: int firstPos = dstBegin;
099: for (int i = srcBegin; i < lastPos; i++) {
100: char c = src[i];
101: byte b = (byte) c;
102: int code = (int) c;
103: String subst = HTML_CODES[code];
104: if (subst == null) {
105: //if( c >= ' ' && c <='\u0080' ) {
106: if (c <= '\u0080') {
107: dst[dstBegin++] = b;
108: } else if (c < '\u0100') {
109: dst[dstBegin++] = AMPERCENT;
110: dst[dstBegin++] = HASH;
111: dst[dstBegin++] = (byte) Character.forDigit(
112: (code / 100), 10);
113: dst[dstBegin++] = (byte) Character.forDigit(
114: (code % 100 / 10), 10);
115: dst[dstBegin++] = (byte) Character.forDigit(
116: (code % 10), 10);
117: dst[dstBegin++] = SEMICOLON;
118: }
119: } else if (subst.length() > 0 && subst.charAt(0) == '*') {
120:
121: int len = subst.length();
122: subst.getBytes(1, len, dst, dstBegin);
123: dstBegin += len - 1;
124: } else {
125: dst[dstBegin++] = AMPERCENT;
126: int len = subst.length();
127: subst.getBytes(0, len, dst, dstBegin);
128: dstBegin += len;
129: dst[dstBegin++] = SEMICOLON;
130: }
131: }
132:
133: return dstBegin - firstPos;
134: }
135:
136: public int bytesToChars(byte src[], char dst[], int srcBegin,
137: int length, int dstBegin) {
138: int lastPos = srcBegin + length;
139: int firstPos = dstBegin;
140: int i = srcBegin;
141: Hashtable nameHash = getNameHash();
142:
143: while (i < lastPos) {
144: byte b = src[i++];
145: if (b == AMPERCENT) {
146: int startPos = i;
147: while (src[i] != SEMICOLON)
148: i++;
149: if (src[i] == HASH) {
150: String decStr = new String(src, 0, startPos + 1, i
151: - startPos - 1);
152: dst[dstBegin++] = (char) Integer.parseInt(decStr);
153: } else {
154: String code = new String(src, 0, startPos, i
155: - startPos);
156: dst[dstBegin++] = (char) ((Integer) nameHash
157: .get(code)).intValue();
158: }
159: i++;
160: } else {
161: dst[dstBegin++] = (char) b;
162: }
163: }
164: return dstBegin - firstPos;
165: }
166:
167: }
|