KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jface.text.rules.ICharacterScanner;
22 import org.eclipse.jface.text.rules.IRule;
23 import org.eclipse.jface.text.rules.IToken;
24 import org.eclipse.jface.text.rules.IWordDetector;
25 import org.eclipse.jface.text.rules.Token;
26
27
28 /**
29  * An implementation of <code>IRule</code> capable of detecting words.
30  * <p>
31  * Word rules also allow for the association of tokens with specific words.
32  * That is, not only can the rule be used to provide tokens for exact matches,
33  * but also for the generalized notion of a word in the context in which it is used.
34  * A word rules uses a word detector to determine what a word is.</p>
35  * <p>
36  * This word rule allows a word detector to be shared among different word matchers.
37  * Its up to the word matchers to decide if a word matches and, in this a case, which
38  * token is associated with that word.
39  * </p>
40  *
41  * @see IWordDetector
42  * @since 3.0
43  */

44 public class CombinedWordRule implements IRule {
45
46     /**
47      * Word matcher, that associates matched words with tokens.
48      */

49     public static class WordMatcher {
50
51         /** The table of predefined words and token for this matcher */
52         private Map JavaDoc fWords= new HashMap JavaDoc();
53
54         /**
55          * Adds a word and the token to be returned if it is detected.
56          *
57          * @param word the word this rule will search for, may not be <code>null</code>
58          * @param token the token to be returned if the word has been found, may not be <code>null</code>
59          */

60         public void addWord(String JavaDoc word, IToken token) {
61             Assert.isNotNull(word);
62             Assert.isNotNull(token);
63
64             fWords.put(new CharacterBuffer(word), token);
65         }
66
67         /**
68          * Returns the token associated to the given word and the scanner state.
69          *
70          * @param scanner the scanner
71          * @param word the word
72          * @return the token or <code>null</code> if none is associated by this matcher
73          */

74         public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
75             IToken token= (IToken) fWords.get(word);
76             if (token != null)
77                 return token;
78             return Token.UNDEFINED;
79         }
80
81         /**
82          * Removes all words.
83          */

84         public void clearWords() {
85             fWords.clear();
86         }
87     }
88
89     /**
90      * Character buffer, mutable <b>or</b> suitable for use as key in hash maps.
91      */

92     public static class CharacterBuffer {
93
94         /** Buffer content */
95         private char[] fContent;
96         /** Buffer content size */
97         private int fLength= 0;
98
99         /** Is hash code cached? */
100         private boolean fIsHashCached= false;
101         /** The hash code */
102         private int fHashCode;
103
104         /**
105          * Initialize with the given capacity.
106          *
107          * @param capacity the initial capacity
108          */

109         public CharacterBuffer(int capacity) {
110             fContent= new char[capacity];
111         }
112
113         /**
114          * Initialize with the given content.
115          *
116          * @param content the initial content
117          */

118         public CharacterBuffer(String JavaDoc content) {
119             fContent= content.toCharArray();
120             fLength= content.length();
121         }
122
123         /**
124          * Empties this buffer.
125          */

126         public void clear() {
127             fIsHashCached= false;
128             fLength= 0;
129         }
130
131         /**
132          * Appends the given character to the buffer.
133          *
134          * @param c the character
135          */

136         public void append(char c) {
137             fIsHashCached= false;
138             if (fLength == fContent.length) {
139                 char[] old= fContent;
140                 fContent= new char[old.length << 1];
141                 System.arraycopy(old, 0, fContent, 0, old.length);
142             }
143             fContent[fLength++]= c;
144         }
145
146         /**
147          * Returns the length of the content.
148          *
149          * @return the length
150          */

151         public int length() {
152             return fLength;
153         }
154
155         /**
156          * Returns the content as string.
157          *
158          * @return the content
159          */

160         public String JavaDoc toString() {
161             return new String JavaDoc(fContent, 0, fLength);
162         }
163
164         /**
165          * Returns the character at the given position.
166          *
167          * @param i the position
168          * @return the character at position <code>i</code>
169          */

170         public char charAt(int i) {
171             return fContent[i];
172         }
173
174         /*
175          * @see java.lang.Object#hashCode()
176          */

177         public int hashCode() {
178             if (fIsHashCached)
179                 return fHashCode;
180
181             int hash= 0;
182             for (int i= 0, n= fLength; i < n; i++)
183                 hash= 29*hash + fContent[i];
184             fHashCode= hash;
185             fIsHashCached= true;
186             return hash;
187         }
188
189
190         /*
191          * @see java.lang.Object#equals(java.lang.Object)
192          */

193         public boolean equals(Object JavaDoc obj) {
194             if (obj == this)
195                 return true;
196             if (!(obj instanceof CharacterBuffer))
197                 return false;
198             CharacterBuffer buffer= (CharacterBuffer) obj;
199             int length= buffer.length();
200             if (length != fLength)
201                 return false;
202             for (int i= 0; i < length; i++)
203                 if (buffer.charAt(i) != fContent[i])
204                     return false;
205             return true;
206         }
207
208         /**
209          * Is the content equal to the given string?
210          *
211          * @param string the string
212          * @return <code>true</code> iff the content is the same character sequence as in the string
213          */

214         public boolean equals(String JavaDoc string) {
215             int length= string.length();
216             if (length != fLength)
217                 return false;
218             for (int i= 0; i < length; i++)
219                 if (string.charAt(i) != fContent[i])
220                     return false;
221             return true;
222         }
223     }
224
225     /** Internal setting for the uninitialized column constraint */
226     private static final int UNDEFINED= -1;
227
228     /** The word detector used by this rule */
229     private IWordDetector fDetector;
230     /** The default token to be returned on success and if nothing else has been specified. */
231     private IToken fDefaultToken;
232     /** The column constraint */
233     private int fColumn= UNDEFINED;
234     /** Buffer used for pattern detection */
235     private CharacterBuffer fBuffer= new CharacterBuffer(16);
236
237     /** List of word matchers */
238     private List JavaDoc fMatchers= new ArrayList JavaDoc();
239
240     /**
241      * Creates a rule which, with the help of an word detector, will return the token
242      * associated with the detected word. If no token has been associated, the scanner
243      * will be rolled back and an undefined token will be returned in order to allow
244      * any subsequent rules to analyze the characters.
245      *
246      * @param detector the word detector to be used by this rule, may not be <code>null</code>
247      *
248      * @see WordMatcher#addWord(String, IToken)
249      */

250     public CombinedWordRule(IWordDetector detector) {
251         this(detector, null, Token.UNDEFINED);
252     }
253
254     /**
255      * Creates a rule which, with the help of an word detector, will return the token
256      * associated with the detected word. If no token has been associated, the
257      * specified default token will be returned.
258      *
259      * @param detector the word detector to be used by this rule, may not be <code>null</code>
260      * @param defaultToken the default token to be returned on success
261      * if nothing else is specified, may not be <code>null</code>
262      *
263      * @see WordMatcher#addWord(String, IToken)
264      */

265     public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
266         this(detector, null, defaultToken);
267     }
268
269     /**
270      * Creates a rule which, with the help of an word detector, will return the token
271      * associated with the detected word. If no token has been associated, the scanner
272      * will be rolled back and an undefined token will be returned in order to allow
273      * any subsequent rules to analyze the characters.
274      *
275      * @param detector the word detector to be used by this rule, may not be <code>null</code>
276      * @param matcher the initial word matcher
277      *
278      * @see WordMatcher#addWord(String, IToken)
279      */

280     public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
281         this(detector, matcher, Token.UNDEFINED);
282     }
283
284     /**
285      * Creates a rule which, with the help of an word detector, will return the token
286      * associated with the detected word. If no token has been associated, the
287      * specified default token will be returned.
288      *
289      * @param detector the word detector to be used by this rule, may not be <code>null</code>
290      * @param matcher the initial word matcher
291      * @param defaultToken the default token to be returned on success
292      * if nothing else is specified, may not be <code>null</code>
293      *
294      * @see WordMatcher#addWord(String, IToken)
295      */

296     public CombinedWordRule(IWordDetector detector, WordMatcher matcher, IToken defaultToken) {
297
298         Assert.isNotNull(detector);
299         Assert.isNotNull(defaultToken);
300
301         fDetector= detector;
302         fDefaultToken= defaultToken;
303         if (matcher != null)
304             addWordMatcher(matcher);
305     }
306
307
308     /**
309      * Adds the given matcher.
310      *
311      * @param matcher the matcher
312      */

313     public void addWordMatcher(WordMatcher matcher) {
314         fMatchers.add(matcher);
315     }
316
317     /**
318      * Sets a column constraint for this rule. If set, the rule's token
319      * will only be returned if the pattern is detected starting at the
320      * specified column. If the column is smaller then 0, the column
321      * constraint is considered removed.
322      *
323      * @param column the column in which the pattern starts
324      */

325     public void setColumnConstraint(int column) {
326         if (column < 0)
327             column= UNDEFINED;
328         fColumn= column;
329     }
330
331     /*
332      * @see IRule#evaluate(ICharacterScanner)
333      */

334     public IToken evaluate(ICharacterScanner scanner) {
335         int c= scanner.read();
336         if (fDetector.isWordStart((char) c)) {
337             if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
338
339                 fBuffer.clear();
340                 do {
341                     fBuffer.append((char) c);
342                     c= scanner.read();
343                 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
344                 scanner.unread();
345
346                 for (int i= 0, n= fMatchers.size(); i < n; i++) {
347                     IToken token= ((WordMatcher) fMatchers.get(i)).evaluate(scanner, fBuffer);
348                     if (!token.isUndefined())
349                         return token;
350                 }
351
352                 if (fDefaultToken.isUndefined())
353                     unreadBuffer(scanner);
354
355                 return fDefaultToken;
356             }
357         }
358
359         scanner.unread();
360         return Token.UNDEFINED;
361     }
362
363     /**
364      * Returns the characters in the buffer to the scanner.
365      *
366      * @param scanner the scanner to be used
367      */

368     private void unreadBuffer(ICharacterScanner scanner) {
369         for (int i= fBuffer.length() - 1; i >= 0; i--)
370             scanner.unread();
371     }
372 }
373
Popular Tags