KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaWordIterator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13 import com.ibm.icu.text.BreakIterator;
14 import java.text.CharacterIterator JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18
19
20 /**
21  * Breaks java text into word starts, also stops at line start and end. No
22  * direction dependency.
23  *
24  * @since 3.0
25  */

26 public class JavaWordIterator extends BreakIterator {
27
28     /**
29      * The underlying java break iterator. It returns all breaks, including
30      * before and after every whitespace.
31      */

32     private JavaBreakIterator fIterator;
33     /** The current index for the stateful operations. */
34     private int fIndex;
35
36     /**
37      * Creates a new word iterator.
38      */

39     public JavaWordIterator() {
40         fIterator= new JavaBreakIterator();
41         first();
42     }
43
44     /*
45      * @see java.text.BreakIterator#first()
46      */

47     public int first() {
48         fIndex= fIterator.first();
49         return fIndex;
50     }
51
52     /*
53      * @see java.text.BreakIterator#last()
54      */

55     public int last() {
56         fIndex= fIterator.last();
57         return fIndex;
58     }
59
60     /*
61      * @see java.text.BreakIterator#next(int)
62      */

63     public int next(int n) {
64         int next= 0;
65         while (--n > 0 && next != DONE) {
66             next= next();
67         }
68         return next;
69     }
70
71     /*
72      * @see java.text.BreakIterator#next()
73      */

74     public int next() {
75         fIndex= following(fIndex);
76         return fIndex;
77     }
78
79     /*
80      * @see java.text.BreakIterator#previous()
81      */

82     public int previous() {
83         fIndex= preceding(fIndex);
84         return fIndex;
85     }
86
87
88     /*
89      * @see java.text.BreakIterator#preceding(int)
90      */

91     public int preceding(int offset) {
92         int first= fIterator.preceding(offset);
93         if (isWhitespace(first, offset)) {
94             int second= fIterator.preceding(first);
95             if (second != DONE && !isDelimiter(second, first))
96                 return second;
97         }
98         return first;
99     }
100
101     /*
102      * @see java.text.BreakIterator#following(int)
103      */

104     public int following(int offset) {
105         int first= fIterator.following(offset);
106         if (eatFollowingWhitespace(offset, first)) {
107             int second= fIterator.following(first);
108             if (isWhitespace(first, second))
109                 return second;
110         }
111         return first;
112     }
113
114     private boolean eatFollowingWhitespace(int offset, int exclusiveEnd) {
115         if (exclusiveEnd == DONE || offset == DONE)
116             return false;
117
118         if (isWhitespace(offset, exclusiveEnd))
119             return false;
120         if (isDelimiter(offset, exclusiveEnd))
121             return false;
122
123         return true;
124     }
125
126     /**
127      * Returns <code>true</code> if the given sequence into the underlying text
128      * represents a delimiter, <code>false</code> otherwise.
129      *
130      * @param offset the offset
131      * @param exclusiveEnd the end offset
132      * @return <code>true</code> if the given range is a delimiter
133      */

134     private boolean isDelimiter(int offset, int exclusiveEnd) {
135         if (exclusiveEnd == DONE || offset == DONE)
136             return false;
137
138         Assert.isTrue(offset >= 0);
139         Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
140         Assert.isTrue(exclusiveEnd > offset);
141
142         CharSequence JavaDoc seq= fIterator.fText;
143
144         while (offset < exclusiveEnd) {
145             char ch= seq.charAt(offset);
146             if (ch != '\n' && ch != '\r')
147                 return false;
148             offset++;
149         }
150
151         return true;
152     }
153
154     /**
155      * Returns <code>true</code> if the given sequence into the underlying text
156      * represents whitespace, but not a delimiter, <code>false</code> otherwise.
157      *
158      * @param offset the offset
159      * @param exclusiveEnd the end offset
160      * @return <code>true</code> if the given range is whitespace
161      */

162     private boolean isWhitespace(int offset, int exclusiveEnd) {
163         if (exclusiveEnd == DONE || offset == DONE)
164             return false;
165
166         Assert.isTrue(offset >= 0);
167         Assert.isTrue(exclusiveEnd <= getText().getEndIndex());
168         Assert.isTrue(exclusiveEnd > offset);
169
170         CharSequence JavaDoc seq= fIterator.fText;
171
172         while (offset < exclusiveEnd) {
173             char ch= seq.charAt(offset);
174             if (!Character.isWhitespace(ch))
175                 return false;
176             if (ch == '\n' || ch == '\r')
177                 return false;
178             offset++;
179         }
180
181         return true;
182     }
183
184     /*
185      * @see java.text.BreakIterator#current()
186      */

187     public int current() {
188         return fIndex;
189     }
190
191     /*
192      * @see java.text.BreakIterator#getText()
193      */

194     public CharacterIterator JavaDoc getText() {
195         return fIterator.getText();
196     }
197
198     /**
199      * Sets the text as <code>CharSequence</code>.
200      * @param newText the new text
201      */

202     public void setText(CharSequence JavaDoc newText) {
203         fIterator.setText(newText);
204         first();
205     }
206
207     /*
208      * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
209      */

210     public void setText(CharacterIterator JavaDoc newText) {
211         fIterator.setText(newText);
212         first();
213     }
214
215     /*
216      * @see java.text.BreakIterator#setText(java.lang.String)
217      */

218     public void setText(String JavaDoc newText) {
219         setText((CharSequence JavaDoc) newText);
220     }
221
222 }
223
Popular Tags