alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (SourceText.java)

This example Groovy source code file (SourceText.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

arraylist, arraylist, invalid, janitor, list, sourcetext, sourcetext, sourcetextnotavailableexception, sourcetextnotavailableexception, sourceunit, string, string, util

The Groovy SourceText.java source code

/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.codehaus.groovy.runtime.powerassert;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.stmt.AssertStatement;
import org.codehaus.groovy.control.Janitor;
import org.codehaus.groovy.control.SourceUnit;

import java.util.ArrayList;
import java.util.List;

/**
 * Provides the source text for an assertion statement and translates
 * coordinates in the original source text to coordinates relative to the
 * assertion's (normalized) source text.
 *
 * @author Peter Niederwieser
 */
public class SourceText {
    private final int firstLine;
    private String normalizedText;

    private final List<Integer> lineOffsets = new ArrayList();
    private final List<Integer> textOffsets = new ArrayList();

    /**
     * Constructs a <tt>SourceText by reading the given assertion's source
     * text from the given source unit.
     *
     * @param stat       an assertion statement
     * @param sourceUnit the source unit containing the assertion statement
     * @param janitor    a <tt>Janitor for cleaning up reader sources
     */
    public SourceText(AssertStatement stat, SourceUnit sourceUnit, Janitor janitor) {
        if (!hasPlausibleSourcePosition(stat))
            throw new SourceTextNotAvailableException(stat, sourceUnit, "Invalid source position");

        firstLine = stat.getLineNumber();
        textOffsets.add(0);
        normalizedText = "";

        for (int line = stat.getLineNumber(); line <= stat.getLastLineNumber(); line++) {
            String lineText = sourceUnit.getSample(line, 0, janitor);
            if (lineText == null)
                throw new SourceTextNotAvailableException(stat, sourceUnit, "SourceUnit.getSample() returned null");

            if (line == stat.getLastLineNumber())
                lineText = lineText.substring(0, stat.getLastColumnNumber() - 1);
            if (line == stat.getLineNumber()) {
                lineText = lineText.substring(stat.getColumnNumber() - 1);
                lineOffsets.add(stat.getColumnNumber() - 1);
            } else
                lineOffsets.add(countLeadingWhitespace(lineText));

            lineText = lineText.trim();
            if (line != stat.getLastLineNumber() && lineText.length() > 0)
                lineText += ' ';
            normalizedText += lineText;
            textOffsets.add(normalizedText.length());
        }
    }

    /**
     * Returns the assertion's source text after removing line breaks.
     * <p>Limitation: Line comments within the assertion's source text are not
     * handled.
     *
     * @return the assertion's source text after removing line breaks.
     */
    public String getNormalizedText() {
        return normalizedText;
    }

    /**
     * Returns the column in <tt>getNormalizedText() corresponding
     * to the given line and column in the original source text. The
     * first character in the normalized text has column 1.
     *
     * @param line   a line number
     * @param column a column number
     * @return the column in getNormalizedText() corresponding to the given line
     *         and column in the original source text
     */
    public int getNormalizedColumn(int line, int column) {
        int deltaLine = line - firstLine;
        if (deltaLine < 0 || deltaLine >= lineOffsets.size()) // wrong line information
            return -1;
        int deltaColumn = column - lineOffsets.get(deltaLine);
        if (deltaColumn < 0) // wrong column information
            return -1;

        return textOffsets.get(deltaLine) + deltaColumn;
    }

    private boolean hasPlausibleSourcePosition(ASTNode node) {
        return node.getLineNumber() > 0
                && node.getColumnNumber() > 0
                && node.getLastLineNumber() >= node.getLineNumber()
                && node.getLastColumnNumber() >
                (node.getLineNumber() == node.getLastLineNumber() ? node.getColumnNumber() : 0);
    }

    private int countLeadingWhitespace(String lineText) {
        int result = 0;
        while (result < lineText.length() && Character.isWhitespace(lineText.charAt(result)))
            result++;
        return result;
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy SourceText.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.