KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > javadoc > JavaDocScanner


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.jdt.internal.ui.text.javadoc;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Preferences;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.rules.ICharacterScanner;
24 import org.eclipse.jface.text.rules.IToken;
25 import org.eclipse.jface.text.rules.IWordDetector;
26 import org.eclipse.jface.text.rules.SingleLineRule;
27 import org.eclipse.jface.text.rules.Token;
28 import org.eclipse.jface.text.rules.WhitespaceRule;
29 import org.eclipse.jface.text.rules.WordRule;
30
31 import org.eclipse.jdt.ui.text.IColorManager;
32 import org.eclipse.jdt.ui.text.IJavaColorConstants;
33
34 import org.eclipse.jdt.internal.ui.text.CombinedWordRule;
35 import org.eclipse.jdt.internal.ui.text.JavaCommentScanner;
36 import org.eclipse.jdt.internal.ui.text.JavaWhitespaceDetector;
37 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.CharacterBuffer;
38 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher;
39
40 /**
41  * A rule based JavaDoc scanner.
42  */

43 public final class JavaDocScanner extends JavaCommentScanner {
44
45
46     /**
47      * Detector for HTML comment delimiters.
48      */

49     static class HTMLCommentDetector implements IWordDetector {
50
51         /**
52          * @see IWordDetector#isWordStart(char)
53          */

54         public boolean isWordStart(char c) {
55             return (c == '<' || c == '-');
56         }
57
58         /**
59          * @see IWordDetector#isWordPart(char)
60          */

61         public boolean isWordPart(char c) {
62             return (c == '-' || c == '!' || c == '>');
63         }
64     }
65
66     class TagRule extends SingleLineRule {
67
68         /*
69          * @see SingleLineRule
70          */

71         public TagRule(IToken token) {
72             super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
73
}
74
75         /*
76          * @see SingleLineRule
77          */

78         public TagRule(IToken token, char escapeCharacter) {
79             super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
80
}
81
82         private IToken evaluateToken() {
83             try {
84                 final String JavaDoc token= getDocument().get(getTokenOffset(), getTokenLength()) + "."; //$NON-NLS-1$
85

86                 int offset= 0;
87                 char character= token.charAt(++offset);
88
89                 if (character == '/')
90                     character= token.charAt(++offset);
91
92                 while (Character.isWhitespace(character))
93                     character= token.charAt(++offset);
94
95                 while (Character.isLetterOrDigit(character))
96                     character= token.charAt(++offset);
97
98                 while (Character.isWhitespace(character))
99                     character= token.charAt(++offset);
100
101                 if (offset >= 2 && token.charAt(offset) == fEndSequence[0])
102                     return fToken;
103
104             } catch (BadLocationException exception) {
105                 // Do nothing
106
}
107             return getToken(IJavaColorConstants.JAVADOC_DEFAULT);
108         }
109
110         /*
111          * @see PatternRule#evaluate(ICharacterScanner)
112          */

113         public IToken evaluate(ICharacterScanner scanner) {
114             IToken result= super.evaluate(scanner);
115             if (result == fToken)
116                 return evaluateToken();
117             return result;
118         }
119     }
120
121     private static String JavaDoc[] fgTokenProperties= {
122         IJavaColorConstants.JAVADOC_KEYWORD,
123         IJavaColorConstants.JAVADOC_TAG,
124         IJavaColorConstants.JAVADOC_LINK,
125         IJavaColorConstants.JAVADOC_DEFAULT,
126         TASK_TAG
127     };
128
129
130     public JavaDocScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore) {
131         super(manager, store, coreStore, IJavaColorConstants.JAVADOC_DEFAULT, fgTokenProperties);
132     }
133
134     /**
135      * Initialize with the given arguments
136      * @param manager Color manager
137      * @param store Preference store
138      *
139      * @since 3.0
140      */

141     public JavaDocScanner(IColorManager manager, IPreferenceStore store) {
142         this(manager, store, null);
143     }
144
145     public IDocument getDocument() {
146         return fDocument;
147     }
148
149     /*
150      * @see AbstractJavaScanner#createRules()
151      */

152     protected List JavaDoc createRules() {
153
154         List JavaDoc list= new ArrayList JavaDoc();
155
156         // Add rule for tags.
157
Token token= getToken(IJavaColorConstants.JAVADOC_TAG);
158         list.add(new TagRule(token));
159
160
161         // Add rule for HTML comments
162
WordRule wordRule= new WordRule(new HTMLCommentDetector(), token);
163         wordRule.addWord("<!--", token); //$NON-NLS-1$
164
wordRule.addWord("--!>", token); //$NON-NLS-1$
165
list.add(wordRule);
166
167
168         // Add rule for links.
169
token= getToken(IJavaColorConstants.JAVADOC_LINK);
170         list.add(new SingleLineRule("{@link", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
171
list.add(new SingleLineRule("{@value", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
172

173
174         // Add generic whitespace rule.
175
list.add(new WhitespaceRule(new JavaWhitespaceDetector()));
176
177
178         list.addAll(super.createRules());
179         return list;
180     }
181
182     /*
183      * @see org.eclipse.jdt.internal.ui.text.JavaCommentScanner#createMatchers()
184      */

185     protected List JavaDoc createMatchers() {
186         List JavaDoc list= super.createMatchers();
187
188         // Add word rule for keywords.
189
final IToken token= getToken(IJavaColorConstants.JAVADOC_KEYWORD);
190         WordMatcher matcher= new CombinedWordRule.WordMatcher() {
191             public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
192                 int length= word.length();
193                 if (length > 1 && word.charAt(0) == '@') {
194                     int i= 0;
195                     try {
196                         for (; i <= length; i++)
197                             scanner.unread();
198                         int c= scanner.read();
199                         i--;
200                         if (c == '*' || Character.isWhitespace((char)c)) {
201                             scanner.unread();
202                             return token;
203                         }
204                     } finally {
205                         for (; i >= 0; i--)
206                             scanner.read();
207                     }
208                 }
209                 return Token.UNDEFINED;
210             }
211         };
212         list.add(matcher);
213
214         return list;
215     }
216 }
217
218
219
Popular Tags