KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.text.IDocument;
16
17
18 /**
19  * Scanner that exclusively uses predicate rules.
20  * @since 2.0
21  */

22 public class RuleBasedPartitionScanner extends BufferedRuleBasedScanner implements IPartitionTokenScanner {
23
24     /** The content type of the partition in which to resume scanning. */
25     protected String JavaDoc fContentType;
26     /** The offset of the partition inside which to resume. */
27     protected int fPartitionOffset;
28
29
30     /**
31      * Disallow setting the rules since this scanner
32      * exclusively uses predicate rules.
33      *
34      * @param rules the sequence of rules controlling this scanner
35      */

36     public void setRules(IRule[] rules) {
37         throw new UnsupportedOperationException JavaDoc();
38     }
39
40     /*
41      * @see RuleBasedScanner#setRules(IRule[])
42      */

43     public void setPredicateRules(IPredicateRule[] rules) {
44         super.setRules(rules);
45     }
46
47     /*
48      * @see ITokenScanner#setRange(IDocument, int, int)
49      */

50     public void setRange(IDocument document, int offset, int length) {
51         setPartialRange(document, offset, length, null, -1);
52     }
53
54     /*
55      * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int)
56      */

57     public void setPartialRange(IDocument document, int offset, int length, String JavaDoc contentType, int partitionOffset) {
58         fContentType= contentType;
59         fPartitionOffset= partitionOffset;
60         if (partitionOffset > -1) {
61             int delta= offset - partitionOffset;
62             if (delta > 0) {
63                 super.setRange(document, partitionOffset, length + delta);
64                 fOffset= offset;
65                 return;
66             }
67         }
68         super.setRange(document, offset, length);
69     }
70
71     /*
72      * @see ITokenScanner#nextToken()
73      */

74     public IToken nextToken() {
75
76
77         if (fContentType == null || fRules == null) {
78             //don't try to resume
79
return super.nextToken();
80         }
81
82         // inside a partition
83

84         fColumn= UNDEFINED;
85         boolean resume= (fPartitionOffset > -1 && fPartitionOffset < fOffset);
86         fTokenOffset= resume ? fPartitionOffset : fOffset;
87
88         IPredicateRule rule;
89         IToken token;
90
91         for (int i= 0; i < fRules.length; i++) {
92             rule= (IPredicateRule) fRules[i];
93             token= rule.getSuccessToken();
94             if (fContentType.equals(token.getData())) {
95                 token= rule.evaluate(this, resume);
96                 if (!token.isUndefined()) {
97                     fContentType= null;
98                     return token;
99                 }
100             }
101         }
102
103         // haven't found any rule for this type of partition
104
fContentType= null;
105         if (resume)
106             fOffset= fPartitionOffset;
107         return super.nextToken();
108     }
109 }
110
Popular Tags