1 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 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 |