001: package com.teamkonzept.lib;
002:
003: import java.util.*;
004:
005: /**
006: Basisklasse fuer Konvertierungsklassen zum Umwandeln von
007: Javas Unicode in andere Kodierungen oder zurueck
008: * @author $Author: alex $
009: * @version $Revision: 1.11 $
010: */
011: public abstract class TKConverter {
012: /**
013: Converts a string in this CharacterSet to a Java char string.
014: */
015: public abstract int bytesToChars(byte src[], char dst[],
016: int srcBegin, int length, int dstBegin);
017:
018: /**
019: Converts a string from this CharacterSet to a Java string.
020: */
021: public String bytesToString(byte[] code) {
022: int len = minCharSize(code);
023: char[] newChars = new char[len];
024: int total = bytesToChars(code, newChars, 0, code.length, 0);
025: return new String(newChars, 0, total);
026: }
027:
028: /**
029: Converts a Java char string to this CharacterSet.
030: */
031: public abstract int charsToBytes(char src[], byte dst[],
032: int srcBegin, int length, int dstBegin);
033:
034: /**
035: ask wether the converter want's to do the job itself
036: */
037: public byte[] doYourself(String src) {
038: return null;
039: }
040:
041: /**
042: Returns the boolean result whether this character set is equivalent to another CharacterSet.
043: */
044: public boolean equals(Object converter) {
045: return converter.getClass() == getClass();
046: }
047:
048: public int hashCode() {
049: return getName().hashCode();
050: }
051:
052: /**
053: Returns the maximum number of bytes a character takes in this CharacterSet.
054: */
055: public abstract int getMaxBytesPerChar();
056:
057: /**
058: Returns the Internet Assigned Numbers Authority (IANA) name of this CharacterSet.
059: */
060: public abstract String getName();
061:
062: /**
063: Determines the minimum number of bytes needed to store a string in this CharacterSet.
064: */
065: public int minByteSize(byte code[], TKConverter srcConverter) {
066: return minByteSize(code, 0, code.length, srcConverter);
067: }
068:
069: /**
070: Determines the minimum number of bytes needed to store a string in this CharacterSet.
071: */
072: public int minByteSize(byte bytes[], int begin, int length,
073: TKConverter srcConverter) {
074: return minByteSize(length, srcConverter);
075: }
076:
077: /**
078: Determines the minimum number of bytes needed to store an Java char string in this CharacterSet.
079: */
080: public int minByteSize(char src[]) {
081: return minByteSize(src, 0, src.length);
082: }
083:
084: /**
085: Determines the minimum number of bytes needed to store an Java char string in this CharacterSet.
086: */
087: public int minByteSize(char chars[], int begin, int length) {
088: return minByteSize(length);
089: }
090:
091: /**
092: Determines the minimum number of bytes needed to store a Java char string in this CharacterSet.
093: */
094: public int minByteSize(int charCount) {
095: return charCount * getMaxBytesPerChar();
096: }
097:
098: /**
099: Determines the minimum number of bytes needed to store a string in this CharacterSet.
100: */
101: public int minByteSize(int length, TKConverter srcConverter) {
102: return minByteSize(srcConverter.minCharSize(length));
103: }
104:
105: /**
106: Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
107: */
108: public int minCharSize(byte[] code) {
109: return minCharSize(code, 0, code.length);
110: }
111:
112: /**
113: Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
114: */
115: public int minCharSize(byte[] code, int begin, int length) {
116: return minCharSize(code.length);
117: }
118:
119: /**
120: Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
121: */
122: public abstract int minCharSize(int length);
123:
124: /**
125: Converts a Java string to this CharacterSet.
126: */
127: public byte[] stringToBytes(String src) {
128: byte[] result = doYourself(src);
129: if (result != null)
130: return result;
131:
132: char[] srcArray = src.toCharArray();
133: int tmpSize = minByteSize(srcArray);
134: byte[] temp = new byte[tmpSize];
135: int total = charsToBytes(srcArray, temp, 0, srcArray.length, 0);
136: if (total == tmpSize)
137: return temp;
138:
139: result = new byte[total];
140: System.arraycopy(temp, 0, result, 0, total);
141: temp = null;
142: return result;
143: }
144:
145: protected static TKHashtable converterHash;
146:
147: public static void addConverter(String id, String converterClass) {
148: initialize();
149: synchronized (converterHash) {
150: converterHash.put(id, converterClass);
151: }
152: }
153:
154: public static void addConverter(String[] ids, String converterClass) {
155: initialize();
156: synchronized (converterHash) {
157: for (int i = 0; i < ids.length; i++) {
158: converterHash.put(ids[i], converterClass);
159: }
160: }
161: }
162:
163: public static TKConverter getConverter(String id) {
164: initialize();
165: synchronized (converterHash) {
166: Object entry = converterHash.get(id);
167: if (entry == null)
168: return (TKConverter) entry;
169: if (entry instanceof TKConverter)
170: return (TKConverter) entry;
171: try {
172: String classString = (String) entry;
173: Class convClass = Class.forName(classString);
174: TKConverter conv = (TKConverter) convClass
175: .newInstance();
176: Enumeration e = converterHash.keys();
177: while (e.hasMoreElements()) {
178: String key = (String) e.nextElement();
179: Object val = converterHash.get(key);
180: if ((val instanceof String)
181: && (val.equals(classString))) {
182: converterHash.put(key, conv);
183: }
184: }
185: return conv;
186: } catch (Throwable t) {
187: throw new InstantiationError(t.getMessage());
188: }
189: }
190: }
191:
192: public static Enumeration converterIds() {
193: initialize();
194: synchronized (converterHash) {
195: return converterHash.keys();
196: }
197: }
198:
199: protected static boolean initialized = false;
200:
201: public final static void initialize() {
202: if (initialized)
203: return;
204: initialized = true;
205: converterHash = new TKHashtable();
206:
207: TKConverter.addConverter(TKAnsiConverter.CONV_NAME,
208: "com.teamkonzept.lib.TKAnsiConverter");
209: TKConverter.addConverter(TKAnsiConverter.CONV_ID,
210: "com.teamkonzept.lib.TKAnsiConverter");
211: TKConverter.addConverter(TKUrlConverter.CONV_NAME,
212: "com.teamkonzept.lib.TKUrlConverter");
213: TKConverter.addConverter(TKUrlConverter.CONV_ID,
214: "com.teamkonzept.lib.TKUrlConverter");
215: TKConverter.addConverter(TKHtmlConverter.CONV_NAME,
216: "com.teamkonzept.lib.TKHtmlConverter");
217: TKConverter.addConverter(TKHtmlConverter.CONV_ID,
218: "com.teamkonzept.lib.TKHtmlConverter");
219: TKConverter.addConverter(TKUppercaseConverter.CONV_NAME,
220: "com.teamkonzept.lib.TKUppercaseConverter");
221: TKConverter.addConverter(TKUppercaseConverter.CONV_ID,
222: "com.teamkonzept.lib.TKUppercaseConverter");
223: TKConverter.addConverter(TKXmlConverter.CONV_NAME,
224: "com.teamkonzept.lib.TKXmlConverter");
225: TKConverter.addConverter(TKXmlConverter.CONV_ID,
226: "com.teamkonzept.lib.TKXmlConverter");
227: TKConverter.addConverter(TKStringConverter.CONV_NAME,
228: "com.teamkonzept.lib.TKStringConverter");
229: TKConverter.addConverter(TKStringConverter.CONV_ID,
230: "com.teamkonzept.lib.TKStringConverter");
231: TKConverter.addConverter(TKJavaScriptConverter.CONV_NAME,
232: "com.teamkonzept.lib.TKJavaScriptConverter");
233: TKConverter.addConverter(TKJavaScriptConverter.CONV_ID,
234: "com.teamkonzept.lib.TKJavaScriptConverter");
235: TKConverter.addConverter(TKLaTeXConverter.CONV_NAME,
236: "com.teamkonzept.lib.TKLaTeXConverter");
237: TKConverter.addConverter(TKLaTeXConverter.CONV_ID,
238: "com.teamkonzept.lib.TKLaTeXConverter");
239: TKConverter
240: .addConverter(
241: com.teamkonzept.webman.barmer.TKBarmerConverter.CONV_NAME,
242: "com.teamkonzept.webman.barmer.TKBarmerConverter");
243: TKConverter
244: .addConverter(
245: com.teamkonzept.webman.barmer.TKBarmerConverter.CONV_ID,
246: "com.teamkonzept.webman.barmer.TKBarmerConverter");
247:
248: TKConverter
249: .addConverter(
250: com.teamkonzept.webman.bchannel.TKBChannelConverter.CONV_NAME,
251: "com.teamkonzept.webman.bchannel.TKBChannelConverter");
252: TKConverter
253: .addConverter(
254: com.teamkonzept.webman.bchannel.TKBChannelConverter.CONV_ID,
255: "com.teamkonzept.webman.bchannel.TKBChannelConverter");
256: TKConverter
257: .addConverter(
258: com.teamkonzept.webman.bchannel.TKBChannelConverter2.CONV_NAME,
259: "com.teamkonzept.webman.bchannel.TKBChannelConverter2");
260: TKConverter
261: .addConverter(
262: com.teamkonzept.webman.bchannel.TKBChannelConverter2.CONV_ID,
263: "com.teamkonzept.webman.bchannel.TKBChannelConverter2");
264: }
265:
266: static {
267: initialize();
268: }
269: }
|