1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import java.util.stream.Collectors;
29
30 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31 import com.puppycrawl.tools.checkstyle.api.DetailAST;
32 import com.puppycrawl.tools.checkstyle.api.FileText;
33 import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
34
35 /**
36 * Class for constructing xpath queries to suppress nodes
37 * with specified line and column number.
38 */
39 public final class SuppressionsStringPrinter {
40
41 /** Line and column number config value pattern. */
42 private static final Pattern VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX =
43 Pattern.compile("^(\\d+):(\\d+)$");
44
45 /** OS specific line separator. */
46 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
47
48 /** Prevent instances. */
49 private SuppressionsStringPrinter() {
50 // no code
51 }
52
53 /**
54 * Prints generated suppressions.
55 *
56 * @param file the file to process.
57 * @param suppressionLineColumnNumber line and column number of the suppression
58 * @param tabWidth length of the tab character
59 * @return generated suppressions.
60 * @throws IOException if the file could not be read.
61 * @throws IllegalStateException if suppressionLineColumnNumber is not of a valid format.
62 * @throws CheckstyleException if the file is not a Java source.
63 */
64 public static String printSuppressions(File file, String suppressionLineColumnNumber,
65 int tabWidth) throws IOException, CheckstyleException {
66 final Matcher matcher =
67 VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX.matcher(suppressionLineColumnNumber);
68 if (!matcher.matches()) {
69 final String exceptionMsg = String.format(Locale.ROOT,
70 "%s does not match valid format 'line:column'.",
71 suppressionLineColumnNumber);
72 throw new IllegalStateException(exceptionMsg);
73 }
74
75 final FileText fileText = new FileText(file, System.getProperty("file.encoding"));
76 final DetailAST detailAST =
77 JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS);
78 final int lineNumber = Integer.parseInt(matcher.group(1));
79 final int columnNumber = Integer.parseInt(matcher.group(2));
80 return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth);
81 }
82
83 /**
84 * Creates {@code XpathQueryGenerator} instance and generates suppressions.
85 *
86 * @param fileText {@code FileText} object.
87 * @param detailAST {@code DetailAST} object.
88 * @param lineNumber line number.
89 * @param columnNumber column number.
90 * @param tabWidth length of the tab character.
91 * @return generated suppressions.
92 */
93 private static String generate(FileText fileText, DetailAST detailAST, int lineNumber,
94 int columnNumber, int tabWidth) {
95 final XpathQueryGenerator queryGenerator =
96 new XpathQueryGenerator(detailAST, lineNumber, columnNumber, fileText,
97 tabWidth);
98 final List<String> suppressions = queryGenerator.generate();
99 return suppressions.stream().collect(Collectors.joining(LINE_SEPARATOR,
100 "", LINE_SEPARATOR));
101 }
102 }