KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > TKConverter


1 package com.teamkonzept.lib;
2
3 import java.util.*;
4
5 /**
6     Basisklasse fuer Konvertierungsklassen zum Umwandeln von
7     Javas Unicode in andere Kodierungen oder zurueck
8  * @author $Author: alex $
9  * @version $Revision: 1.11 $
10 */

11 public abstract class TKConverter {
12     /**
13         Converts a string in this CharacterSet to a Java char string.
14     */

15     public abstract int bytesToChars(
16         byte src[],
17         char dst[],
18         int srcBegin,
19         int length,
20         int dstBegin
21     );
22     
23     /**
24         Converts a string from this CharacterSet to a Java string.
25     */

26     public String JavaDoc 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 JavaDoc( newChars, 0, total );
32     }
33     /**
34         Converts a Java char string to this CharacterSet.
35     */

36     public abstract int charsToBytes(
37         char src[],
38         byte dst[],
39         int srcBegin,
40         int length,
41         int dstBegin
42     );
43     
44     /**
45         ask wether the converter want's to do the job itself
46     */

47     public byte[] doYourself (String JavaDoc src) { return null; }
48
49     /**
50         Returns the boolean result whether this character set is equivalent to another CharacterSet.
51     */

52     public boolean equals(Object JavaDoc converter)
53     {
54         return converter.getClass() == getClass();
55     }
56     
57     public int hashCode()
58     {
59         return getName().hashCode();
60     }
61     
62     /**
63         Returns the maximum number of bytes a character takes in this CharacterSet.
64     */

65     public abstract int getMaxBytesPerChar();
66     
67     /**
68         Returns the Internet Assigned Numbers Authority (IANA) name of this CharacterSet.
69     */

70     public abstract String JavaDoc getName();
71     
72     /**
73         Determines the minimum number of bytes needed to store a string in this CharacterSet.
74     */

75     public int minByteSize(byte code[], TKConverter srcConverter)
76     {
77         return minByteSize( code, 0, code.length, srcConverter );
78     }
79     
80     /**
81         Determines the minimum number of bytes needed to store a string in this CharacterSet.
82     */

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     /**
94         Determines the minimum number of bytes needed to store an Java char string in this CharacterSet.
95     */

96     public int minByteSize( char src[] )
97     {
98         return minByteSize( src, 0, src.length );
99     }
100     
101     /**
102         Determines the minimum number of bytes needed to store an Java char string in this CharacterSet.
103     */

104     public int minByteSize(
105         char chars[],
106         int begin,
107         int length
108     )
109     {
110         return minByteSize( length );
111     }
112     
113     /**
114         Determines the minimum number of bytes needed to store a Java char string in this CharacterSet.
115     */

116     public int minByteSize( int charCount )
117     {
118         return charCount * getMaxBytesPerChar();
119     }
120     /**
121         Determines the minimum number of bytes needed to store a string in this CharacterSet.
122     */

123     public int minByteSize( int length, TKConverter srcConverter )
124     {
125         return minByteSize( srcConverter.minCharSize(length) );
126     }
127     
128     /**
129         Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
130     */

131     public int minCharSize( byte[] code )
132     {
133         return minCharSize( code, 0, code.length );
134     }
135     
136     /**
137         Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
138     */

139     public int minCharSize( byte[] code, int begin, int length )
140     {
141         return minCharSize( code.length );
142     }
143     
144     /**
145         Determines the minimum number of chars needed to store a string in this CharacterSet in a Java string.
146     */

147     public abstract int minCharSize( int length );
148     
149     /**
150         Converts a Java string to this CharacterSet.
151     */

152     public byte[] stringToBytes( String JavaDoc 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 JavaDoc id, String JavaDoc converterClass )
172     {
173         initialize();
174         synchronized( converterHash ) {
175             converterHash.put( id, converterClass );
176         }
177     }
178     
179     public static void addConverter( String JavaDoc[] ids, String JavaDoc 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 JavaDoc id )
190     {
191         initialize();
192         synchronized( converterHash ) {
193             Object JavaDoc entry = converterHash.get( id );
194             if( entry == null) return (TKConverter) entry;
195             if( entry instanceof TKConverter) return (TKConverter) entry;
196             try {
197                 String JavaDoc classString = (String JavaDoc) entry;
198                 Class JavaDoc convClass = Class.forName( classString );
199                 TKConverter conv = (TKConverter) convClass.newInstance();
200                 Enumeration e = converterHash.keys();
201                 while( e.hasMoreElements() ) {
202                     String JavaDoc key = (String JavaDoc) e.nextElement();
203                     Object JavaDoc val = converterHash.get( key );
204                     if( (val instanceof String JavaDoc) && ( val.equals( classString ) ) ) {
205                         converterHash.put( key, conv );
206                     }
207                 }
208                 return conv;
209             }
210             catch( Throwable JavaDoc t ) {
211                 throw new InstantiationError JavaDoc( 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