2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.text;
\r
10 import java.io.BufferedInputStream;
\r
11 import java.io.DataInputStream;
\r
12 import java.io.IOException;
\r
13 import java.io.InputStream;
\r
15 import com.ibm.icu.impl.CharTrie;
\r
16 import com.ibm.icu.impl.Trie;
\r
19 * <p>Internal class used for Rule Based Break Iterators</p>
\r
20 * <p>This class provides access to the compiled break rule data, as
\r
21 * it is stored in a .brk file.
\r
23 final class RBBIDataWrapper {
\r
25 // These fields are the ready-to-use compiled rule data, as
\r
26 // read from the file.
\r
28 RBBIDataHeader fHeader;
\r
38 // Indexes to fields in the ICU4C style binary form of the RBBI Data Header
\r
39 // Used by the rule compiler when flattening the data.
\r
41 final static int DH_SIZE = 24;
\r
42 final static int DH_MAGIC = 0;
\r
43 final static int DH_FORMATVERSION = 1;
\r
44 final static int DH_LENGTH = 2;
\r
45 final static int DH_CATCOUNT = 3;
\r
46 final static int DH_FTABLE = 4;
\r
47 final static int DH_FTABLELEN = 5;
\r
48 final static int DH_RTABLE = 6;
\r
49 final static int DH_RTABLELEN = 7;
\r
50 final static int DH_SFTABLE = 8;
\r
51 final static int DH_SFTABLELEN = 9;
\r
52 final static int DH_SRTABLE = 10;
\r
53 final static int DH_SRTABLELEN = 11;
\r
54 final static int DH_TRIE = 12;
\r
55 final static int DH_TRIELEN = 13;
\r
56 final static int DH_RULESOURCE = 14;
\r
57 final static int DH_RULESOURCELEN = 15;
\r
58 final static int DH_STATUSTABLE = 16;
\r
59 final static int DH_STATUSTABLELEN = 17;
\r
62 // Index offsets to the fields in a state table row.
\r
63 // Corresponds to struct RBBIStateTableRow in the C version.
\r
65 final static int ACCEPTING = 0;
\r
66 final static int LOOKAHEAD = 1;
\r
67 final static int TAGIDX = 2;
\r
68 final static int RESERVED = 3;
\r
69 final static int NEXTSTATES = 4;
\r
71 // Index offsets to header fields of a state table
\r
72 // struct RBBIStateTable {... in the C version.
\r
74 final static int NUMSTATES = 0;
\r
75 final static int ROWLEN = 2;
\r
76 final static int FLAGS = 4;
\r
77 final static int RESERVED_2 = 6;
\r
78 final static int ROW_DATA = 8;
\r
80 // Bit selectors for the "FLAGS" field of the state table header
\r
81 // enum RBBIStateTableFlags in the C version.
\r
83 final static int RBBI_LOOKAHEAD_HARD_BREAK = 1;
\r
84 final static int RBBI_BOF_REQUIRED = 2;
\r
87 * Data Header. A struct-like class with the fields from the RBBI data file header.
\r
89 final static class RBBIDataHeader {
\r
90 int fMagic; // == 0xbla0
\r
91 int fVersion; // == 1 (for ICU 3.2 and earlier.
\r
92 byte[] fFormatVersion; // For ICU 3.4 and later.
\r
93 int fLength; // Total length in bytes of this RBBI Data,
\r
94 // including all sections, not just the header.
\r
95 int fCatCount; // Number of character categories.
\r
98 // Offsets and sizes of each of the subsections within the RBBI data.
\r
99 // All offsets are bytes from the start of the RBBIDataHeader.
\r
100 // All sizes are in bytes.
\r
102 int fFTable; // forward state transition table.
\r
104 int fRTable; // Offset to the reverse state transition table.
\r
106 int fSFTable; // safe point forward transition table
\r
108 int fSRTable; // safe point reverse transition table
\r
110 int fTrie; // Offset to Trie data for character categories
\r
112 int fRuleSource; // Offset to the source for for the break
\r
113 int fRuleSourceLen; // rules. Stored UChar *.
\r
114 int fStatusTable; // Offset to the table of rule status values
\r
115 int fStatusTableLen;
\r
117 public RBBIDataHeader() {
\r
119 fFormatVersion = new byte[4];
\r
125 * RBBI State Table Indexing Function. Given a state number, return the
\r
126 * array index of the start of the state table row for that state.
\r
129 int getRowIndex(int state){
\r
130 return ROW_DATA + state * (fHeader.fCatCount + 4);
\r
133 static class TrieFoldingFunc implements Trie.DataManipulate {
\r
134 public int getFoldingOffset(int data) {
\r
135 if ((data & 0x8000) != 0) {
\r
136 return data & 0x7fff;
\r
142 static TrieFoldingFunc fTrieFoldingFunc = new TrieFoldingFunc();
\r
145 RBBIDataWrapper() {
\r
149 * Get an RBBIDataWrapper from an InputStream onto a pre-compiled set
\r
152 static RBBIDataWrapper get(InputStream is) throws IOException {
\r
155 DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
\r
156 RBBIDataWrapper This = new RBBIDataWrapper();
\r
158 // Seek past the ICU data header.
\r
159 // TODO: verify that the header looks good.
\r
162 // Read in the RBBI data header...
\r
163 This.fHeader = new RBBIDataHeader();
\r
164 This.fHeader.fMagic = dis.readInt();
\r
165 This.fHeader.fVersion = dis.readInt();
\r
166 This.fHeader.fFormatVersion[0] = (byte) (This.fHeader.fVersion >> 24);
\r
167 This.fHeader.fFormatVersion[1] = (byte) (This.fHeader.fVersion >> 16);
\r
168 This.fHeader.fFormatVersion[2] = (byte) (This.fHeader.fVersion >> 8);
\r
169 This.fHeader.fFormatVersion[3] = (byte) (This.fHeader.fVersion);
\r
170 This.fHeader.fLength = dis.readInt();
\r
171 This.fHeader.fCatCount = dis.readInt();
\r
172 This.fHeader.fFTable = dis.readInt();
\r
173 This.fHeader.fFTableLen = dis.readInt();
\r
174 This.fHeader.fRTable = dis.readInt();
\r
175 This.fHeader.fRTableLen = dis.readInt();
\r
176 This.fHeader.fSFTable = dis.readInt();
\r
177 This.fHeader.fSFTableLen = dis.readInt();
\r
178 This.fHeader.fSRTable = dis.readInt();
\r
179 This.fHeader.fSRTableLen = dis.readInt();
\r
180 This.fHeader.fTrie = dis.readInt();
\r
181 This.fHeader.fTrieLen = dis.readInt();
\r
182 This.fHeader.fRuleSource = dis.readInt();
\r
183 This.fHeader.fRuleSourceLen = dis.readInt();
\r
184 This.fHeader.fStatusTable = dis.readInt();
\r
185 This.fHeader.fStatusTableLen = dis.readInt();
\r
186 dis.skip(6 * 4); // uint32_t fReserved[6];
\r
189 if (This.fHeader.fMagic != 0xb1a0 ||
\r
190 ! (This.fHeader.fVersion == 1 || // ICU 3.2 and earlier
\r
191 This.fHeader.fFormatVersion[0] == 3) // ICU 3.4
\r
193 throw new IOException("Break Iterator Rule Data Magic Number Incorrect, or unsupported data version.");
\r
196 // Current position in input stream.
\r
197 int pos = 24 * 4; // offset of end of header, which has 24 fields, all int32_t (4 bytes)
\r
200 // Read in the Forward state transition table as an array of shorts.
\r
203 // Quick Sanity Check
\r
204 if (This.fHeader.fFTable < pos || This.fHeader.fFTable > This.fHeader.fLength) {
\r
205 throw new IOException("Break iterator Rule data corrupt");
\r
208 // Skip over any padding preceding this table
\r
209 dis.skip(This.fHeader.fFTable - pos);
\r
210 pos = This.fHeader.fFTable;
\r
212 This.fFTable = new short[This.fHeader.fFTableLen / 2];
\r
213 for ( i=0; i<This.fFTable.length; i++) {
\r
214 This.fFTable[i] = dis.readShort();
\r
219 // Read in the Reverse state table
\r
222 // Skip over any padding in the file
\r
223 dis.skip(This.fHeader.fRTable - pos);
\r
224 pos = This.fHeader.fRTable;
\r
226 // Create & fill the table itself.
\r
227 This.fRTable = new short[This.fHeader.fRTableLen / 2];
\r
228 for (i=0; i<This.fRTable.length; i++) {
\r
229 This.fRTable[i] = dis.readShort();
\r
234 // Read in the Safe Forward state table
\r
236 if (This.fHeader.fSFTableLen > 0) {
\r
237 // Skip over any padding in the file
\r
238 dis.skip(This.fHeader.fSFTable - pos);
\r
239 pos = This.fHeader.fSFTable;
\r
241 // Create & fill the table itself.
\r
242 This.fSFTable = new short[This.fHeader.fSFTableLen / 2];
\r
243 for (i=0; i<This.fSFTable.length; i++) {
\r
244 This.fSFTable[i] = dis.readShort();
\r
250 // Read in the Safe Reverse state table
\r
252 if (This.fHeader.fSRTableLen > 0) {
\r
253 // Skip over any padding in the file
\r
254 dis.skip(This.fHeader.fSRTable - pos);
\r
255 pos = This.fHeader.fSRTable;
\r
257 // Create & fill the table itself.
\r
258 This.fSRTable = new short[This.fHeader.fSRTableLen / 2];
\r
259 for (i=0; i<This.fSRTable.length; i++) {
\r
260 This.fSRTable[i] = dis.readShort();
\r
266 // Unserialize the Character categories TRIE
\r
267 // Because we can't be absolutely certain where the Trie deserialize will
\r
268 // leave the input stream, leave position unchanged.
\r
269 // The seek to the start of the next item following the TRIE will get us
\r
272 dis.skip(This.fHeader.fTrie - pos); // seek input stream from end of previous section to
\r
273 pos = This.fHeader.fTrie; // to the start of the trie
\r
275 dis.mark(This.fHeader.fTrieLen+100); // Mark position of start of TRIE in the input
\r
276 // and tell Java to keep the mark valid so long
\r
277 // as we don't go more than 100 bytes past the
\r
278 // past the end of the TRIE.
\r
280 This.fTrie = new CharTrie(dis, fTrieFoldingFunc); // Deserialize the TRIE, leaving input
\r
281 // stream at an unknown position, preceding the
\r
282 // padding between TRIE and following section.
\r
284 dis.reset(); // Move input stream back to marked position at
\r
285 // the start of the serialized TRIE. Now our
\r
286 // "pos" variable and the input stream are in
\r
290 // Read the Rule Status Table
\r
292 if (pos > This.fHeader.fStatusTable) {
\r
293 throw new IOException("Break iterator Rule data corrupt");
\r
295 dis.skip(This.fHeader.fStatusTable - pos);
\r
296 pos = This.fHeader.fStatusTable;
\r
297 This.fStatusTable = new int[This.fHeader.fStatusTableLen / 4];
\r
298 for (i=0; i<This.fStatusTable.length; i++) {
\r
299 This.fStatusTable[i] = dis.readInt();
\r
304 // Put the break rule source into a String
\r
306 if (pos > This.fHeader.fRuleSource) {
\r
307 throw new IOException("Break iterator Rule data corrupt");
\r
309 dis.skip(This.fHeader.fRuleSource - pos);
\r
310 pos = This.fHeader.fRuleSource;
\r
311 StringBuilder sb = new StringBuilder(This.fHeader.fRuleSourceLen / 2);
\r
312 for (i=0; i<This.fHeader.fRuleSourceLen; i+=2) {
\r
313 sb.append(dis.readChar());
\r
316 This.fRuleSource = sb.toString();
\r
318 if (RuleBasedBreakIterator.fDebugEnv!=null && RuleBasedBreakIterator.fDebugEnv.indexOf("data")>=0) {
\r
325 // Getters for fields from the state table header
\r
327 final static int getNumStates(short table[]) {
\r
328 int hi = table[NUMSTATES];
\r
329 int lo = table[NUMSTATES+1];
\r
330 int val = (hi<<16) + (lo&0x0000ffff);
\r
336 /* Debug function to display the break iterator data. */
\r
338 System.out.println("RBBI Data Wrapper dump ...");
\r
339 System.out.println();
\r
340 System.out.println("Forward State Table");
\r
341 dumpTable(fFTable);
\r
342 System.out.println("Reverse State Table");
\r
343 dumpTable(fRTable);
\r
344 System.out.println("Forward Safe Points Table");
\r
345 dumpTable(fSFTable);
\r
346 System.out.println("Reverse Safe Points Table");
\r
347 dumpTable(fSRTable);
\r
349 dumpCharCategories();
\r
350 System.out.println("Source Rules: " + fRuleSource);
\r
356 /* Fixed width int-to-string conversion. */
\r
357 static public String intToString(int n, int width) {
\r
358 StringBuilder dest = new StringBuilder(width);
\r
360 while (dest.length() < width) {
\r
361 dest.insert(0, ' ');
\r
363 return dest.toString();
\r
368 /* Fixed width int-to-string conversion. */
\r
369 static public String intToHexString(int n, int width) {
\r
370 StringBuilder dest = new StringBuilder(width);
\r
371 dest.append(Integer.toHexString(n));
\r
372 while (dest.length() < width) {
\r
373 dest.insert(0, ' ');
\r
375 return dest.toString();
\r
380 /** Dump a state table. (A full set of RBBI rules has 4 state tables.) */
\r
381 private void dumpTable(short table[]) {
\r
382 if (table == null) {
\r
383 System.out.println(" -- null -- ");
\r
387 String header = " Row Acc Look Tag";
\r
388 for (n=0; n<fHeader.fCatCount; n++) {
\r
389 header += intToString(n, 5);
\r
391 System.out.println(header);
\r
392 for (n=0; n<header.length(); n++) {
\r
393 System.out.print("-");
\r
395 System.out.println();
\r
396 for (state=0; state< getNumStates(table); state++) {
\r
397 dumpRow(table, state);
\r
399 System.out.println();
\r
406 * Dump (for debug) a single row of an RBBI state table
\r
410 private void dumpRow(short table[], int state) {
\r
411 StringBuilder dest = new StringBuilder(fHeader.fCatCount*5 + 20);
\r
412 dest.append(intToString(state, 4));
\r
413 int row = getRowIndex(state);
\r
414 if (table[row+ACCEPTING] != 0) {
\r
415 dest.append(intToString(table[row+ACCEPTING], 5));
\r
419 if (table[row+LOOKAHEAD] != 0) {
\r
420 dest.append(intToString(table[row+LOOKAHEAD], 5));
\r
424 dest.append(intToString(table[row+TAGIDX], 5));
\r
426 for (int col=0; col<fHeader.fCatCount; col++) {
\r
427 dest.append(intToString(table[row+NEXTSTATES+col], 5));
\r
430 System.out.println(dest);
\r
435 private void dumpCharCategories() {
\r
436 int n = fHeader.fCatCount;
\r
437 String catStrings[] = new String[n+1];
\r
438 int rangeStart = 0;
\r
443 int lastNewline[] = new int[n+1];
\r
445 for (category = 0; category <= fHeader.fCatCount; category ++) {
\r
446 catStrings[category] = "";
\r
448 System.out.println("\nCharacter Categories");
\r
449 System.out.println("--------------------");
\r
450 for (char32 = 0; char32<=0x10ffff; char32++) {
\r
451 category = fTrie.getCodePointValue(char32);
\r
452 category &= ~0x4000; // Mask off dictionary bit.
\r
453 if (category < 0 || category > fHeader.fCatCount) {
\r
454 System.out.println("Error, bad category " + Integer.toHexString(category) +
\r
455 " for char " + Integer.toHexString(char32));
\r
458 if (category == lastCat ) {
\r
459 rangeEnd = char32;
\r
461 if (lastCat >= 0) {
\r
462 if (catStrings[lastCat].length() > lastNewline[lastCat] + 70) {
\r
463 lastNewline[lastCat] = catStrings[lastCat].length() + 10;
\r
464 catStrings[lastCat] += "\n ";
\r
467 catStrings[lastCat] += " " + Integer.toHexString(rangeStart);
\r
468 if (rangeEnd != rangeStart) {
\r
469 catStrings[lastCat] += "-" + Integer.toHexString(rangeEnd);
\r
472 lastCat = category;
\r
473 rangeStart = rangeEnd = char32;
\r
476 catStrings[lastCat] += " " + Integer.toHexString(rangeStart);
\r
477 if (rangeEnd != rangeStart) {
\r
478 catStrings[lastCat] += "-" + Integer.toHexString(rangeEnd);
\r
481 for (category = 0; category <= fHeader.fCatCount; category ++) {
\r
482 System.out.println (intToString(category, 5) + " " + catStrings[category]);
\r
484 System.out.println();
\r
488 /*static RBBIDataWrapper get(String name) throws IOException {
\r
489 String fullName = "data/" + name;
\r
490 InputStream is = ICUData.getRequiredStream(fullName);
\r
494 public static void main(String[] args) {
\r
496 if (args.length == 0) {
\r
501 System.out.println("RBBIDataWrapper.main(" + s + ") ");
\r
503 String versionedName = ICUResourceBundle.ICU_BUNDLE+"/"+ s + ".brk";
\r
506 RBBIDataWrapper This = RBBIDataWrapper.get(versionedName);
\r
509 catch (Exception e) {
\r
510 System.out.println("Exception: " + e.toString());
\r