1 package com.teamkonzept.lib; 2 3 import java.util.*; 4 5 11 public abstract class TKConverter { 12 15 public abstract int bytesToChars( 16 byte src[], 17 char dst[], 18 int srcBegin, 19 int length, 20 int dstBegin 21 ); 22 23 26 public String bytesToString( byte[] code ) 27 { 28 int len = minCharSize( code ); 29 char[] newChars = new char[ len ]; 30 int total = bytesToChars( code, newChars, 0, code.length, 0 ); 31 return new String ( newChars, 0, total ); 32 } 33 36 public abstract int charsToBytes( 37 char src[], 38 byte dst[], 39 int srcBegin, 40 int length, 41 int dstBegin 42 ); 43 44 47 public byte[] doYourself (String src) { return null; } 48 49 52 public boolean equals(Object converter) 53 { 54 return converter.getClass() == getClass(); 55 } 56 57 public int hashCode() 58 { 59 return getName().hashCode(); 60 } 61 62 65 public abstract int getMaxBytesPerChar(); 66 67 70 public abstract String getName(); 71 72 75 public int minByteSize(byte code[], TKConverter srcConverter) 76 { 77 return minByteSize( code, 0, code.length, srcConverter ); 78 } 79 80 83 public int minByteSize( 84 byte bytes[], 85 int begin, 86 int length, 87 TKConverter srcConverter 88 ) 89 { 90 return minByteSize( length, srcConverter ); 91 } 92 93 96 public int minByteSize( char src[] ) 97 { 98 return minByteSize( src, 0, src.length ); 99 } 100 101 104 public int minByteSize( 105 char chars[], 106 int begin, 107 int length 108 ) 109 { 110 return minByteSize( length ); 111 } 112 113 116 public int minByteSize( int charCount ) 117 { 118 return charCount * getMaxBytesPerChar(); 119 } 120 123 public int minByteSize( int length, TKConverter srcConverter ) 124 { 125 return minByteSize( srcConverter.minCharSize(length) ); 126 } 127 128 131 public int minCharSize( byte[] code ) 132 { 133 return minCharSize( code, 0, code.length ); 134 } 135 136 139 public int minCharSize( byte[] code, int begin, int length ) 140 { 141 return minCharSize( code.length ); 142 } 143 144 147 public abstract int minCharSize( int length ); 148 149 152 public byte[] stringToBytes( String src ) 153 { 154 byte[] result = doYourself (src); 155 if (result != null) return result; 156 157 char[] srcArray = src.toCharArray(); 158 int tmpSize = minByteSize( srcArray ); 159 byte[] temp = new byte[ tmpSize ]; 160 int total = charsToBytes( srcArray, temp, 0, srcArray.length, 0); 161 if( total == tmpSize ) return temp; 162 163 result = new byte[ total ]; 164 System.arraycopy( temp, 0, result, 0, total ); 165 temp = null; 166 return result; 167 } 168 169 protected static TKHashtable converterHash; 170 171 public static void addConverter( String id, String converterClass ) 172 { 173 initialize(); 174 synchronized( converterHash ) { 175 converterHash.put( id, converterClass ); 176 } 177 } 178 179 public static void addConverter( String [] ids, String converterClass ) 180 { 181 initialize(); 182 synchronized( converterHash ) { 183 for( int i=0; i<ids.length; i++ ) { 184 converterHash.put( ids[i], converterClass ); 185 } 186 } 187 } 188 189 public static TKConverter getConverter( String id ) 190 { 191 initialize(); 192 synchronized( converterHash ) { 193 Object entry = converterHash.get( id ); 194 if( entry == null) return (TKConverter) entry; 195 if( entry instanceof TKConverter) return (TKConverter) entry; 196 try { 197 String classString = (String ) entry; 198 Class convClass = Class.forName( classString ); 199 TKConverter conv = (TKConverter) convClass.newInstance(); 200 Enumeration e = converterHash.keys(); 201 while( e.hasMoreElements() ) { 202 String key = (String ) e.nextElement(); 203 Object val = converterHash.get( key ); 204 if( (val instanceof String ) && ( val.equals( classString ) ) ) { 205 converterHash.put( key, conv ); 206 } 207 } 208 return conv; 209 } 210 catch( Throwable t ) { 211 throw new InstantiationError ( t.getMessage() ); 212 } 213 } 214 } 215 216 public static Enumeration converterIds() 217 { 218 initialize(); 219 synchronized( converterHash ) { 220 return converterHash.keys(); 221 } 222 } 223 224 protected static boolean initialized = false; 225 226 public final static void initialize() 227 { 228 if( initialized ) return; 229 initialized = true; 230 converterHash = new TKHashtable(); 231 232 TKConverter.addConverter( TKAnsiConverter.CONV_NAME, "com.teamkonzept.lib.TKAnsiConverter" ); 233 TKConverter.addConverter( TKAnsiConverter.CONV_ID, "com.teamkonzept.lib.TKAnsiConverter" ); 234 TKConverter.addConverter( TKUrlConverter.CONV_NAME, "com.teamkonzept.lib.TKUrlConverter" ); 235 TKConverter.addConverter( TKUrlConverter.CONV_ID, "com.teamkonzept.lib.TKUrlConverter" ); 236 TKConverter.addConverter( TKHtmlConverter.CONV_NAME, "com.teamkonzept.lib.TKHtmlConverter" ); 237 TKConverter.addConverter( TKHtmlConverter.CONV_ID, "com.teamkonzept.lib.TKHtmlConverter" ); 238 TKConverter.addConverter( TKUppercaseConverter.CONV_NAME, "com.teamkonzept.lib.TKUppercaseConverter" ); 239 TKConverter.addConverter( TKUppercaseConverter.CONV_ID, "com.teamkonzept.lib.TKUppercaseConverter" ); 240 TKConverter.addConverter( TKXmlConverter.CONV_NAME, "com.teamkonzept.lib.TKXmlConverter" ); 241 TKConverter.addConverter( TKXmlConverter.CONV_ID, "com.teamkonzept.lib.TKXmlConverter" ); 242 TKConverter.addConverter( TKStringConverter.CONV_NAME, "com.teamkonzept.lib.TKStringConverter" ); 243 TKConverter.addConverter( TKStringConverter.CONV_ID, "com.teamkonzept.lib.TKStringConverter" ); 244 TKConverter.addConverter( TKJavaScriptConverter.CONV_NAME, "com.teamkonzept.lib.TKJavaScriptConverter" ); 245 TKConverter.addConverter( TKJavaScriptConverter.CONV_ID, "com.teamkonzept.lib.TKJavaScriptConverter" ); 246 TKConverter.addConverter( TKLaTeXConverter.CONV_NAME, "com.teamkonzept.lib.TKLaTeXConverter" ); 247 TKConverter.addConverter( TKLaTeXConverter.CONV_ID, "com.teamkonzept.lib.TKLaTeXConverter" ); 248 TKConverter.addConverter( com.teamkonzept.webman.barmer.TKBarmerConverter.CONV_NAME, "com.teamkonzept.webman.barmer.TKBarmerConverter" ); 249 TKConverter.addConverter( com.teamkonzept.webman.barmer.TKBarmerConverter.CONV_ID, "com.teamkonzept.webman.barmer.TKBarmerConverter" ); 250 251 TKConverter.addConverter( com.teamkonzept.webman.bchannel.TKBChannelConverter.CONV_NAME, "com.teamkonzept.webman.bchannel.TKBChannelConverter" ); 252 TKConverter.addConverter( com.teamkonzept.webman.bchannel.TKBChannelConverter.CONV_ID, "com.teamkonzept.webman.bchannel.TKBChannelConverter" ); 253 TKConverter.addConverter( com.teamkonzept.webman.bchannel.TKBChannelConverter2.CONV_NAME, "com.teamkonzept.webman.bchannel.TKBChannelConverter2" ); 254 TKConverter.addConverter( com.teamkonzept.webman.bchannel.TKBChannelConverter2.CONV_ID, "com.teamkonzept.webman.bchannel.TKBChannelConverter2" ); 255 } 256 257 static { 258 initialize(); 259 } 260 } 261 262 | Popular Tags |