DictionaryBasedBreakIterator.java
8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.text;
import com.adobe.agl.impl.Assert;
import com.adobe.agl.text.BreakDictionary;
import com.adobe.agl.text.RBBIDataWrapper;
import com.adobe.agl.text.RuleBasedBreakIterator;
import java.io.IOException;
import java.io.InputStream;
import java.text.CharacterIterator;
import java.util.Stack;
import java.util.Vector;
public class DictionaryBasedBreakIterator
extends RuleBasedBreakIterator {
private boolean usingCTDictionary = false;
private BreakDictionary dictionary;
int[] cachedBreakPositions;
int positionInCache;
protected DictionaryBasedBreakIterator(InputStream compiledRules) throws IOException {
this.fRData = RBBIDataWrapper.get(compiledRules);
this.dictionary = null;
this.usingCTDictionary = true;
}
public void setText(CharacterIterator newText) {
super.setText(newText);
this.cachedBreakPositions = null;
this.fDictionaryCharCount = 0;
this.positionInCache = 0;
}
public int first() {
this.cachedBreakPositions = null;
this.fDictionaryCharCount = 0;
this.positionInCache = 0;
return super.first();
}
public int last() {
this.cachedBreakPositions = null;
this.fDictionaryCharCount = 0;
this.positionInCache = 0;
return super.last();
}
public int previous() {
int nextResult;
CharacterIterator text = this.getText();
if (this.cachedBreakPositions != null && this.positionInCache > 0) {
--this.positionInCache;
text.setIndex(this.cachedBreakPositions[this.positionInCache]);
return this.cachedBreakPositions[this.positionInCache];
}
this.cachedBreakPositions = null;
int offset = this.current();
int result = super.previous();
if (this.cachedBreakPositions != null) {
this.positionInCache = this.cachedBreakPositions.length - 2;
return result;
}
while (result < offset && (nextResult = this.next()) < offset) {
result = nextResult;
}
if (this.cachedBreakPositions != null) {
this.positionInCache = this.cachedBreakPositions.length - 2;
}
if (result != -1) {
text.setIndex(result);
}
return result;
}
public int preceding(int offset) {
CharacterIterator text = this.getText();
DictionaryBasedBreakIterator.checkOffset(offset, text);
if (this.cachedBreakPositions == null || offset <= this.cachedBreakPositions[0] || offset > this.cachedBreakPositions[this.cachedBreakPositions.length - 1]) {
this.cachedBreakPositions = null;
return super.preceding(offset);
}
this.positionInCache = 0;
while (this.positionInCache < this.cachedBreakPositions.length && offset > this.cachedBreakPositions[this.positionInCache]) {
++this.positionInCache;
}
--this.positionInCache;
text.setIndex(this.cachedBreakPositions[this.positionInCache]);
return text.getIndex();
}
public int following(int offset) {
CharacterIterator text = this.getText();
DictionaryBasedBreakIterator.checkOffset(offset, text);
if (this.cachedBreakPositions == null || offset < this.cachedBreakPositions[0] || offset >= this.cachedBreakPositions[this.cachedBreakPositions.length - 1]) {
this.cachedBreakPositions = null;
return super.following(offset);
}
this.positionInCache = 0;
while (this.positionInCache < this.cachedBreakPositions.length && offset >= this.cachedBreakPositions[this.positionInCache]) {
++this.positionInCache;
}
text.setIndex(this.cachedBreakPositions[this.positionInCache]);
return text.getIndex();
}
protected int handleNext() {
CharacterIterator text = this.getText();
if (this.cachedBreakPositions == null || this.positionInCache == this.cachedBreakPositions.length - 1) {
int startPos = text.getIndex();
this.fDictionaryCharCount = 0;
int result = super.handleNext();
if (!this.usingCTDictionary && this.fDictionaryCharCount > 1 && result - startPos > 1) {
this.divideUpDictionaryRange(startPos, result);
} else {
this.cachedBreakPositions = null;
return result;
}
}
if (this.cachedBreakPositions != null) {
++this.positionInCache;
text.setIndex(this.cachedBreakPositions[this.positionInCache]);
return this.cachedBreakPositions[this.positionInCache];
}
Assert.assrt(false);
return -9999;
}
private void divideUpDictionaryRange(int startPos, int endPos) {
CharacterIterator text = this.getText();
text.setIndex(startPos);
int c = DictionaryBasedBreakIterator.CICurrent32(text);
while (!this.isDictionaryChar(c)) {
c = DictionaryBasedBreakIterator.CINext32(text);
}
Stack currentBreakPositions = new Stack();
Stack<Integer> possibleBreakPositions = new Stack<Integer>();
Vector<Object> wrongBreakPositions = new Vector<Object>();
int state = 0;
int farthestEndPoint = text.getIndex();
Stack bestBreakPositions = null;
c = DictionaryBasedBreakIterator.CICurrent32(text);
do {
if (this.dictionary.at(state, 0) == -1) {
possibleBreakPositions.push(new Integer(text.getIndex()));
}
if ((state = this.dictionary.at(state, (char)c) & 65535) == 65535) {
currentBreakPositions.push((Integer)new Integer(text.getIndex()));
break;
}
if (state == 0 || text.getIndex() >= endPos) {
if (text.getIndex() > farthestEndPoint) {
farthestEndPoint = text.getIndex();
bestBreakPositions = (Stack)currentBreakPositions.clone();
}
while (!possibleBreakPositions.isEmpty() && wrongBreakPositions.contains(possibleBreakPositions.peek())) {
possibleBreakPositions.pop();
}
if (possibleBreakPositions.isEmpty()) {
if (bestBreakPositions != null) {
currentBreakPositions = bestBreakPositions;
if (farthestEndPoint >= endPos) break;
text.setIndex(farthestEndPoint + 1);
} else {
if ((currentBreakPositions.size() == 0 || ((Integer)currentBreakPositions.peek()).intValue() != text.getIndex()) && text.getIndex() != startPos) {
currentBreakPositions.push(new Integer(text.getIndex()));
}
DictionaryBasedBreakIterator.CINext32(text);
currentBreakPositions.push(new Integer(text.getIndex()));
}
} else {
Integer temp = (Integer)possibleBreakPositions.pop();
Object temp2 = null;
while (!currentBreakPositions.isEmpty() && temp < (Integer)currentBreakPositions.peek()) {
temp2 = currentBreakPositions.pop();
wrongBreakPositions.addElement(temp2);
}
currentBreakPositions.push(temp);
text.setIndex((Integer)currentBreakPositions.peek());
}
c = DictionaryBasedBreakIterator.CICurrent32(text);
state = 0;
if (text.getIndex() < endPos) continue;
break;
}
c = DictionaryBasedBreakIterator.CINext32(text);
} while (true);
if (!currentBreakPositions.isEmpty()) {
currentBreakPositions.pop();
}
currentBreakPositions.push(new Integer(endPos));
this.cachedBreakPositions = new int[currentBreakPositions.size() + 1];
this.cachedBreakPositions[0] = startPos;
for (int i = 0; i < currentBreakPositions.size(); ++i) {
this.cachedBreakPositions[i + 1] = (Integer)currentBreakPositions.elementAt(i);
}
this.positionInCache = 0;
}
}