All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dantwining.whitespace.WhitespaceUtils Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package com.github.dantwining.whitespace;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class WhitespaceUtils {

	private WhitespaceUtils() {
		throw new AssertionError("Utility class: should never be instantiated");
	}

    public static void detectWhitespace(boolean verify, File searchBaseDirectory, Log mavenLog) throws MojoExecutionException, MojoFailureException {

		String[] extensions = {"java", "xml"};
		Collection matchingFiles = FileUtils.listFiles(searchBaseDirectory, extensions, true);

        if (!searchBaseDirectory.isDirectory()) {
            mavenLog.debug("Skipping non-existent directory: " + searchBaseDirectory.getAbsolutePath());
            return;
        }
        for (File matchingFile : matchingFiles) {
            mavenLog.debug("Reading file: " + matchingFile.getAbsolutePath());

			List lines;
			try {
				lines = FileUtils.readLines(matchingFile, "UTF-8");
			} catch (IOException e) {
				throw new MojoExecutionException("Failed to read lines from " + matchingFile.getAbsolutePath(), e);
			}

			Boolean isFileModified = false;
			List trimmedLines = new ArrayList(lines.size());
			for (String line : lines) {

				String trimmedLine = StringUtils.stripEnd(line, null);

				Boolean isLineModified = (trimmedLine.equals(line));
				isFileModified = (isFileModified || isLineModified);

				trimmedLines.add(trimmedLine);

			}

			if (isFileModified) {

				if (verify) {
					throw new MojoFailureException("Trailing whitespace found in " + matchingFile.getAbsolutePath());
				}
				try {
					FileUtils.writeLines(matchingFile, "UTF-8", trimmedLines);
				} catch (IOException e) {
					throw new MojoExecutionException("Failed to write lines to " + matchingFile.getAbsolutePath(), e);
				}

			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy