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