View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.StandardCharsets;
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.getProperty("line.separator");
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.getAbsoluteFile(),
77                  System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
78          final DetailAST detailAST =
79                  JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS);
80          final int lineNumber = Integer.parseInt(matcher.group(1));
81          final int columnNumber = Integer.parseInt(matcher.group(2));
82          return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth);
83      }
84  
85      /**
86       * Creates {@code XpathQueryGenerator} instance and generates suppressions.
87       *
88       * @param fileText {@code FileText} object.
89       * @param detailAST {@code DetailAST} object.
90       * @param lineNumber line number.
91       * @param columnNumber column number.
92       * @param tabWidth length of the tab character.
93       * @return generated suppressions.
94       */
95      private static String generate(FileText fileText, DetailAST detailAST, int lineNumber,
96                                     int columnNumber, int tabWidth) {
97          final XpathQueryGenerator queryGenerator =
98                  new XpathQueryGenerator(detailAST, lineNumber, columnNumber, fileText,
99                          tabWidth);
100         final List<String> suppressions = queryGenerator.generate();
101         return suppressions.stream().collect(Collectors.joining(LINE_SEPARATOR,
102                 "", LINE_SEPARATOR));
103     }
104 }