KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > rules > WordRule


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.jface.text.rules;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20
21
22 /**
23  * An implementation of <code>IRule</code> capable of detecting words
24  * Word rules also allow for the association of tokens with specific words.
25  * That is, not only can the rule be used to provide tokens for exact matches,
26  * but also for the generalized notion of a word in the context in which it is used.
27  * A word rules uses a word detector to determine what a word is.
28  *
29  * @see IWordDetector
30  */

31 public class WordRule implements IRule {
32
33     /** Internal setting for the un-initialized column constraint. */
34     protected static final int UNDEFINED= -1;
35
36     /** The word detector used by this rule. */
37     protected IWordDetector fDetector;
38     /** The default token to be returned on success and if nothing else has been specified. */
39     protected IToken fDefaultToken;
40     /** The column constraint. */
41     protected int fColumn= UNDEFINED;
42     /** The table of predefined words and token for this rule. */
43     protected Map JavaDoc fWords= new HashMap JavaDoc();
44     /** Buffer used for pattern detection. */
45     private StringBuffer JavaDoc fBuffer= new StringBuffer JavaDoc();
46     /**
47      * Tells whether this rule is case sensitive.
48      * @since 3.3
49      */

50     private boolean fIgnoreCase= false;
51
52     /**
53      * Creates a rule which, with the help of an word detector, will return the token
54      * associated with the detected word. If no token has been associated, the scanner
55      * will be rolled back and an undefined token will be returned in order to allow
56      * any subsequent rules to analyze the characters.
57      *
58      * @param detector the word detector to be used by this rule, may not be <code>null</code>
59      * @see #addWord(String, IToken)
60      */

61     public WordRule(IWordDetector detector) {
62         this(detector, Token.UNDEFINED, false);
63     }
64
65     /**
66      * Creates a rule which, with the help of a word detector, will return the token
67      * associated with the detected word. If no token has been associated, the
68      * specified default token will be returned.
69      *
70      * @param detector the word detector to be used by this rule, may not be <code>null</code>
71      * @param defaultToken the default token to be returned on success
72      * if nothing else is specified, may not be <code>null</code>
73      * @see #addWord(String, IToken)
74      */

75     public WordRule(IWordDetector detector, IToken defaultToken) {
76         this(detector, defaultToken, false);
77     }
78
79     /**
80      * Creates a rule which, with the help of a word detector, will return the token
81      * associated with the detected word. If no token has been associated, the
82      * specified default token will be returned.
83      *
84      * @param detector the word detector to be used by this rule, may not be <code>null</code>
85      * @param defaultToken the default token to be returned on success
86      * if nothing else is specified, may not be <code>null</code>
87      * @param ignoreCase the case sensitivity associated with this rule
88      * @see #addWord(String, IToken)
89      * @since 3.3
90      */

91     public WordRule(IWordDetector detector, IToken defaultToken, boolean ignoreCase) {
92         Assert.isNotNull(detector);
93         Assert.isNotNull(defaultToken);
94
95         fDetector= detector;
96         fDefaultToken= defaultToken;
97         fIgnoreCase= ignoreCase;
98     }
99
100     /**
101      * Adds a word and the token to be returned if it is detected.
102      *
103      * @param word the word this rule will search for, may not be <code>null</code>
104      * @param token the token to be returned if the word has been found, may not be <code>null</code>
105      */

106     public void addWord(String JavaDoc word, IToken token) {
107         Assert.isNotNull(word);
108         Assert.isNotNull(token);
109
110         fWords.put(word, token);
111     }
112
113     /**
114      * Sets a column constraint for this rule. If set, the rule's token
115      * will only be returned if the pattern is detected starting at the
116      * specified column. If the column is smaller then 0, the column
117      * constraint is considered removed.
118      *
119      * @param column the column in which the pattern starts
120      */

121     public void setColumnConstraint(int column) {
122         if (column < 0)
123             column= UNDEFINED;
124         fColumn= column;
125     }
126
127     /*
128      * @see IRule#evaluate(ICharacterScanner)
129      */

130     public IToken evaluate(ICharacterScanner scanner) {
131         int c= scanner.read();
132         if (c != ICharacterScanner.EOF && fDetector.isWordStart((char) c)) {
133             if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
134
135                 fBuffer.setLength(0);
136                 do {
137                     fBuffer.append((char) c);
138                     c= scanner.read();
139                 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
140                 scanner.unread();
141
142                 String JavaDoc buffer= fBuffer.toString();
143                 IToken token= (IToken)fWords.get(buffer);
144                 
145                 if(fIgnoreCase) {
146                     Iterator JavaDoc iter= fWords.keySet().iterator();
147                     while (iter.hasNext()) {
148                         String JavaDoc key= (String JavaDoc)iter.next();
149                         if(buffer.equalsIgnoreCase(key)) {
150                             token= (IToken)fWords.get(key);
151                             break;
152                         }
153                     }
154                 } else
155                     token= (IToken)fWords.get(buffer);
156                 
157                 if (token != null)
158                     return token;
159
160                 if (fDefaultToken.isUndefined())
161                     unreadBuffer(scanner);
162
163                 return fDefaultToken;
164             }
165         }
166
167         scanner.unread();
168         return Token.UNDEFINED;
169     }
170
171     /**
172      * Returns the characters in the buffer to the scanner.
173      *
174      * @param scanner the scanner to be used
175      */

176     protected void unreadBuffer(ICharacterScanner scanner) {
177         for (int i= fBuffer.length() - 1; i >= 0; i--)
178             scanner.unread();
179     }
180
181 }
182
Popular Tags