~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to editor/libsrc/org/netbeans/editor/ext/ExtFinderFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.editor.ext;
 
43
 
 
44
import javax.swing.text.BadLocationException;
 
45
import org.netbeans.editor.Analyzer;
 
46
import org.netbeans.editor.FinderFactory;
 
47
import org.netbeans.editor.BaseDocument;
 
48
import org.netbeans.editor.Utilities;
 
49
 
 
50
/**
 
51
* Various finders are located here.
 
52
*
 
53
* @author Miloslav Metelka
 
54
* @version 1.00
 
55
*/
 
56
 
 
57
public class ExtFinderFactory {
 
58
 
 
59
    /** Finder that collects the whole lines and calls
 
60
    * the <tt>lineFound()</tt> method that can do a local find.
 
61
    * !!! Udelat to poradne i s vice bufferama
 
62
    */
 
63
    public static abstract class LineFwdFinder extends FinderFactory.AbstractFinder {
 
64
 
 
65
        private int origStartPos;
 
66
 
 
67
        private int origLimitPos;
 
68
 
 
69
        public LineFwdFinder() {
 
70
        }
 
71
 
 
72
        public int adjustStartPos(BaseDocument doc, int startPos) {
 
73
            origStartPos = startPos;
 
74
            try {
 
75
                return Utilities.getRowStart(doc, startPos);
 
76
            } catch (BadLocationException e) {
 
77
                return startPos;
 
78
            }
 
79
        }
 
80
 
 
81
        public int adjustLimitPos(BaseDocument doc, int limitPos) {
 
82
            origLimitPos = limitPos;
 
83
            try {
 
84
                return Utilities.getRowEnd(doc, limitPos);
 
85
            } catch (BadLocationException e) {
 
86
                return limitPos;
 
87
            }
 
88
        }
 
89
 
 
90
        /** find function that must be defined by descendant */
 
91
        public int find(int bufferStartPos, char buffer[],
 
92
                        int offset1, int offset2, int reqPos, int limitPos) {
 
93
            int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
 
94
            while (true) {
 
95
                int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
 
96
                boolean lfFound = (lfOffset >= 0);
 
97
                if (!lfFound) {
 
98
                    lfOffset = offset2;
 
99
                }
 
100
 
 
101
                int lineOffset = lineFound(buffer, offset, lfOffset,
 
102
                                           Math.max(origStartPos - bufferStartPos, offset),
 
103
                                           Math.min(origLimitPos - bufferStartPos, lfOffset));
 
104
                if (lineOffset >= 0) {
 
105
                    found = true;
 
106
                    return bufferStartPos + offset + lineOffset;
 
107
                }
 
108
 
 
109
                if (lfFound) {
 
110
                    offset = lfOffset + 1; // skip '\n'
 
111
                } else {
 
112
                    break;
 
113
                }
 
114
            }
 
115
            return bufferStartPos + offset2;
 
116
        }
 
117
 
 
118
        /** Line was found and is present in the given buffer. The given
 
119
        * buffer is either the original buffer passed to the <tt>find()</tt>
 
120
        * or constructed buffer if the line is at the border of the previous
 
121
        * and next buffer.
 
122
        * @return non-negative number means the target string was found and
 
123
        *   the returned number is offset on the line where the string was found.
 
124
        *   Negative number means the target string was not found on the line
 
125
        *   and the search will continue with the next line.
 
126
        */
 
127
        protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
 
128
                                         int startOffset, int endOffset);
 
129
 
 
130
    }
 
131
 
 
132
    /** Finder that collects the whole lines and calls
 
133
    * the <tt>lineFound()</tt> method that can do a local find.
 
134
    * !!! Udelat to poradne i s vice bufferama
 
135
    */
 
136
    public static abstract class LineBwdFinder extends FinderFactory.AbstractFinder {
 
137
 
 
138
        private int origStartPos;
 
139
 
 
140
        private int origLimitPos;
 
141
 
 
142
        public LineBwdFinder() {
 
143
        }
 
144
 
 
145
        public int adjustStartPos(BaseDocument doc, int startPos) {
 
146
            origStartPos = startPos;
 
147
            try {
 
148
                return Utilities.getRowEnd(doc, startPos);
 
149
            } catch (BadLocationException e) {
 
150
                return startPos;
 
151
            }
 
152
        }
 
153
 
 
154
        public int adjustLimitPos(BaseDocument doc, int limitPos) {
 
155
            origLimitPos = limitPos;
 
156
            try {
 
157
                return Utilities.getRowStart(doc, limitPos);
 
158
            } catch (BadLocationException e) {
 
159
                return limitPos;
 
160
            }
 
161
        }
 
162
 
 
163
        /** find function that must be defined by descendant */
 
164
        public int find(int bufferStartPos, char buffer[],
 
165
                        int offset1, int offset2, int reqPos, int limitPos) {
 
166
            int offset = reqPos - bufferStartPos + 1; // !!! Udelat poradne s moznosti vice bufferu
 
167
            while (true) {
 
168
                boolean lfFound = false;
 
169
                int lfOffsetP1 = offset;
 
170
                while (lfOffsetP1 > offset1) {
 
171
                    if (buffer[--lfOffsetP1] == '\n') {
 
172
                        lfFound = true;
 
173
                        lfOffsetP1++; // past '\n'
 
174
                        break;
 
175
                    }
 
176
                }
 
177
                if (!lfFound) {
 
178
                    lfOffsetP1 = offset1;
 
179
                }
 
180
 
 
181
                int lineOffset = lineFound(buffer, lfOffsetP1, offset,
 
182
                                           Math.max(origLimitPos - bufferStartPos, lfOffsetP1),
 
183
                                           Math.min(origStartPos - bufferStartPos, offset));
 
184
                if (lineOffset >= 0) {
 
185
                    found = true;
 
186
                    return bufferStartPos + offset + lineOffset;
 
187
                }
 
188
 
 
189
                if (lfFound) {
 
190
                    offset = lfOffsetP1 - 1; // skip '\n'
 
191
                } else {
 
192
                    break;
 
193
                }
 
194
            }
 
195
            return bufferStartPos + offset1 - 1;
 
196
        }
 
197
 
 
198
        /** Line was found and is present in the given buffer. The given
 
199
        * buffer is either the original buffer passed to the <tt>find()</tt>
 
200
        * or constructed buffer if the line is at the border of the previous
 
201
        * and next buffer.
 
202
        * @return non-negative number means the target string was found and
 
203
        *   the returned number is offset on the line where the string was found.
 
204
        *   Negative number means the target string was not found on the line
 
205
        *   and the search will continue with the next line.
 
206
        */
 
207
        protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
 
208
                                         int startOffset, int endOffset);
 
209
 
 
210
    }
 
211
 
 
212
    /** Finder that collects the whole lines and calls
 
213
    * the <tt>lineFound()</tt> method that can do a local find.
 
214
    * !!! Udelat to poradne i s vice bufferama
 
215
    */
 
216
    public static abstract class LineBlocksFinder extends FinderFactory.AbstractBlocksFinder {
 
217
 
 
218
        private int origStartPos;
 
219
 
 
220
        private int origLimitPos;
 
221
 
 
222
        public LineBlocksFinder() {
 
223
        }
 
224
 
 
225
        public int adjustStartPos(BaseDocument doc, int startPos) {
 
226
            origStartPos = startPos;
 
227
            try {
 
228
                return Utilities.getRowStart(doc, startPos);
 
229
            } catch (BadLocationException e) {
 
230
                return startPos;
 
231
            }
 
232
        }
 
233
 
 
234
        public int adjustLimitPos(BaseDocument doc, int limitPos) {
 
235
            origLimitPos = limitPos;
 
236
            try {
 
237
                return Utilities.getRowEnd(doc, limitPos);
 
238
            } catch (BadLocationException e) {
 
239
                return limitPos;
 
240
            }
 
241
        }
 
242
 
 
243
        /** find function that must be defined by descendant */
 
244
        public int find(int bufferStartPos, char buffer[],
 
245
                        int offset1, int offset2, int reqPos, int limitPos) {
 
246
            int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
 
247
            while (true) {
 
248
                int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
 
249
                boolean lfFound = (lfOffset >= 0);
 
250
                if (!lfFound) {
 
251
                    lfOffset = offset2;
 
252
                }
 
253
 
 
254
                int lineOffset = lineFound(buffer, offset, lfOffset,
 
255
                                           Math.max(origStartPos - bufferStartPos, offset),
 
256
                                           Math.min(origLimitPos - bufferStartPos, lfOffset));
 
257
                if (lineOffset >= 0) {
 
258
                    found = true;
 
259
                    return bufferStartPos + offset + lineOffset;
 
260
                }
 
261
 
 
262
                if (lfFound) {
 
263
                    offset = lfOffset + 1; // skip '\n'
 
264
                } else {
 
265
                    break;
 
266
                }
 
267
            }
 
268
            return bufferStartPos + offset2;
 
269
        }
 
270
 
 
271
        /** Line was found and is present in the given buffer. The given
 
272
        * buffer is either the original buffer passed to the <tt>find()</tt>
 
273
        * or constructed buffer if the line is at the border of the previous
 
274
        * and next buffer.
 
275
        * @return non-negative number means the target string was found and
 
276
        *   the returned number is offset on the line where the string was found.
 
277
        *   Negative number means the target string was not found on the line
 
278
        *   and the search will continue with the next line.
 
279
        */
 
280
        protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
 
281
                                         int startOffset, int endOffset);
 
282
 
 
283
    }
 
284
 
 
285
 
 
286
 
 
287
}