001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor.ext;
015:
016: import javax.swing.text.BadLocationException;
017:
018: import org.netbeans.editor.Analyzer;
019: import org.netbeans.editor.BaseDocument;
020: import org.netbeans.editor.FinderFactory;
021: import org.netbeans.editor.Utilities;
022:
023: /**
024: * Various finders are located here.
025: *
026: * @author Miloslav Metelka
027: * @version 1.00
028: */
029:
030: public class ExtFinderFactory {
031:
032: /**
033: * Finder that collects the whole lines and calls the <tt>lineFound()</tt>
034: * method that can do a local find. !!! Udelat to poradne i s vice bufferama
035: */
036: public static abstract class LineFwdFinder extends
037: FinderFactory.AbstractFinder {
038:
039: private char[] lineBuffer = Analyzer.EMPTY_CHAR_ARRAY;
040:
041: private int lineLen;
042:
043: private int origStartPos;
044:
045: private int origLimitPos;
046:
047: public LineFwdFinder() {
048: }
049:
050: public int adjustStartPos(BaseDocument doc, int startPos) {
051: origStartPos = startPos;
052: try {
053: return Utilities.getRowStart(doc, startPos);
054: } catch (BadLocationException e) {
055: return startPos;
056: }
057: }
058:
059: public int adjustLimitPos(BaseDocument doc, int limitPos) {
060: origLimitPos = limitPos;
061: try {
062: return Utilities.getRowEnd(doc, limitPos);
063: } catch (BadLocationException e) {
064: return limitPos;
065: }
066: }
067:
068: /** find function that must be defined by descendant */
069: public int find(int bufferStartPos, char buffer[], int offset1,
070: int offset2, int reqPos, int limitPos) {
071: int offset = reqPos - bufferStartPos; // !!! Udelat poradne s
072: // moznosti vice bufferu
073: while (true) {
074: int lfOffset = Analyzer.findFirstLFOffset(buffer,
075: offset, offset2 - offset);
076: boolean lfFound = (lfOffset >= 0);
077: if (!lfFound) {
078: lfOffset = offset2;
079: }
080:
081: int lineOffset = lineFound(
082: buffer,
083: offset,
084: lfOffset,
085: Math.max(origStartPos - bufferStartPos, offset),
086: Math.min(origLimitPos - bufferStartPos,
087: lfOffset));
088: if (lineOffset >= 0) {
089: found = true;
090: return bufferStartPos + offset + lineOffset;
091: }
092:
093: if (lfFound) {
094: offset = lfOffset + 1; // skip '\n'
095: } else {
096: break;
097: }
098: }
099: return bufferStartPos + offset2;
100: }
101:
102: /**
103: * Line was found and is present in the given buffer. The given buffer
104: * is either the original buffer passed to the <tt>find()</tt> or
105: * constructed buffer if the line is at the border of the previous and
106: * next buffer.
107: *
108: * @return non-negative number means the target string was found and the
109: * returned number is offset on the line where the string was
110: * found. Negative number means the target string was not found
111: * on the line and the search will continue with the next line.
112: */
113: protected abstract int lineFound(char[] buffer,
114: int lineStartOffset, int lineEndOffset,
115: int startOffset, int endOffset);
116:
117: }
118:
119: /**
120: * Finder that collects the whole lines and calls the <tt>lineFound()</tt>
121: * method that can do a local find. !!! Udelat to poradne i s vice bufferama
122: */
123: public static abstract class LineBwdFinder extends
124: FinderFactory.AbstractFinder {
125:
126: private char[] lineBuffer = Analyzer.EMPTY_CHAR_ARRAY;
127:
128: private int lineLen;
129:
130: private int origStartPos;
131:
132: private int origLimitPos;
133:
134: public LineBwdFinder() {
135: }
136:
137: public int adjustStartPos(BaseDocument doc, int startPos) {
138: origStartPos = startPos;
139: try {
140: return Utilities.getRowEnd(doc, startPos);
141: } catch (BadLocationException e) {
142: return startPos;
143: }
144: }
145:
146: public int adjustLimitPos(BaseDocument doc, int limitPos) {
147: origLimitPos = limitPos;
148: try {
149: return Utilities.getRowStart(doc, limitPos);
150: } catch (BadLocationException e) {
151: return limitPos;
152: }
153: }
154:
155: /** find function that must be defined by descendant */
156: public int find(int bufferStartPos, char buffer[], int offset1,
157: int offset2, int reqPos, int limitPos) {
158: int offset = reqPos - bufferStartPos + 1; // !!! Udelat poradne s
159: // moznosti vice bufferu
160: while (true) {
161: boolean lfFound = false;
162: int lfOffsetP1 = offset;
163: while (lfOffsetP1 > offset1) {
164: if (buffer[--lfOffsetP1] == '\n') {
165: lfFound = true;
166: lfOffsetP1++; // past '\n'
167: break;
168: }
169: }
170: if (!lfFound) {
171: lfOffsetP1 = offset1;
172: }
173:
174: int lineOffset = lineFound(buffer, lfOffsetP1, offset,
175: Math.max(origLimitPos - bufferStartPos,
176: lfOffsetP1), Math.min(origStartPos
177: - bufferStartPos, offset));
178: if (lineOffset >= 0) {
179: found = true;
180: return bufferStartPos + offset + lineOffset;
181: }
182:
183: if (lfFound) {
184: offset = lfOffsetP1 - 1; // skip '\n'
185: } else {
186: break;
187: }
188: }
189: return bufferStartPos + offset1 - 1;
190: }
191:
192: /**
193: * Line was found and is present in the given buffer. The given buffer
194: * is either the original buffer passed to the <tt>find()</tt> or
195: * constructed buffer if the line is at the border of the previous and
196: * next buffer.
197: *
198: * @return non-negative number means the target string was found and the
199: * returned number is offset on the line where the string was
200: * found. Negative number means the target string was not found
201: * on the line and the search will continue with the next line.
202: */
203: protected abstract int lineFound(char[] buffer,
204: int lineStartOffset, int lineEndOffset,
205: int startOffset, int endOffset);
206:
207: }
208:
209: /**
210: * Finder that collects the whole lines and calls the <tt>lineFound()</tt>
211: * method that can do a local find. !!! Udelat to poradne i s vice bufferama
212: */
213: public static abstract class LineBlocksFinder extends
214: FinderFactory.AbstractBlocksFinder {
215:
216: private char[] lineBuffer = Analyzer.EMPTY_CHAR_ARRAY;
217:
218: private int lineLen;
219:
220: private int origStartPos;
221:
222: private int origLimitPos;
223:
224: public LineBlocksFinder() {
225: }
226:
227: public int adjustStartPos(BaseDocument doc, int startPos) {
228: origStartPos = startPos;
229: try {
230: return Utilities.getRowStart(doc, startPos);
231: } catch (BadLocationException e) {
232: return startPos;
233: }
234: }
235:
236: public int adjustLimitPos(BaseDocument doc, int limitPos) {
237: origLimitPos = limitPos;
238: try {
239: return Utilities.getRowEnd(doc, limitPos);
240: } catch (BadLocationException e) {
241: return limitPos;
242: }
243: }
244:
245: /** find function that must be defined by descendant */
246: public int find(int bufferStartPos, char buffer[], int offset1,
247: int offset2, int reqPos, int limitPos) {
248: int offset = reqPos - bufferStartPos; // !!! Udelat poradne s
249: // moznosti vice bufferu
250: while (true) {
251: int lfOffset = Analyzer.findFirstLFOffset(buffer,
252: offset, offset2 - offset);
253: boolean lfFound = (lfOffset >= 0);
254: if (!lfFound) {
255: lfOffset = offset2;
256: }
257:
258: int lineOffset = lineFound(
259: buffer,
260: offset,
261: lfOffset,
262: Math.max(origStartPos - bufferStartPos, offset),
263: Math.min(origLimitPos - bufferStartPos,
264: lfOffset));
265: if (lineOffset >= 0) {
266: found = true;
267: return bufferStartPos + offset + lineOffset;
268: }
269:
270: if (lfFound) {
271: offset = lfOffset + 1; // skip '\n'
272: } else {
273: break;
274: }
275: }
276: return bufferStartPos + offset2;
277: }
278:
279: /**
280: * Line was found and is present in the given buffer. The given buffer
281: * is either the original buffer passed to the <tt>find()</tt> or
282: * constructed buffer if the line is at the border of the previous and
283: * next buffer.
284: *
285: * @return non-negative number means the target string was found and the
286: * returned number is offset on the line where the string was
287: * found. Negative number means the target string was not found
288: * on the line and the search will continue with the next line.
289: */
290: protected abstract int lineFound(char[] buffer,
291: int lineStartOffset, int lineEndOffset,
292: int startOffset, int endOffset);
293:
294: }
295:
296: }
|