KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaPartitionScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.text;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.text.rules.EndOfLineRule;
18 import org.eclipse.jface.text.rules.ICharacterScanner;
19 import org.eclipse.jface.text.rules.IPredicateRule;
20 import org.eclipse.jface.text.rules.IToken;
21 import org.eclipse.jface.text.rules.IWordDetector;
22 import org.eclipse.jface.text.rules.MultiLineRule;
23 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
24 import org.eclipse.jface.text.rules.SingleLineRule;
25 import org.eclipse.jface.text.rules.Token;
26 import org.eclipse.jface.text.rules.WordRule;
27
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
29
30
31 /**
32  * This scanner recognizes the JavaDoc comments and Java multi line comments.
33  */

34 public class JavaPartitionScanner extends RuleBasedPartitionScanner implements IJavaPartitions {
35
36     /**
37      * Detector for empty comments.
38      */

39     static class EmptyCommentDetector implements IWordDetector {
40
41         /*
42          * @see IWordDetector#isWordStart
43          */

44         public boolean isWordStart(char c) {
45             return (c == '/');
46         }
47
48         /*
49          * @see IWordDetector#isWordPart
50          */

51         public boolean isWordPart(char c) {
52             return (c == '*' || c == '/');
53         }
54     }
55
56
57     /**
58      * Word rule for empty comments.
59      */

60     static class EmptyCommentRule extends WordRule implements IPredicateRule {
61
62         private IToken fSuccessToken;
63         /**
64          * Constructor for EmptyCommentRule.
65          * @param successToken
66          */

67         public EmptyCommentRule(IToken successToken) {
68             super(new EmptyCommentDetector());
69             fSuccessToken= successToken;
70             addWord("/**/", fSuccessToken); //$NON-NLS-1$
71
}
72
73         /*
74          * @see IPredicateRule#evaluate(ICharacterScanner, boolean)
75          */

76         public IToken evaluate(ICharacterScanner scanner, boolean resume) {
77             return evaluate(scanner);
78         }
79
80         /*
81          * @see IPredicateRule#getSuccessToken()
82          */

83         public IToken getSuccessToken() {
84             return fSuccessToken;
85         }
86     }
87
88
89
90     /**
91      * Creates the partitioner and sets up the appropriate rules.
92      */

93     public JavaPartitionScanner() {
94         super();
95
96         IToken string= new Token(JAVA_STRING);
97         IToken character= new Token(JAVA_CHARACTER);
98         IToken javaDoc= new Token(JAVA_DOC);
99         IToken multiLineComment= new Token(JAVA_MULTI_LINE_COMMENT);
100         IToken singleLineComment= new Token(JAVA_SINGLE_LINE_COMMENT);
101
102         List JavaDoc rules= new ArrayList JavaDoc();
103
104         // Add rule for single line comments.
105
rules.add(new EndOfLineRule("//", singleLineComment)); //$NON-NLS-1$
106

107         // Add rule for strings.
108
rules.add(new SingleLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
109

110         // Add rule for character constants.
111
rules.add(new SingleLineRule("'", "'", character, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
112

113         // Add special case word rule.
114
EmptyCommentRule wordRule= new EmptyCommentRule(multiLineComment);
115         rules.add(wordRule);
116
117         // Add rules for multi-line comments and javadoc.
118
rules.add(new MultiLineRule("/**", "*/", javaDoc)); //$NON-NLS-1$ //$NON-NLS-2$
119
rules.add(new MultiLineRule("/*", "*/", multiLineComment)); //$NON-NLS-1$ //$NON-NLS-2$
120

121         IPredicateRule[] result= new IPredicateRule[rules.size()];
122         rules.toArray(result);
123         setPredicateRules(result);
124     }
125 }
126
Popular Tags