KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7  
8 package com.ibm.icu.impl;
9 import java.io.*;
10 import com.ibm.icu.impl.ICUDebug;
11
12 /**
13  * @version 1.0
14  * @author Ram Viswanadha
15  */

16
17     /*
18      * Description of the format of unorm.icu version 2.1.
19      *
20      * Main change from version 1 to version 2:
21      * Use of new, common Trie instead of normalization-specific tries.
22      * Change to version 2.1: add third/auxiliary trie with associated data.
23      *
24      * For more details of how to use the data structures see the code
25      * in unorm.cpp (runtime normalization code) and
26      * in gennorm.c and gennorm/store.c (build-time data generation).
27      *
28      * For the serialized format of Trie see Trie.c/TrieHeader.
29      *
30      * - Overall partition
31      *
32      * unorm.icu customarily begins with a UDataInfo structure, see udata.h and .c.
33      * After that there are the following structures:
34      *
35      * char indexes[INDEX_TOP]; -- INDEX_TOP=32, see enum in this file
36      *
37      * Trie normTrie; -- size in bytes=indexes[INDEX_TRIE_SIZE]
38      *
39      * char extraData[extraDataTop]; -- extraDataTop=indexes[INDEX_UCHAR_COUNT]
40      * extraData[0] contains the number of units for
41      * FC_NFKC_Closure (formatVersion>=2.1)
42      *
43      * char combiningTable[combiningTableTop]; -- combiningTableTop=indexes[INDEX_COMBINE_DATA_COUNT]
44      * combiningTableTop may include one 16-bit padding unit
45      * to make sure that fcdTrie is 32-bit-aligned
46      *
47      * Trie fcdTrie; -- size in bytes=indexes[INDEX_FCD_TRIE_SIZE]
48      *
49      * Trie auxTrie; -- size in bytes=indexes[INDEX_AUX_TRIE_SIZE]
50      *
51      * char canonStartSets[canonStartSetsTop] -- canonStartSetsTop=indexes[INDEX_CANON_SET_COUNT]
52      * serialized USets, see uset.c
53      *
54      *
55      * The indexes array contains lengths and sizes of the following arrays and structures
56      * as well as the following values:
57      * indexes[INDEX_COMBINE_FWD_COUNT]=combineFwdTop
58      * -- one more than the highest combining index computed for forward-only-combining characters
59      * indexes[INDEX_COMBINE_BOTH_COUNT]=combineBothTop-combineFwdTop
60      * -- number of combining indexes computed for both-ways-combining characters
61      * indexes[INDEX_COMBINE_BACK_COUNT]=combineBackTop-combineBothTop
62      * -- number of combining indexes computed for backward-only-combining characters
63      *
64      * indexes[INDEX_MIN_NF*_NO_MAYBE] (where *={ C, D, KC, KD })
65      * -- first code point with a quick check NF* value of NO/MAYBE
66      *
67      *
68      * - Tries
69      *
70      * The main structures are two Trie tables ("compact arrays"),
71      * each with one index array and one data array.
72      * See Trie.h and Trie.c.
73      *
74      *
75      * - Tries in unorm.icu
76      *
77      * The first trie (normTrie above)
78      * provides data for the NF* quick checks and normalization.
79      * The second trie (fcdTrie above) provides data just for FCD checks.
80      *
81      *
82      * - norm32 data words from the first trie
83      *
84      * The norm32Table contains one 32-bit word "norm32" per code point.
85      * It contains the following bit fields:
86      * 31..16 extra data index, EXTRA_SHIFT is used to shift this field down
87      * if this index is <EXTRA_INDEX_TOP then it is an index into
88      * extraData[] where variable-length normalization data for this
89      * code point is found
90      * if this index is <EXTRA_INDEX_TOP+EXTRA_SURROGATE_TOP
91      * then this is a norm32 for a leading surrogate, and the index
92      * value is used together with the following trailing surrogate
93      * code unit in the second trie access
94      * if this index is >=EXTRA_INDEX_TOP+EXTRA_SURROGATE_TOP
95      * then this is a norm32 for a "special" character,
96      * i.e., the character is a Hangul syllable or a Jamo
97      * see EXTRA_HANGUL etc.
98      * generally, instead of extracting this index from the norm32 and
99      * comparing it with the above constants,
100      * the normalization code compares the entire norm32 value
101      * with MIN_SPECIAL, SURROGATES_TOP, MIN_HANGUL etc.
102      *
103      * 15..8 combining class (cc) according to UnicodeData.txt
104      *
105      * 7..6 COMBINES_ANY flags, used in composition to see if a character
106      * combines with any following or preceding character(s)
107      * at all
108      * 7 COMBINES_BACK
109      * 6 COMBINES_FWD
110      *
111      * 5..0 quick check flags, set for "no" or "maybe", with separate flags for
112      * each normalization form
113      * the higher bits are "maybe" flags; for NF*D there are no such flags
114      * the lower bits are "no" flags for all forms, in the same order
115      * as the "maybe" flags,
116      * which is (MSB to LSB): NFKD NFD NFKC NFC
117      * 5..4 QC_ANY_MAYBE
118      * 3..0 QC_ANY_NO
119      * see further related constants
120      *
121      *
122      * - Extra data per code point
123      *
124      * "Extra data" is referenced by the index in norm32.
125      * It is variable-length data. It is only present, and only those parts
126      * of it are, as needed for a given character.
127      * The norm32 extra data index is added to the beginning of extraData[]
128      * to get to a vector of 16-bit words with data at the following offsets:
129      *
130      * [-1] Combining index for composition.
131      * Stored only if norm32&COMBINES_ANY .
132      * [0] Lengths of the canonical and compatibility decomposition strings.
133      * Stored only if there are decompositions, i.e.,
134      * if norm32&(QC_NFD|QC_NFKD)
135      * High byte: length of NFKD, or 0 if none
136      * Low byte: length of NFD, or 0 if none
137      * Each length byte also has another flag:
138      * Bit 7 of a length byte is set if there are non-zero
139      * combining classes (cc's) associated with the respective
140      * decomposition. If this flag is set, then the decomposition
141      * is preceded by a 16-bit word that contains the
142      * leading and trailing cc's.
143      * Bits 6..0 of a length byte are the length of the
144      * decomposition string, not counting the cc word.
145      * [1..n] NFD
146      * [n+1..] NFKD
147      *
148      * Each of the two decompositions consists of up to two parts:
149      * - The 16-bit words with the leading and trailing cc's.
150      * This is only stored if bit 7 of the corresponding length byte
151      * is set. In this case, at least one of the cc's is not zero.
152      * High byte: leading cc==cc of the first code point in the decomposition string
153      * Low byte: trailing cc==cc of the last code point in the decomposition string
154      * - The decomposition string in UTF-16, with length code units.
155      *
156      *
157      * - Combining indexes and combiningTable[]
158      *
159      * Combining indexes are stored at the [-1] offset of the extra data
160      * if the character combines forward or backward with any other characters.
161      * They are used for (re)composition in NF*C.
162      * Values of combining indexes are arranged according to whether a character
163      * combines forward, backward, or both ways:
164      * forward-only < both ways < backward-only
165      *
166      * The index values for forward-only and both-ways combining characters
167      * are indexes into the combiningTable[].
168      * The index values for backward-only combining characters are simply
169      * incremented from the preceding index values to be unique.
170      *
171      * In the combiningTable[], a variable-length list
172      * of variable-length (back-index, code point) pair entries is stored
173      * for each forward-combining character.
174      *
175      * These back-indexes are the combining indexes of both-ways or backward-only
176      * combining characters that the forward-combining character combines with.
177      *
178      * Each list is sorted in ascending order of back-indexes.
179      * Each list is terminated with the last back-index having bit 15 set.
180      *
181      * Each pair (back-index, code point) takes up either 2 or 3
182      * 16-bit words.
183      * The first word of a list entry is the back-index, with its bit 15 set if
184      * this is the last pair in the list.
185      *
186      * The second word contains flags in bits 15..13 that determine
187      * if there is a third word and how the combined character is encoded:
188      * 15 set if there is a third word in this list entry
189      * 14 set if the result is a supplementary character
190      * 13 set if the result itself combines forward
191      *
192      * According to these bits 15..14 of the second word,
193      * the result character is encoded as follows:
194      * 00 or 01 The result is <=0x1fff and stored in bits 12..0 of
195      * the second word.
196      * 10 The result is 0x2000..0xffff and stored in the third word.
197      * Bits 12..0 of the second word are not used.
198      * 11 The result is a supplementary character.
199      * Bits 9..0 of the leading surrogate are in bits 9..0 of
200      * the second word.
201      * Add 0xd800 to these bits to get the complete surrogate.
202      * Bits 12..10 of the second word are not used.
203      * The trailing surrogate is stored in the third word.
204      *
205      *
206      * - FCD trie
207      *
208      * The FCD trie is very simple.
209      * It is a folded trie with 16-bit data words.
210      * In each word, the high byte contains the leading cc of the character,
211      * and the low byte contains the trailing cc of the character.
212      * These cc's are the cc's of the first and last code points in the
213      * canonical decomposition of the character.
214      *
215      * Since all 16 bits are used for cc's, lead surrogates must be tested
216      * by checking the code unit instead of the trie data.
217      * This is done only if the 16-bit data word is not zero.
218      * If the code unit is a leading surrogate and the data word is not zero,
219      * then instead of cc's it contains the offset for the second trie lookup.
220      *
221      *
222      * - Auxiliary trie and data
223      *
224      *
225      * The auxiliary 16-bit trie contains data for additional properties.
226      * Bits
227      * 15..13 reserved
228      * 12 not NFC_Skippable (f) (formatVersion>=2.2)
229      * 11 flag: not a safe starter for canonical closure
230      * 10 composition exclusion
231      * 9.. 0 index into extraData[] to FC_NFKC_Closure string
232      * (not for lead surrogate),
233      * or lead surrogate offset (for lead surrogate, if 9..0 not zero)
234      *
235      * Conditions for "NF* Skippable" from Mark Davis' com.ibm.text.UCD.NFSkippable:
236      * (used in NormalizerTransliterator)
237      *
238      * A skippable character is
239      * a) unassigned, or ALL of the following:
240      * b) of combining class 0.
241      * c) not decomposed by this normalization form.
242      * AND if NFC or NFKC,
243      * d) can never compose with a previous character.
244      * e) can never compose with a following character.
245      * f) can never change if another character is added.
246      * Example: a-breve might satisfy all but f, but if you
247      * add an ogonek it changes to a-ogonek + breve
248      *
249      * a)..e) must be tested from norm32.
250      * Since f) is more complicated, the (not-)NFC_Skippable flag (f) is built
251      * into the auxiliary trie.
252      * The same bit is used for NFC and NFKC; (c) differs for them.
253      * As usual, we build the "not skippable" flags so that unassigned
254      * code points get a 0 bit.
255      * This bit is only valid after (a)..(e) test FALSE; test NFD_NO before (f) as well.
256      * Test Hangul LV syllables entirely in code.
257      *
258      *
259      * - FC_NFKC_Closure strings in extraData[]
260      *
261      * Strings are either stored as a single code unit or as the length
262      * followed by that many units.
263      *
264      * - structure inside canonStartSets[]
265      *
266      * This array maps from code points c to sets of code points (USerializedSet).
267      * The result sets are the code points whose canonical decompositions start
268      * with c.
269      *
270      * canonStartSets[] contains the following sub-arrays:
271      *
272      * indexes[_NORM_SET_INDEX_TOP]
273      * - contains lengths of sub-arrays etc.
274      *
275      * startSets[indexes[_NORM_SET_INDEX_CANON_SETS_LENGTH]-_NORM_SET_INDEX_TOP]
276      * - contains serialized sets (USerializedSet) of canonical starters for
277      * enumerating canonically equivalent strings
278      * indexes[_NORM_SET_INDEX_CANON_SETS_LENGTH] includes _NORM_SET_INDEX_TOP
279      * for details about the structure see uset.c
280      *
281      * bmpTable[indexes[_NORM_SET_INDEX_CANON_BMP_TABLE_LENGTH]]
282      * - a sorted search table for BMP code points whose results are
283      * either indexes to USerializedSets or single code points for
284      * single-code point sets;
285      * each entry is a pair of { code point, result } with result=(binary) yy xxxxxx xxxxxxxx
286      * if yy==01 then there is a USerializedSet at canonStartSets+x
287      * else build a USerializedSet with result as the single code point
288      *
289      * suppTable[indexes[_NORM_SET_INDEX_CANON_SUPP_TABLE_LENGTH]]
290      * - a sorted search table for supplementary code points whose results are
291      * either indexes to USerializedSets or single code points for
292      * single-code point sets;
293      * each entry is a triplet of { high16(cp), low16(cp), result }
294      * each code point's high-word may contain extra data in bits 15..5:
295      * if the high word has bit 15 set, then build a set with a single code point
296      * which is (((high16(cp)&0x1f00)<<8)|result;
297      * else there is a USerializedSet at canonStartSets+result
298      */

299 final class NormalizerDataReader implements ICUBinary.Authenticate {
300     private final static boolean debug = ICUDebug.enabled("NormalizerDataReader");
301     
302    /**
303     * <p>Protected constructor.</p>
304     * @param inputStream ICU uprop.dat file input stream
305     * @exception IOException throw if data file fails authentication
306     * @draft 2.1
307     */

308     protected NormalizerDataReader(InputStream inputStream)
309                                         throws IOException{
310         if(debug) System.out.println("Bytes in inputStream " + inputStream.available());
311         
312         unicodeVersion = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID, this);
313         
314         if(debug) System.out.println("Bytes left in inputStream " +inputStream.available());
315         
316         dataInputStream = new DataInputStream(inputStream);
317         
318         if(debug) System.out.println("Bytes left in dataInputStream " +dataInputStream.available());
319     }
320     
321     // protected methods -------------------------------------------------
322

323     protected int[] readIndexes(int length)throws IOException{
324         int[] indexes = new int[length];
325         //Read the indexes
326
for (int i = 0; i <length ; i++) {
327              indexes[i] = dataInputStream.readInt();
328         }
329         return indexes;
330     }
331     /**
332     * <p>Reads unorm.icu, parse it into blocks of data to be stored in
333     * NormalizerImpl.</P
334     * @param normBytes
335     * @param fcdBytes
336     * @param auxBytes
337     * @param extraData
338     * @param combiningTable
339     * @param canonStartSets
340     * @exception IOException thrown when data reading fails
341     * @draft 2.1
342     */

343     protected void read(byte[] normBytes, byte[] fcdBytes, byte[] auxBytes,
344                         char[] extraData, char[] combiningTable,
345                         Object JavaDoc[] canonStartSets)
346                         throws IOException{
347
348          //Read the bytes that make up the normTrie
349
dataInputStream.read(normBytes);
350         
351          //normTrieStream= new ByteArrayInputStream(normBytes);
352

353          //Read the extra data
354
for(int i=0;i<extraData.length;i++){
355              extraData[i]=dataInputStream.readChar();
356          }
357          
358          //Read the combining class table
359
for(int i=0; i<combiningTable.length; i++){
360              combiningTable[i]=dataInputStream.readChar();
361          }
362          
363          //Read the fcdTrie
364
dataInputStream.read(fcdBytes);
365          
366          
367          //Read the AuxTrie
368
dataInputStream.read(auxBytes);
369         
370         //Read the canonical start sets
371
int[] canonStartSetsIndexes = new int[NormalizerImpl.SET_INDEX_TOP];
372         
373         for(int i=0; i<canonStartSetsIndexes.length; i++){
374              canonStartSetsIndexes[i]=dataInputStream.readChar();
375          }
376         
377         char[] startSets = new char[canonStartSetsIndexes[NormalizerImpl.SET_INDEX_CANON_SETS_LENGTH]-NormalizerImpl.SET_INDEX_TOP];
378         
379         for(int i=0; i<startSets.length; i++){
380              startSets[i]=dataInputStream.readChar();
381          }
382          char[] bmpTable = new char[canonStartSetsIndexes[NormalizerImpl.SET_INDEX_CANON_BMP_TABLE_LENGTH]];
383         for(int i=0; i<bmpTable.length; i++){
384              bmpTable[i]=dataInputStream.readChar();
385          }
386         char[] suppTable = new char[canonStartSetsIndexes[NormalizerImpl.SET_INDEX_CANON_SUPP_TABLE_LENGTH]];
387         for(int i=0; i<suppTable.length; i++){
388              suppTable[i]=dataInputStream.readChar();
389          }
390          canonStartSets[NormalizerImpl.CANON_SET_INDICIES_INDEX ] = canonStartSetsIndexes;
391          canonStartSets[NormalizerImpl.CANON_SET_START_SETS_INDEX] = startSets;
392          canonStartSets[NormalizerImpl.CANON_SET_BMP_TABLE_INDEX ] = bmpTable;
393          canonStartSets[NormalizerImpl.CANON_SET_SUPP_TABLE_INDEX] = suppTable;
394     }
395     
396     public byte[] getDataFormatVersion(){
397         return DATA_FORMAT_VERSION;
398     }
399     
400     public boolean isDataVersionAcceptable(byte version[])
401     {
402         return version[0] == DATA_FORMAT_VERSION[0]
403                && version[2] == DATA_FORMAT_VERSION[2]
404                && version[3] == DATA_FORMAT_VERSION[3];
405     }
406     
407     public byte[] getUnicodeVersion(){
408         return unicodeVersion;
409     }
410     // private data members -------------------------------------------------
411

412
413     /**
414     * ICU data file input stream
415     */

416     private DataInputStream dataInputStream;
417     
418     private byte[] unicodeVersion;
419                                        
420     /**
421     * File format version that this class understands.
422     * No guarantees are made if a older version is used
423     * see store.c of gennorm for more information and values
424     */

425     private static final byte DATA_FORMAT_ID[] = {(byte)0x4E, (byte)0x6F,
426                                                     (byte)0x72, (byte)0x6D};
427     private static final byte DATA_FORMAT_VERSION[] = {(byte)0x2, (byte)0x2,
428                                                         (byte)0x5, (byte)0x2};
429     
430 }
431
Popular Tags