1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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.nio.charset.Charset;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.stream.Collectors;
30
31 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32 import com.puppycrawl.tools.checkstyle.api.DetailAST;
33 import com.puppycrawl.tools.checkstyle.api.FileText;
34 import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
35
36 /**
37 * Class for constructing xpath queries to suppress nodes
38 * with specified line and column number.
39 */
40 public final class SuppressionsStringPrinter {
41
42 /** Line and column number config value pattern. */
43 private static final Pattern VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX =
44 Pattern.compile("^(\\d+):(\\d+)$");
45
46 /** OS specific line separator. */
47 private static final String LINE_SEPARATOR = System.lineSeparator();
48
49 /** Prevent instances. */
50 private SuppressionsStringPrinter() {
51 // no code
52 }
53
54 /**
55 * Prints generated suppressions.
56 *
57 * @param file the file to process.
58 * @param suppressionLineColumnNumber line and column number of the suppression
59 * @param tabWidth length of the tab character
60 * @return generated suppressions.
61 * @throws IOException if the file could not be read.
62 * @throws IllegalStateException if suppressionLineColumnNumber is not of a valid format.
63 * @throws CheckstyleException if the file is not a Java source.
64 */
65 public static String printSuppressions(File file, String suppressionLineColumnNumber,
66 int tabWidth) throws IOException, CheckstyleException {
67 final Matcher matcher =
68 VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX.matcher(suppressionLineColumnNumber);
69 if (!matcher.matches()) {
70 final String exceptionMsg = String.format(Locale.ROOT,
71 "%s does not match valid format 'line:column'.",
72 suppressionLineColumnNumber);
73 throw new IllegalStateException(exceptionMsg);
74 }
75
76 final FileText fileText = new FileText(file, Charset.defaultCharset().name());
77 final DetailAST detailAST =
78 JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS);
79 final int lineNumber = Integer.parseInt(matcher.group(1));
80 final int columnNumber = Integer.parseInt(matcher.group(2));
81 return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth);
82 }
83
84 /**
85 * Creates {@code XpathQueryGenerator} instance and generates suppressions.
86 *
87 * @param fileText {@code FileText} object.
88 * @param detailAST {@code DetailAST} object.
89 * @param lineNumber line number.
90 * @param columnNumber column number.
91 * @param tabWidth length of the tab character.
92 * @return generated suppressions.
93 */
94 private static String generate(FileText fileText, DetailAST detailAST, int lineNumber,
95 int columnNumber, int tabWidth) {
96 final XpathQueryGenerator queryGenerator =
97 new XpathQueryGenerator(detailAST, lineNumber, columnNumber, fileText,
98 tabWidth);
99 final List<String> suppressions = queryGenerator.generate();
100 return suppressions.stream().collect(Collectors.joining(LINE_SEPARATOR,
101 "", LINE_SEPARATOR));
102 }
103 }