• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / whitespace-maven-plugin / 559

07 Dec 2025 10:25PM UTC coverage: 18.803% (-0.4%) from 19.186%
559

push

github

hazendaz
[ci] formatting

22 of 140 branches covered (15.71%)

Branch coverage included in aggregate %.

44 of 211 relevant lines covered (20.85%)

0.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/main/java/com/github/dantwining/whitespace/WhitespaceUtils.java
1
/*
2
 * whitespace-maven-plugin (https://github.com/hazendaz/whitespace-maven-plugin)
3
 *
4
 * Copyright 2011-2025 dantwining, Hazendaz.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of The Apache Software License,
8
 * Version 2.0 which accompanies this distribution, and is available at
9
 * https://www.apache.org/licenses/LICENSE-2.0.txt
10
 *
11
 * Contributors:
12
 *     dantwining (dantwining).
13
 *     Hazendaz (Jeremy Landis).
14
 */
15
package com.github.dantwining.whitespace;
16

17
import java.io.File;
18
import java.io.IOException;
19
import java.nio.charset.Charset;
20
import java.nio.file.Files;
21
import java.nio.file.StandardOpenOption;
22
import java.util.ArrayList;
23
import java.util.Collection;
24
import java.util.List;
25

26
import org.apache.commons.io.FileUtils;
27
import org.apache.commons.lang3.StringUtils;
28
import org.apache.maven.plugin.MojoExecutionException;
29
import org.apache.maven.plugin.MojoFailureException;
30
import org.apache.maven.plugin.logging.Log;
31

32
/**
33
 * The Class WhitespaceUtils.
34
 */
35
public final class WhitespaceUtils {
36

37
    /**
38
     * Instantiates a new whitespace utils.
39
     */
40
    private WhitespaceUtils() {
41
        // Do not instantiate
42
    }
43

44
    /**
45
     * Detect whitespace.
46
     *
47
     * @param verify
48
     *            the verify
49
     * @param searchBaseDirectory
50
     *            the search base directory
51
     * @param extensions
52
     *            the extensions
53
     * @param mavenLog
54
     *            the maven log
55
     * @param encoding
56
     *            the character encoding used for resources
57
     * @param failOnReadError
58
     *            the boolean flag to choose treatment for read errors
59
     *
60
     * @throws MojoExecutionException
61
     *             the mojo execution exception
62
     * @throws MojoFailureException
63
     *             the mojo failure exception
64
     */
65
    public static void detectWhitespace(boolean verify, File searchBaseDirectory, String extensions, Log mavenLog,
66
            String encoding, boolean failOnReadError) throws MojoExecutionException, MojoFailureException {
67

68
        if (!searchBaseDirectory.isDirectory()) {
1✔
69
            mavenLog.debug("Skipping non-existent directory: " + searchBaseDirectory.getAbsolutePath());
1✔
70
            return;
1✔
71
        }
72
        String cleanExtensions = StringUtils.deleteWhitespace(extensions);
1✔
73
        mavenLog.info("Processing the following extensions: " + cleanExtensions);
1✔
74

75
        Collection<File> matchingFiles = FileUtils.listFiles(searchBaseDirectory, cleanExtensions.split(","), true);
1✔
76

77
        List<String> verifyFailed = new ArrayList<>();
1✔
78

79
        for (File matchingFile : matchingFiles) {
1✔
80
            mavenLog.debug("Reading file: " + matchingFile.getAbsolutePath());
1✔
81

82
            List<String> lines;
83
            try {
84
                lines = Files.readAllLines(matchingFile.toPath(), Charset.forName(encoding));
1✔
85
            } catch (IOException e) {
×
86
                if (failOnReadError) {
×
87
                    throw new MojoExecutionException(
×
88
                            "Failed to read lines from " + matchingFile.getAbsolutePath() + ": " + e.getMessage(), e);
×
89
                } else {
90
                    mavenLog.warn(
×
91
                            "Failed to read lines from " + matchingFile.getAbsolutePath() + ": " + e.getMessage());
×
92
                    // Skip this file and continue processing others
93
                    continue;
×
94
                }
95
            }
1✔
96

97
            boolean isFileModified = false;
1✔
98
            List<String> trimmedLines = new ArrayList<>(lines.size());
1✔
99
            int lineNumber = 0;
1✔
100

101
            for (String line : lines) {
1✔
102

103
                if (mavenLog.isDebugEnabled()) {
1!
104
                    lineNumber++;
×
105
                }
106

107
                String trimmedLine = StringUtils.stripEnd(line, null);
1✔
108

109
                boolean isLineModified = !trimmedLine.equals(line);
1✔
110

111
                if (mavenLog.isDebugEnabled() && isLineModified) {
1!
112
                    mavenLog.debug("Whitespace found on line " + lineNumber);
×
113
                }
114

115
                trimmedLines.add(trimmedLine);
1✔
116

117
                isFileModified = isFileModified || isLineModified;
1✔
118
            }
1✔
119

120
            if (isFileModified) {
1✔
121

122
                if (verify) {
1✔
123
                    verifyFailed.add(matchingFile.getAbsolutePath());
1✔
124
                } else {
125
                    try {
126
                        Files.write(matchingFile.toPath(), trimmedLines, Charset.forName(encoding),
1✔
127
                                StandardOpenOption.TRUNCATE_EXISTING);
128
                    } catch (IOException e) {
×
129
                        throw new MojoExecutionException("Failed to write lines to " + matchingFile.getAbsolutePath(),
×
130
                                e);
131
                    }
1✔
132
                }
133

134
            }
135
        }
1✔
136

137
        if (!verifyFailed.isEmpty()) {
1✔
138
            throw new MojoFailureException("Trailing whitespace found in " + verifyFailed);
1✔
139
        }
140
    }
1✔
141

142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc