1
15
16 package gate.corpora;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20
21 import gate.util.Out;
22
23
30
31 public class RepositioningInfo extends ArrayList {
32
33
34 static final long serialVersionUID = -2895662600168468559L;
35
36 private static final boolean DEBUG = false;
37
38
41 public class PositionInfo implements Serializable {
42
43
44 static final long serialVersionUID = -7747351720249898499L;
45
46
47 private long m_origPos, m_origLength, m_currPos, m_currLength;
48
49
50 public PositionInfo(long orig, long origLen, long curr, long currLen) {
51 m_origPos = orig;
52 m_origLength = origLen;
53 m_currPos = curr;
54 m_currLength = currLen;
55 }
57
58 public long getCurrentPosition() {
59 return m_currPos;
60 }
62
63 public long getOriginalPosition() {
64 return m_origPos;
65 }
67
68 public long getOriginalLength() {
69 return m_origLength;
70 }
72
73 public long getCurrentLength() {
74 return m_currLength;
75 }
77
78 public String toString() {
79 return "("+m_origPos+","+m_origLength+","
80 +m_currPos+","+m_currLength+")";
81 } }
84
85 public RepositioningInfo() {
86 super();
87 }
89
90 public void addPositionInfo(long origPos, long origLength,
91 long currPos, long currLength) {
92 int insertPos = 0;
94 PositionInfo lastPI;
95
96 for(int i = size(); i>0; i--) {
97 lastPI = (PositionInfo) get(i-1);
98 if(lastPI.getOriginalPosition() < origPos) {
99 insertPos = i;
100 break;
101 } }
104 add(insertPos, new PositionInfo(origPos, origLength, currPos, currLength));
105 }
107
110 public long getExtractedPos(long absPos) {
111 long result = absPos;
112 PositionInfo currPI = null;
113 int size = size();
114
115 if(size != 0) {
116 long origPos, origLen;
117 boolean found = false;
118
119 for(int i=0; i<size; ++i) {
120 currPI = (PositionInfo) get(i);
121 origPos = currPI.getOriginalPosition();
122 origLen = currPI.getOriginalLength();
123
124 if(absPos <= origPos+origLen) {
125 if(absPos < origPos) {
126 result = -1;
128 }
129 else {
130 result = currPI.getCurrentPosition() + absPos - origPos;
132 } found = true;
134 break;
135 } }
138 if(!found) {
139 result = -1;
141 } }
144 return result;
145 }
147 public long getOriginalPos(long relPos) {
148 return getOriginalPos(relPos, false);
149 }
151
154 public long getOriginalPos(long relPos, boolean afterChar) {
155 long result = relPos;
156 PositionInfo currPI = null;
157 int size = size();
158
159 if(size != 0) {
160 long currPos, currLen;
161 boolean found = false;
162
163 for(int i=0; i<size; ++i) {
164 currPI = (PositionInfo) get(i);
165 currPos = currPI.getCurrentPosition();
166 currLen = currPI.getCurrentLength();
167
168 if(afterChar && relPos == currPos+currLen) {
169 result = currPI.getOriginalPosition() + currPI.getOriginalLength();
170 found = true;
171 break;
172 }
174 if(relPos < currPos+currLen) {
175 if(relPos < currPos) {
176 result = -1;
178 }
179 else {
180 result = currPI.getOriginalPosition() + relPos - currPos;
182 } found = true;
184 break;
185 } }
188 if(!found) {
189 result = -1;
191 } }
194 return result;
195 }
197
198 public long getExtractedPosFlow(long absPos) {
199 long result = -1;
200 return result;
201 }
203
204 public long getOriginalPosFlow(long relPos) {
205 long result = -1;
206 return result;
207 }
209
213 public int getIndexByOriginalPosition(long absPos) {
214 PositionInfo currPI = null;
215 int result = -1;
216
217 int size = size();
218 long origPos, origLen;
219
220 for(int i=0; i<size; ++i) {
222 currPI = (PositionInfo) get(i);
223 origPos = currPI.getOriginalPosition();
224 origLen = currPI.getOriginalLength();
225
226 if(absPos <= origPos+origLen) {
227 if(absPos >= origPos) {
228 result = i;
229 } break;
231 } }
234 return result;
235 }
237
243 public int getIndexByOriginalPositionFlow(long absPos) {
244 PositionInfo currPI = null;
245
246 int size = size();
247 int result = size;
248 long origPos, origLen;
249
250 for(int i=0; i<size; ++i) {
252 currPI = (PositionInfo) get(i);
253 origPos = currPI.getOriginalPosition();
254 origLen = currPI.getOriginalLength();
255
256 if(absPos <= origPos+origLen) {
257 if(absPos >= origPos) {
259 result = i;
260 }
261 else {
262 result = i-1;
264 } break;
266 } }
269 return result;
270 }
272
298 public void correctInformation(long originalPos, long origLen, long newLen) {
299 PositionInfo currPI;
300 PositionInfo frontPI, correctPI, endPI;
301
302 int index = getIndexByOriginalPositionFlow(originalPos);
303
304 if(index == -1) {
306 index = 0;
307 }
309
313 for(int i=index; i<size(); ++i) {
314 currPI = (PositionInfo) get(i);
315 currPI.m_currPos -= origLen - newLen;
316 }
318 currPI = (PositionInfo) get(index);
319 if(originalPos >= currPI.m_origPos
320 && currPI.m_origPos + currPI.m_origLength >= originalPos + origLen) {
321 long frontLen = originalPos - currPI.m_origPos;
322
323 frontPI = new PositionInfo(currPI.m_origPos,
324 frontLen,
325 currPI.m_currPos,
326 frontLen);
327 correctPI = new PositionInfo(originalPos,
328 origLen,
329 currPI.m_currPos + frontLen,
330 newLen);
331 long endLen = currPI.m_origLength - frontLen - origLen;
332 endPI = new PositionInfo(originalPos + origLen,
333 endLen,
334 currPI.m_currPos + frontLen + newLen,
335 endLen);
336
337 set(index, frontPI); if(endPI.m_origLength > 0) {
339 add(index+1, endPI); } if(correctPI.m_origLength > 0) {
342 add(index+1, correctPI); } } }
347
352 public void correctInformationOriginalMove(long originalPos, long moveLen) {
353 PositionInfo currPI;
354
355 if(DEBUG) {
356 if(originalPos < 380) Out.println("Before correction: "+this);
358 }
360 int index = getIndexByOriginalPositionFlow(originalPos);
361
362 if(index == -1) {
364 index = 0;
365 }
367 if(index == size()) {
369 return;
370 }
372 for(int i = index+1; i<size(); ++i) {
373 currPI = (PositionInfo) get(i);
374 currPI.m_origPos += moveLen;
375 }
377 currPI = (PositionInfo) get(index);
378
379 if(originalPos > currPI.m_origPos) {
381 if(originalPos < currPI.m_origPos + currPI.m_origLength) {
382 PositionInfo frontPI, endPI;
383 long frontLen = originalPos - currPI.m_origPos;
384 frontPI = new PositionInfo(currPI.m_origPos,
385 frontLen,
386 currPI.m_currPos,
387 frontLen);
388
389 long endLen = currPI.m_origLength - frontLen;
390 endPI = new PositionInfo(originalPos + moveLen,
391 endLen,
392 currPI.m_currPos + frontLen,
393 endLen);
394 set(index, frontPI); if(endPI.m_origLength != 0) {
396 add(index+1, endPI); }
399 if(DEBUG) {
400 if(originalPos < 380) { Out.println("Point 2. Current: "+currPI);
402 Out.println("Point 2. frontPI: "+frontPI);
403 Out.println("Point 2. endPI: "+endPI);
404 }
405 } } } else {
409 currPI.m_origPos += moveLen;
411 }
412
413 if(DEBUG) {
414 if(originalPos < 380) {
415 Out.println("Correction move: "+originalPos+", "+moveLen);
416 Out.println("Corrected: "+this);
417 Out.println("index: "+index);
418
423 }
424 } }
427 }