Example usage for org.eclipse.jdt.core.dom StringLiteral getRoot

List of usage examples for org.eclipse.jdt.core.dom StringLiteral getRoot

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom StringLiteral getRoot.

Prototype

public final ASTNode getRoot() 

Source Link

Document

Returns the root node at or above this node; returns this node if it is a root.

Usage

From source file:org.eclipse.babel.tapiji.tools.java.util.ASTutils.java

License:Open Source License

public static boolean existsNonInternationalisationComment(StringLiteral literal) throws BadLocationException {
    CompilationUnit cu = (CompilationUnit) literal.getRoot();
    ICompilationUnit icu = (ICompilationUnit) cu.getJavaElement();

    IDocument doc = null;/*from  w w  w  . j  a  v a 2 s  .  c  om*/
    try {
        doc = new Document(icu.getSource());
    } catch (JavaModelException e) {
        Logger.logError(e);
    }

    // get whole line in which string literal
    int lineNo = doc.getLineOfOffset(literal.getStartPosition());
    int lineOffset = doc.getLineOffset(lineNo);
    int lineLength = doc.getLineLength(lineNo);
    String lineOfString = doc.get(lineOffset, lineLength);

    // search for a line comment in this line
    int indexComment = lineOfString.indexOf("//");

    if (indexComment == -1) {
        return false;
    }

    String comment = lineOfString.substring(indexComment);

    // remove first "//" of line comment
    comment = comment.substring(2).toLowerCase();

    // split line comments, necessary if more NON-NLS comments exist in one line, eg.: $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3
    String[] comments = comment.split("//");

    for (String commentFrag : comments) {
        commentFrag = commentFrag.trim();

        // if comment match format: "$non-nls$" then ignore whole line
        if (commentFrag.matches("^\\$non-nls\\$$")) {
            return true;

            // if comment match format: "$non-nls-{number}$" then only
            // ignore string which is on given position
        } else if (commentFrag.matches("^\\$non-nls-\\d+\\$$")) {
            int iString = findNonInternationalisationPosition(cu, doc, literal.getStartPosition()) + 1;
            int iComment = new Integer(commentFrag.substring(9, 10));
            if (iString == iComment) {
                return true;
            }
        }
    }
    return false;
}