KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > CompilationUnitBuffer


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.corext.dom;
12
13 import org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.jdt.core.ToolFactory;
16 import org.eclipse.jdt.core.compiler.IScanner;
17 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
18 import org.eclipse.jdt.core.compiler.InvalidInputException;
19
20 public class CompilationUnitBuffer {
21     private IScanner fScanner;
22     private int fSourceLength;
23     
24     public CompilationUnitBuffer(ICompilationUnit unit) throws JavaModelException {
25         fScanner= ToolFactory.createScanner(true, false, false, false);
26         char[] source= unit.getBuffer().getCharacters();
27         fScanner.setSource(source);
28         fSourceLength= source.length;
29     }
30     
31     public char[] getCharacters() {
32         return fScanner.getSource();
33     }
34     
35     public char getCharAt(int index) {
36         return fScanner.getSource()[index];
37     }
38     
39     public int indexOf(int token, int start) {
40         return indexOf(token, start, fSourceLength - start);
41     }
42     
43     public int indexOf(int token, int start, int length) {
44         if (length <= 0)
45             return -1;
46         try {
47             fScanner.resetTo(start, start + length - 1);
48             int next;
49             while((next= fScanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
50                 if (next == token) {
51                     return fScanner.getCurrentTokenStartPosition();
52                 }
53             }
54             return -1;
55         } catch (InvalidInputException e) {
56             return -1;
57         }
58     }
59
60     public int indexAfter(int token, int start) {
61         int result= indexOf(token, start);
62         if (result == -1)
63             return result;
64         return fScanner.getCurrentTokenEndPosition() + 1;
65     }
66     
67     /**
68      * Returns the index of the next token. White spaces are ignored. Comments
69      * are ignored if <code>considerComments</code> is set to <code>false
70      * </code>.
71      *
72      * @param start the start offset
73      * @param considerComments <code>true</code> if comments are to be
74      * considered; otherwise <code>false</code>
75      * @return the index of the next token or -1 if no appropriate toke was found
76      */

77     public int indexOfNextToken(int start, boolean considerComments) {
78         try {
79             fScanner.resetTo(start, fSourceLength - 1);
80             int token;
81             while ((token= fScanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
82                 if (!considerComments && isComment(token))
83                     continue;
84                 return fScanner.getCurrentTokenStartPosition();
85             }
86             return -1;
87         } catch (InvalidInputException e) {
88             return -1;
89         }
90     }
91
92     private boolean isComment(int token) {
93         return token == ITerminalSymbols.TokenNameCOMMENT_BLOCK || token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
94             || token == ITerminalSymbols.TokenNameCOMMENT_LINE;
95     }
96 }
97
Popular Tags