refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / CombinedWordRule.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.jface.text.Assert;
19 import org.eclipse.jface.text.rules.ICharacterScanner;
20 import org.eclipse.jface.text.rules.IRule;
21 import org.eclipse.jface.text.rules.IToken;
22 import org.eclipse.jface.text.rules.IWordDetector;
23 import org.eclipse.jface.text.rules.Token;
24
25 /**
26  * An implementation of <code>IRule</code> capable of detecting words.
27  * <p>
28  * Word rules also allow for the association of tokens with specific words. That
29  * is, not only can the rule be used to provide tokens for exact matches, but
30  * also for the generalized notion of a word in the context in which it is used.
31  * A word rules uses a word detector to determine what a word is.
32  * </p>
33  * <p>
34  * This word rule allows a word detector to be shared among different word
35  * matchers. Its up to the word matchers to decide if a word matches and, in
36  * this a case, which token is associated with that word.
37  * </p>
38  * 
39  * @see IWordDetector
40  * @since 3.0
41  */
42 public class CombinedWordRule implements IRule {
43
44         /**
45          * Word matcher, that associates matched words with tokens.
46          */
47         public static class WordMatcher {
48
49                 /** The table of predefined words and token for this matcher */
50                 private Map fWords = new HashMap();
51
52                 /**
53                  * Adds a word and the token to be returned if it is detected.
54                  * 
55                  * @param word
56                  *            the word this rule will search for, may not be
57                  *            <code>null</code>
58                  * @param token
59                  *            the token to be returned if the word has been found, may
60                  *            not be <code>null</code>
61                  */
62                 public void addWord(String word, IToken token) {
63                         Assert.isNotNull(word);
64                         Assert.isNotNull(token);
65
66                         fWords.put(new CharacterBuffer(word), token);
67                 }
68
69                 /**
70                  * Returns the token associated to the given word and the scanner state.
71                  * 
72                  * @param scanner
73                  *            the scanner
74                  * @param word
75                  *            the word
76                  * @return the token or <code>null</code> if none is associated by
77                  *         this matcher
78                  */
79                 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
80                         IToken token = (IToken) fWords.get(word);
81                         if (token != null)
82                                 return token;
83                         return Token.UNDEFINED;
84                 }
85
86                 /**
87                  * Removes all words.
88                  */
89                 public void clearWords() {
90                         fWords.clear();
91                 }
92         }
93
94         /**
95          * Character buffer, mutable <b>or</b> suitable for use as key in hash
96          * maps.
97          */
98         public static class CharacterBuffer {
99
100                 /** Buffer content */
101                 private char[] fContent;
102
103                 /** Buffer content size */
104                 private int fLength = 0;
105
106                 /** Is hash code cached? */
107                 private boolean fIsHashCached = false;
108
109                 /** The hash code */
110                 private int fHashCode;
111
112                 /**
113                  * Initialize with the given capacity.
114                  * 
115                  * @param capacity
116                  *            the initial capacity
117                  */
118                 public CharacterBuffer(int capacity) {
119                         fContent = new char[capacity];
120                 }
121
122                 /**
123                  * Initialize with the given content.
124                  * 
125                  * @param string
126                  *            the initial content
127                  */
128                 public CharacterBuffer(String content) {
129                         fContent = content.toCharArray();
130                         fLength = content.length();
131                 }
132
133                 /**
134                  * Empties this buffer.
135                  */
136                 public void clear() {
137                         fIsHashCached = false;
138                         fLength = 0;
139                 }
140
141                 /**
142                  * Appends the given character to the buffer.
143                  * 
144                  * @param c
145                  *            the character
146                  */
147                 public void append(char c) {
148                         fIsHashCached = false;
149                         if (fLength == fContent.length) {
150                                 char[] old = fContent;
151                                 fContent = new char[old.length << 1];
152                                 System.arraycopy(old, 0, fContent, 0, old.length);
153                         }
154                         fContent[fLength++] = c;
155                 }
156
157                 /**
158                  * Returns the length of the content.
159                  * 
160                  * @return the length
161                  */
162                 public int length() {
163                         return fLength;
164                 }
165
166                 /**
167                  * Returns the content as string.
168                  * 
169                  * @return the content
170                  */
171                 public String toString() {
172                         return new String(fContent, 0, fLength);
173                 }
174
175                 /**
176                  * Returns the character at the given position.
177                  * 
178                  * @param i
179                  *            the position
180                  * @return the character at position <code>i</code>
181                  */
182                 public char charAt(int i) {
183                         return fContent[i];
184                 }
185
186                 /*
187                  * @see java.lang.Object#hashCode()
188                  */
189                 public int hashCode() {
190                         if (fIsHashCached)
191                                 return fHashCode;
192
193                         int hash = 0;
194                         for (int i = 0, n = fLength; i < n; i++)
195                                 hash = 29 * hash + fContent[i];
196                         fHashCode = hash;
197                         fIsHashCached = true;
198                         return hash;
199                 }
200
201                 /*
202                  * @see java.lang.Object#equals(java.lang.Object)
203                  */
204                 public boolean equals(Object obj) {
205                         if (obj == this)
206                                 return true;
207                         if (!(obj instanceof CharacterBuffer))
208                                 return false;
209                         CharacterBuffer buffer = (CharacterBuffer) obj;
210                         int length = buffer.length();
211                         if (length != fLength)
212                                 return false;
213                         for (int i = 0; i < length; i++)
214                                 if (buffer.charAt(i) != fContent[i])
215                                         return false;
216                         return true;
217                 }
218
219                 /**
220                  * Is the content equal to the given string?
221                  * 
222                  * @param string
223                  *            the string
224                  * @return <code>true</code> iff the content is the same character
225                  *         sequence as in the string
226                  */
227                 public boolean equals(String string) {
228                         int length = string.length();
229                         if (length != fLength)
230                                 return false;
231                         for (int i = 0; i < length; i++)
232                                 if (string.charAt(i) != fContent[i])
233                                         return false;
234                         return true;
235                 }
236         }
237
238         /** Internal setting for the uninitialized column constraint */
239         private static final int UNDEFINED = -1;
240
241         /** The word detector used by this rule */
242         private IWordDetector fDetector;
243
244         /**
245          * The default token to be returned on success and if nothing else has been
246          * specified.
247          */
248         private IToken fDefaultToken;
249
250         /** The column constraint */
251         private int fColumn = UNDEFINED;
252
253         /** Buffer used for pattern detection */
254         private CharacterBuffer fBuffer = new CharacterBuffer(16);
255
256         /** List of word matchers */
257         private List fMatchers = new ArrayList();
258
259         /**
260          * Creates a rule which, with the help of an word detector, will return the
261          * token associated with the detected word. If no token has been associated,
262          * the scanner will be rolled back and an undefined token will be returned
263          * in order to allow any subsequent rules to analyze the characters.
264          * 
265          * @param detector
266          *            the word detector to be used by this rule, may not be
267          *            <code>null</code>
268          * 
269          * @see #addWord(String, IToken)
270          */
271         public CombinedWordRule(IWordDetector detector) {
272                 this(detector, null, Token.UNDEFINED);
273         }
274
275         /**
276          * Creates a rule which, with the help of an word detector, will return the
277          * token associated with the detected word. If no token has been associated,
278          * the specified default token will be returned.
279          * 
280          * @param detector
281          *            the word detector to be used by this rule, may not be
282          *            <code>null</code>
283          * @param defaultToken
284          *            the default token to be returned on success if nothing else is
285          *            specified, may not be <code>null</code>
286          * 
287          * @see #addWord(String, IToken)
288          */
289         public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
290                 this(detector, null, defaultToken);
291         }
292
293         /**
294          * Creates a rule which, with the help of an word detector, will return the
295          * token associated with the detected word. If no token has been associated,
296          * the scanner will be rolled back and an undefined token will be returned
297          * in order to allow any subsequent rules to analyze the characters.
298          * 
299          * @param detector
300          *            the word detector to be used by this rule, may not be
301          *            <code>null</code>
302          * @param matcher
303          *            the initial word matcher
304          * 
305          * @see #addWord(String, IToken)
306          */
307         public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
308                 this(detector, matcher, Token.UNDEFINED);
309         }
310
311         /**
312          * Creates a rule which, with the help of an word detector, will return the
313          * token associated with the detected word. If no token has been associated,
314          * the specified default token will be returned.
315          * 
316          * @param detector
317          *            the word detector to be used by this rule, may not be
318          *            <code>null</code>
319          * @param matcher
320          *            the initial word matcher
321          * @param defaultToken
322          *            the default token to be returned on success if nothing else is
323          *            specified, may not be <code>null</code>
324          * 
325          * @see #addWord(String, IToken)
326          */
327         public CombinedWordRule(IWordDetector detector, WordMatcher matcher,
328                         IToken defaultToken) {
329
330                 Assert.isNotNull(detector);
331                 Assert.isNotNull(defaultToken);
332
333                 fDetector = detector;
334                 fDefaultToken = defaultToken;
335                 if (matcher != null)
336                         addWordMatcher(matcher);
337         }
338
339         /**
340          * Adds the given matcher.
341          * 
342          * @param matcher
343          *            the matcher
344          */
345         public void addWordMatcher(WordMatcher matcher) {
346                 fMatchers.add(matcher);
347         }
348
349         /**
350          * Sets a column constraint for this rule. If set, the rule's token will
351          * only be returned if the pattern is detected starting at the specified
352          * column. If the column is smaller then 0, the column constraint is
353          * considered removed.
354          * 
355          * @param column
356          *            the column in which the pattern starts
357          */
358         public void setColumnConstraint(int column) {
359                 if (column < 0)
360                         column = UNDEFINED;
361                 fColumn = column;
362         }
363
364         /*
365          * @see IRule#evaluate(ICharacterScanner)
366          */
367         public IToken evaluate(ICharacterScanner scanner) {
368                 int c = scanner.read();
369                 if (fDetector.isWordStart((char) c)) {
370                         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
371
372                                 fBuffer.clear();
373                                 do {
374                                         fBuffer.append((char) c);
375                                         c = scanner.read();
376                                 } while (c != ICharacterScanner.EOF
377                                                 && fDetector.isWordPart((char) c));
378                                 scanner.unread();
379
380                                 for (int i = 0, n = fMatchers.size(); i < n; i++) {
381                                         IToken token = ((WordMatcher) fMatchers.get(i)).evaluate(
382                                                         scanner, fBuffer);
383                                         if (!token.isUndefined())
384                                                 return token;
385                                 }
386
387                                 if (fDefaultToken.isUndefined())
388                                         unreadBuffer(scanner);
389
390                                 return fDefaultToken;
391                         }
392                 }
393
394                 scanner.unread();
395                 return Token.UNDEFINED;
396         }
397
398         /**
399          * Returns the characters in the buffer to the scanner.
400          * 
401          * @param scanner
402          *            the scanner to be used
403          */
404         private void unreadBuffer(ICharacterScanner scanner) {
405                 for (int i = fBuffer.length() - 1; i >= 0; i--)
406                         scanner.unread();
407         }
408 }