Example usage for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameStringLiteral

List of usage examples for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameStringLiteral

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameStringLiteral.

Prototype

int TokenNameStringLiteral

To view the source code for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameStringLiteral.

Click Source Link

Usage

From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringRefactoring.java

License:Open Source License

/**
 * Try to find the selected Java element in the compilation unit.
 *
 * If selection matches a string literal, capture it, otherwise add a fatal error
 * to the status.//from ww  w. j a  v a 2  s .  c  o m
 *
 * On success, advance the monitor by 3.
 * Returns status.isOK().
 */
private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status,
        IProgressMonitor monitor) {
    try {
        IBuffer buffer = unit.getBuffer();

        IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments
                false, //tokenizeWhiteSpace
                false, //assertMode
                false //recordLineSeparator
        );
        scanner.setSource(buffer.getCharacters());
        monitor.worked(1);

        for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner
                .getNextToken()) {
            if (scanner.getCurrentTokenStartPosition() <= mSelectionStart
                    && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) {
                // found the token, but only keep if the right type
                if (token == ITerminalSymbols.TokenNameStringLiteral) {
                    mTokenString = new String(scanner.getCurrentTokenSource());
                }
                break;
            } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) {
                // scanner is past the selection, abort.
                break;
            }
        }
    } catch (JavaModelException e1) {
        // Error in unit.getBuffer. Ignore.
    } catch (InvalidInputException e2) {
        // Error in scanner.getNextToken. Ignore.
    } finally {
        monitor.worked(1);
    }

    if (mTokenString != null) {
        // As a literal string, the token should have surrounding quotes. Remove them.
        // Note: unquoteAttrValue technically removes either " or ' paired quotes, whereas
        // the Java token should only have " quotes. Since we know the type to be a string
        // literal, there should be no confusion here.
        mTokenString = unquoteAttrValue(mTokenString);

        // We need a non-empty string literal
        if (mTokenString.length() == 0) {
            mTokenString = null;
        }
    }

    if (mTokenString == null) {
        status.addFatalError("Please select a Java string literal.");
    }

    monitor.worked(1);
    return status.isOK();
}

From source file:com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringRefactoring.java

License:Open Source License

/**
 * Try to find the selected Java element in the compilation unit.
 * //from w  w w.j a  va  2  s.  c  om
 * If selection matches a string literal, capture it, otherwise add a fatal error
 * to the status.
 * 
 * On success, advance the monitor by 3.
 */
private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status,
        IProgressMonitor monitor) {
    try {
        IBuffer buffer = unit.getBuffer();

        IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments
                false, //tokenizeWhiteSpace
                false, //assertMode
                false //recordLineSeparator
        );
        scanner.setSource(buffer.getCharacters());
        monitor.worked(1);

        for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner
                .getNextToken()) {
            if (scanner.getCurrentTokenStartPosition() <= mSelectionStart
                    && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) {
                // found the token, but only keep of the right type
                if (token == ITerminalSymbols.TokenNameStringLiteral) {
                    mTokenString = new String(scanner.getCurrentTokenSource());
                }
                break;
            } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) {
                // scanner is past the selection, abort.
                break;
            }
        }
    } catch (JavaModelException e1) {
        // Error in unit.getBuffer. Ignore.
    } catch (InvalidInputException e2) {
        // Error in scanner.getNextToken. Ignore.
    } finally {
        monitor.worked(1);
    }

    if (mTokenString != null) {
        // As a literal string, the token should have surrounding quotes. Remove them.
        int len = mTokenString.length();
        if (len > 0 && mTokenString.charAt(0) == '"' && mTokenString.charAt(len - 1) == '"') {
            mTokenString = mTokenString.substring(1, len - 1);
        }
        // We need a non-empty string literal
        if (mTokenString.length() == 0) {
            mTokenString = null;
        }
    }

    if (mTokenString == null) {
        status.addFatalError("Please select a Java string literal.");
    }

    monitor.worked(1);
    return status.isOK();
}

From source file:comparison.SJavaTokenComparator.java

License:Open Source License

/**
 * Returns the length of the token that//from  w  ww.  j  a  v  a2s.c om
 * initiates the given comment type.
 *
 * @param tokenType
 * @return the length of the token that start a comment
 * @since 3.3
 */
private static int getCommentStartTokenLength(int tokenType) {
    if (tokenType == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
        return 3;
    } else if (tokenType == ITerminalSymbols.TokenNameStringLiteral) {
        return 1;
    } else {
        return 2;
    }
}