KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Assert;
15
16
17
18 /**
19  * A specific single line rule which stipulates that the start
20  * and end sequence occur within a single word, as defined by a word detector.
21  *
22  * @see IWordDetector
23  */

24 public class WordPatternRule extends SingleLineRule {
25
26     /** The word detector used by this rule */
27     protected IWordDetector fDetector;
28     /** The internal buffer used for pattern detection */
29     private StringBuffer JavaDoc fBuffer= new StringBuffer JavaDoc();
30
31     /**
32      * Creates a rule for the given starting and ending word
33      * pattern which, if detected, will return the specified token.
34      * A word detector is used to identify words.
35      *
36      * @param detector the word detector to be used
37      * @param startSequence the start sequence of the word pattern
38      * @param endSequence the end sequence of the word pattern
39      * @param token the token to be returned on success
40      */

41     public WordPatternRule(IWordDetector detector, String JavaDoc startSequence, String JavaDoc endSequence, IToken token) {
42         this(detector, startSequence, endSequence, token, (char)0);
43     }
44
45     /**
46     /**
47      * Creates a rule for the given starting and ending word
48      * pattern which, if detected, will return the specified token.
49      * A word detector is used to identify words.
50      * Any character which follows the given escapeCharacter will be ignored.
51      *
52      * @param detector the word detector to be used
53      * @param startSequence the start sequence of the word pattern
54      * @param endSequence the end sequence of the word pattern
55      * @param token the token to be returned on success
56      * @param escapeCharacter the escape character
57      */

58     public WordPatternRule(IWordDetector detector, String JavaDoc startSequence, String JavaDoc endSequence, IToken token, char escapeCharacter) {
59         super(startSequence, endSequence, token, escapeCharacter);
60         Assert.isNotNull(detector);
61         fDetector= detector;
62     }
63
64     /**
65      * Returns whether the end sequence was detected.
66      * The rule acquires the rest of the word, using the
67      * provided word detector, and tests to determine if
68      * it ends with the end sequence.
69      *
70      * @param scanner the scanner to be used
71      * @return <code>true</code> if the word ends on the given end sequence
72      */

73     protected boolean endSequenceDetected(ICharacterScanner scanner) {
74         fBuffer.setLength(0);
75         int c= scanner.read();
76         while (fDetector.isWordPart((char) c)) {
77             fBuffer.append((char) c);
78             c= scanner.read();
79         }
80         scanner.unread();
81
82         if (fBuffer.length() >= fEndSequence.length) {
83             for (int i=fEndSequence.length - 1, j= fBuffer.length() - 1; i >= 0; i--, j--) {
84                 if (fEndSequence[i] != fBuffer.charAt(j)) {
85                     unreadBuffer(scanner);
86                     return false;
87                 }
88             }
89             return true;
90         }
91
92         unreadBuffer(scanner);
93         return false;
94     }
95
96     /**
97      * Returns the characters in the buffer to the scanner.
98      * Note that the rule must also return the characters
99      * read in as part of the start sequence expect the first one.
100      *
101      * @param scanner the scanner to be used
102      */

103     protected void unreadBuffer(ICharacterScanner scanner) {
104         fBuffer.insert(0, fStartSequence);
105         for (int i= fBuffer.length() - 1; i > 0; i--)
106             scanner.unread();
107     }
108 }
109
Popular Tags