KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > ICUResourceBundleReader


1 //##header 1189099963000 FOUNDATION
2
/*
3  *******************************************************************************
4  * Copyright (C) 2004-2005, International Business Machines Corporation and *
5  * others. All Rights Reserved. *
6  *******************************************************************************
7  */

8 package com.ibm.icu.impl;
9
10 import java.io.BufferedInputStream JavaDoc;
11 import java.io.DataInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14
15 import com.ibm.icu.util.ULocale;
16 import com.ibm.icu.util.VersionInfo;
17
18
19
20 /**
21  * This class reads the *.res resource bundle format
22  *
23  * (For the latest version of the file format documentation see
24  * ICU4C's source/common/uresdata.h file.)
25  *
26  * File format for .res resource bundle files (formatVersion=1.2)
27  *
28  * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
29  * with nested, hierarchical data structures.
30  * It physically contains the following:
31  *
32  * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
33  * currently, the root item must be a table or table32 resource item
34  * int32_t indexes[indexes[0]]; -- array of indexes for friendly
35  * reading and swapping; see URES_INDEX_* above
36  * new in formatVersion 1.1 (ICU 2.8)
37  * char keys[]; -- characters for key strings
38  * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
39  * (minus the space for root and indexes[]),
40  * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
41  * padded to multiple of 4 bytes for 4-alignment of the following data
42  * data; -- data directly and indirectly indexed by the root item;
43  * the structure is determined by walking the tree
44  *
45  * Each resource bundle item has a 32-bit Resource handle (see typedef above)
46  * which contains the item type number in its upper 4 bits (31..28) and either
47  * an offset or a direct value in its lower 28 bits (27..0).
48  * The order of items is undefined and only determined by walking the tree.
49  * Leaves of the tree may be stored first or last or anywhere in between,
50  * and it is in theory possible to have unreferenced holes in the file.
51  *
52  * Direct values:
53  * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
54  * - Integer values are 28-bit values stored in the Resource handle itself;
55  * the interpretation of unsigned vs. signed integers is up to the application.
56  *
57  * All other types and values use 28-bit offsets to point to the item's data.
58  * The offset is an index to the first 32-bit word of the value, relative to the
59  * start of the resource data (i.e., the root item handle is at offset 0).
60  * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
61  * All resource item values are 4-aligned.
62  *
63  * The structures (memory layouts) for the values for each item type are listed
64  * in the table above.
65  *
66  * Nested, hierarchical structures: -------------
67  *
68  * Table items contain key-value pairs where the keys are 16-bit offsets to char * key strings.
69  * Key string offsets are also relative to the start of the resource data (of the root handle),
70  * i.e., the first string has an offset of 4 (after the 4-byte root handle).
71  *
72  * The values of these pairs are Resource handles.
73  *
74  * Array items are simple vectors of Resource handles.
75  *
76  * An alias item is special (and new in ICU 2.4): --------------
77  *
78  * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
79  * another resource bundle's item according to the path in the string.
80  * This is used to share items across bundles that are in different lookup/fallback
81  * chains (e.g., large collation data among zh_TW and zh_HK).
82  * This saves space (for large items) and maintenance effort (less duplication of data).
83  *
84  * --------------------------------------------------------------------------
85  *
86  * Resource types:
87  *
88  * Most resources have their values stored at four-byte offsets from the start
89  * of the resource data. These values are at least 4-aligned.
90  * Some resource values are stored directly in the offset field of the Resource itself.
91  * See UResType in unicode/ures.h for enumeration constants for Resource types.
92  *
93  * Type Name Memory layout of values
94  * (in parentheses: scalar, non-offset values)
95  *
96  * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
97  * or (empty string ("") if offset==0)
98  * 1 Binary: int32_t length, uint8_t[length], (padding)
99  * - this value should be 32-aligned -
100  * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
101  * 3 Alias: (physically same value layout as string, new in ICU 2.4)
102  * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
103  * (new in formatVersion 1.1/ICU 2.8)
104  *
105  * 7 Integer: (28-bit offset is integer value)
106  * 8 Array: int32_t count, Resource[count]
107  *
108  * 14 Integer Vector: int32_t length, int32_t[length]
109  * 15 Reserved: This value denotes special purpose resources and is for internal use.
110  *
111  * Note that there are 3 types with data vector values:
112  * - Vectors of 8-bit bytes stored as type Binary.
113  * - Vectors of 16-bit words stored as type Unicode String
114  * (no value restrictions, all values 0..ffff allowed!).
115  * - Vectors of 32-bit words stored as type Integer Vector.
116  *
117  *
118  */

119 public final class ICUResourceBundleReader implements ICUBinary.Authenticate{
120
121     /**
122      * File format version that this class understands.
123      * "ResB"
124      */

125     private static final byte DATA_FORMAT_ID[] = {(byte)0x52, (byte)0x65,
126                                                      (byte)0x73, (byte)0x42};
127
128     private static final String JavaDoc ICU_RESOURCE_SUFFIX = ".res";
129     
130     /* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
131     private static final int URES_INDEX_LENGTH = 0; /* [0] contains URES_INDEX_TOP==the length of indexes[] */
132     private static final int URES_INDEX_STRINGS_TOP = 1; /* [1] contains the top of the strings, */
133                                                                         /* same as the bottom of resources, rounded up */
134     private static final int URES_INDEX_RESOURCES_TOP = 2; /* [2] contains the top of all resources */
135     private static final int URES_INDEX_BUNDLE_TOP = 3; /* [3] contains the top of the bundle, */
136                                                                         /* in case it were ever different from [2] */
137     private static final int URES_INDEX_MAX_TABLE_LENGTH = 4; /* [4] max. length of any table */
138     private static final int URES_INDEX_ATTRIBUTES = 5; /* [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2) */
139     private static final int URES_INDEX_TOP = 6;
140
141     //private static final int URES_STRINGS_BOTTOM=(1+URES_INDEX_TOP)*4;
142

143     /*
144      * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
145      * New in formatVersion 1.2 (ICU 3.6).
146      *
147      * If set, then this resource bundle is a standalone bundle.
148      * If not set, then the bundle participates in locale fallback, eventually
149      * all the way to the root bundle.
150      * If indexes[] is missing or too short, then the attribute cannot be determined
151      * reliably. Dependency checking should ignore such bundles, and loading should
152      * use fallbacks.
153      */

154     private static final int URES_ATT_NO_FALLBACK = 1;
155
156     private static final boolean DEBUG = false;
157     
158     private byte[] /* formatVersion, */ dataVersion;
159
160     private int rootRes;
161     private int[] indexes;
162     private boolean noFallback; /* see URES_ATT_NO_FALLBACK */
163
164     private byte[] data;
165
166     private ICUResourceBundleReader(InputStream JavaDoc stream, String JavaDoc resolvedName){
167
168         BufferedInputStream JavaDoc bs = new BufferedInputStream JavaDoc(stream);
169         try{
170             if(DEBUG) System.out.println("The InputStream class is: " + stream.getClass().getName());
171             if(DEBUG) System.out.println("The BufferedInputStream class is: " + bs.getClass().getName());
172             if(DEBUG) System.out.println("The bytes avialable in stream before reading the header: " + bs.available());
173             
174             dataVersion = ICUBinary.readHeader(bs,DATA_FORMAT_ID,this);
175
176             if(DEBUG) System.out.println("The bytes available in stream after reading the header: " + bs.available());
177                  
178             readData(bs);
179             stream.close();
180         }catch(IOException JavaDoc ex){
181 //#ifndef FOUNDATION
182
//## throw new RuntimeException("Data file "+ resolvedName+ " is corrupt.", ex);
183
//#else
184
throw new RuntimeException JavaDoc("Data file "+ resolvedName+ " is corrupt.");
185 //#endif
186
}
187     }
188     public static ICUResourceBundleReader getReader(String JavaDoc baseName, String JavaDoc localeName, ClassLoader JavaDoc root){
189         String JavaDoc resolvedName = getFullName(baseName, localeName);
190         InputStream JavaDoc stream = ICUData.getStream(root,resolvedName);
191         
192         if(stream==null){
193             return null;
194         }
195         ICUResourceBundleReader reader = new ICUResourceBundleReader(stream, resolvedName);
196         return reader;
197     }
198
199     private static void writeInt(int i, byte[] bytes, int offset) {
200         bytes[offset++]=(byte)(i>>24);
201         bytes[offset++]=(byte)(i>>16);
202         bytes[offset++]=(byte)(i>>8);
203         bytes[offset]=(byte)i;
204     }
205
206     private void readData(InputStream JavaDoc stream)
207             throws IOException JavaDoc{
208         
209         DataInputStream JavaDoc ds = new DataInputStream JavaDoc(stream);
210
211         if(DEBUG) System.out.println("The DataInputStream class is: " + ds.getClass().getName());
212         if(DEBUG) System.out.println("The available bytes in the stream before reading the data: "+ds.available());
213
214         /*
215          * The following will read two integers before ds.mark().
216          * Later, the two integers need to be placed into data[],
217          * then ds.reset(), then ds.readFully(into rest of data[]).
218          *
219          * This is necessary because we don't know the readLimit for ds.mark()
220          * until we have read the second integer (indexLength).
221          */

222         rootRes = ds.readInt();
223
224         // read the variable-length indexes[] array
225
int indexLength = ds.readInt();
226         ds.mark((indexLength-1)*4);
227
228         indexes = new int[indexLength];
229         indexes[URES_INDEX_LENGTH] = indexLength;
230
231         for(int i=1; i<indexLength; i++){
232             indexes[i] = ds.readInt();
233         }
234
235         // determine if this resource bundle falls back to a parent bundle
236
// along normal locale ID fallback
237
noFallback =
238             indexLength > URES_INDEX_ATTRIBUTES &&
239             (indexes[URES_INDEX_ATTRIBUTES]&URES_ATT_NO_FALLBACK)!=0;
240
241         // read the entire bundle (after the header) into data[]
242
// put rootRes and indexLength into data[0..7]
243
// and the rest of the data into data[8..length-1]
244
int length = indexes[URES_INDEX_BUNDLE_TOP]*4;
245         if(DEBUG) System.out.println("The number of bytes in the bundle: "+length);
246     
247         data = new byte[length];
248         writeInt(rootRes, data, 0);
249         writeInt(indexLength, data, 4);
250
251         // now reset to the mark, which was set after reading rootRes and indexLength
252
ds.reset();
253         ds.readFully(data, 8, length-8);
254     }
255
256     /**
257      * Gets the full name of the resource with suffix.
258      */

259     public static String JavaDoc getFullName(String JavaDoc baseName, String JavaDoc localeName){
260         if(baseName==null || baseName.length()==0){
261             if(localeName.length()==0){
262                 return ULocale.getDefault().toString()+ICU_RESOURCE_SUFFIX;
263             }else{
264                 return localeName+ICU_RESOURCE_SUFFIX;
265             }
266         }else{
267             if(baseName.indexOf('.')==-1){
268                 if(baseName.charAt(baseName.length()-1)!= '/'){
269                     return baseName+"/"+localeName+ICU_RESOURCE_SUFFIX;
270                 }else{
271                     return baseName+localeName+ICU_RESOURCE_SUFFIX;
272                 }
273             }else{
274                 baseName = baseName.replace('.','/');
275                 if(localeName.length()==0){
276                     return baseName+ICU_RESOURCE_SUFFIX;
277                 }else{
278                     return baseName+"_"+localeName+ICU_RESOURCE_SUFFIX;
279                 }
280             }
281         }
282     }
283     
284     public VersionInfo getVersion(){
285         return VersionInfo.getInstance(dataVersion[0],dataVersion[1],dataVersion[2],dataVersion[3]);
286     }
287     public boolean isDataVersionAcceptable(byte version[]){
288         // while ICU4C can read formatVersion 1.0 and up,
289
// ICU4J requires 1.1 as a minimum
290
// formatVersion = version;
291
return version[0] == 1 && version[1] >= 1;
292     }
293     
294     public byte[] getData(){
295         return data;
296     }
297     public int getRootResource() {
298         return rootRes;
299     }
300     public boolean getNoFallback() {
301         return noFallback;
302     }
303 }
304
Popular Tags