KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.core.runtime.Assert;
15
16
17 /**
18  * An implementation of <code>IRule</code> detecting a numerical value.
19  */

20 public class NumberRule implements IRule {
21
22     /** Internal setting for the un-initialized column constraint */
23     protected static final int UNDEFINED= -1;
24     /** The token to be returned when this rule is successful */
25     protected IToken fToken;
26     /** The column constraint */
27     protected int fColumn= UNDEFINED;
28
29     /**
30      * Creates a rule which will return the specified
31      * token when a numerical sequence is detected.
32      *
33      * @param token the token to be returned
34      */

35     public NumberRule(IToken token) {
36         Assert.isNotNull(token);
37         fToken= token;
38     }
39
40     /**
41      * Sets a column constraint for this rule. If set, the rule's token
42      * will only be returned if the pattern is detected starting at the
43      * specified column. If the column is smaller then 0, the column
44      * constraint is considered removed.
45      *
46      * @param column the column in which the pattern starts
47      */

48     public void setColumnConstraint(int column) {
49         if (column < 0)
50             column= UNDEFINED;
51         fColumn= column;
52     }
53
54     /*
55      * @see IRule#evaluate(ICharacterScanner)
56      */

57     public IToken evaluate(ICharacterScanner scanner) {
58         int c= scanner.read();
59         if (Character.isDigit((char)c)) {
60             if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
61                 do {
62                     c= scanner.read();
63                 } while (Character.isDigit((char) c));
64                 scanner.unread();
65                 return fToken;
66             }
67         }
68
69         scanner.unread();
70         return Token.UNDEFINED;
71     }
72 }
73
Popular Tags