001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.util;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
015:
016: import org.eclipse.jdt.core.ToolFactory;
017: import org.eclipse.jdt.core.compiler.IScanner;
018: import org.eclipse.jdt.core.compiler.ITerminalSymbols;
019: import org.eclipse.jdt.core.compiler.InvalidInputException;
020:
021: import org.eclipse.jdt.internal.corext.dom.Selection;
022: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
023:
024: public class CommentAnalyzer {
025:
026: private CommentAnalyzer() {
027: }
028:
029: public static RefactoringStatus perform(Selection selection,
030: IScanner scanner, int start, int length) {
031: RefactoringStatus result = new RefactoringStatus();
032: if (length <= 0)
033: return result;
034: new CommentAnalyzer().check(result, selection, scanner, start,
035: start + length - 1);
036: return result;
037: }
038:
039: private void check(RefactoringStatus result, Selection selection,
040: IScanner scanner, int start, int end) {
041: char[] characters = scanner.getSource();
042: selection = adjustSelection(characters, selection, end);
043: scanner.resetTo(start, end);
044:
045: int token = 0;
046: try {
047: loop: while (token != ITerminalSymbols.TokenNameEOF) {
048: token = scanner.getNextToken();
049: switch (token) {
050: case ITerminalSymbols.TokenNameCOMMENT_LINE:
051: case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
052: case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
053: if (checkStart(scanner, selection.getOffset())) {
054: result
055: .addFatalError(RefactoringCoreMessages.CommentAnalyzer_starts_inside_comment);
056: break loop;
057: }
058: if (checkEnd(scanner, selection.getInclusiveEnd())) {
059: result
060: .addFatalError(RefactoringCoreMessages.CommentAnalyzer_ends_inside_comment);
061: break loop;
062: }
063: break;
064: }
065: }
066: } catch (InvalidInputException e) {
067: result
068: .addFatalError(RefactoringCoreMessages.CommentAnalyzer_internal_error);
069: }
070: }
071:
072: private boolean checkStart(IScanner scanner, int position) {
073: return scanner.getCurrentTokenStartPosition() < position
074: && position <= scanner.getCurrentTokenEndPosition();
075: }
076:
077: private boolean checkEnd(IScanner scanner, int position) {
078: return scanner.getCurrentTokenStartPosition() <= position
079: && position < scanner.getCurrentTokenEndPosition();
080: }
081:
082: private Selection adjustSelection(char[] characters,
083: Selection selection, int end) {
084: int newEnd = selection.getInclusiveEnd();
085: for (int i = selection.getExclusiveEnd(); i <= end; i++) {
086: char ch = characters[i];
087: if (ch != '\n' && ch != '\r')
088: break;
089: newEnd++;
090: }
091: return Selection.createFromStartEnd(selection.getOffset(),
092: newEnd);
093: }
094:
095: /**
096: * Removes comments and whitespace
097: * @param reference the type reference
098: * @return the reference only consisting of dots and java identifier characters
099: */
100: public static String normalizeReference(String reference) {
101: IScanner scanner = ToolFactory.createScanner(false, false,
102: false, false);
103: scanner.setSource(reference.toCharArray());
104: StringBuffer sb = new StringBuffer();
105: try {
106: int tokenType = scanner.getNextToken();
107: while (tokenType != ITerminalSymbols.TokenNameEOF) {
108: sb.append(scanner.getRawTokenSource());
109: tokenType = scanner.getNextToken();
110: }
111: } catch (InvalidInputException e) {
112: Assert.isTrue(false, reference);
113: }
114: reference = sb.toString();
115: return reference;
116: }
117: }
|