1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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("^([0-9]+):([0-9]+)$");
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       * @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 CheckstyleException if the file is not a Java source.
62       */
63      public static String printSuppressions(File file, String suppressionLineColumnNumber,
64                                             int tabWidth) throws IOException, CheckstyleException {
65          final Matcher matcher =
66                  VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX.matcher(suppressionLineColumnNumber);
67          if (matcher.matches()) {
68              final FileText fileText = new FileText(file.getAbsoluteFile(),
69                      System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
70              final DetailAST detailAST =
71                      JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS);
72              final int lineNumber = Integer.parseInt(matcher.group(1));
73              final int columnNumber = Integer.parseInt(matcher.group(2));
74              return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth);
75          }
76          else {
77              final String exceptionMsg = String.format(Locale.ROOT,
78                      "%s does not match valid format 'line:column'.",
79                      suppressionLineColumnNumber);
80              throw new IllegalStateException(exceptionMsg);
81          }
82      }
83  
84      /**
85       * Creates {@code XpathQueryGenerator} instance and generates suppressions.
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 }