KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jface.text.rules;
13
14
15 import org.eclipse.core.runtime.Assert;
16
17
18 /**
19  * An implementation of <code>IRule</code> capable of detecting whitespace.
20  * A whitespace rule uses a whitespace detector in order to find out which
21  * characters are whitespace characters.
22  *
23  * @see IWhitespaceDetector
24  */

25 public class WhitespaceRule implements IRule {
26
27     /** The whitespace detector used by this rule */
28     protected IWhitespaceDetector fDetector;
29
30     /**
31      * Creates a rule which, with the help of an
32      * whitespace detector, will return a whitespace
33      * token when a whitespace is detected.
34      *
35      * @param detector the rule's whitespace detector, may not be <code>null</code>
36      */

37     public WhitespaceRule(IWhitespaceDetector detector) {
38         Assert.isNotNull(detector);
39         fDetector= detector;
40     }
41
42     /*
43      * @see IRule#evaluate(ICharacterScanner)
44      */

45     public IToken evaluate(ICharacterScanner scanner) {
46         int c= scanner.read();
47         if (fDetector.isWhitespace((char) c)) {
48             do {
49                 c= scanner.read();
50             } while (fDetector.isWhitespace((char) c));
51             scanner.unread();
52             return Token.WHITESPACE;
53         }
54
55         scanner.unread();
56         return Token.UNDEFINED;
57     }
58 }
59
Popular Tags