View Javadoc
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.OutputStream;
23  import java.io.OutputStreamWriter;
24  import java.io.PrintWriter;
25  import java.nio.charset.StandardCharsets;
26  import java.nio.file.Path;
27  
28  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
29  import com.puppycrawl.tools.checkstyle.api.AuditListener;
30  
31  /**
32   * Generates <b>suppressions.xml</b> file, based on violations occurred.
33   * See issue <a href="https://github.com/checkstyle/checkstyle/issues/102">#102</a>
34   */
35  public final class XpathFileGeneratorAuditListener
36          extends AbstractAutomaticBean
37          implements AuditListener {
38  
39      /** The " quote character. */
40      private static final String QUOTE_CHAR = "\"";
41  
42      /**
43       * Helper writer that allows easy encoding and printing.
44       */
45      private final PrintWriter writer;
46  
47      /** Close output stream in auditFinished. */
48      private final boolean closeStream;
49  
50      /** Determines if xml header is printed. */
51      private boolean isXmlHeaderPrinted;
52  
53      /**
54       * Creates a new {@code SuppressionFileGenerator} instance.
55       * Sets the output to a defined stream.
56       *
57       * @param out the output stream
58       * @param outputStreamOptions if {@code CLOSE} stream should be closed in auditFinished()
59       * @throws IllegalArgumentException if outputStreamOptions is null
60       */
61      public XpathFileGeneratorAuditListener(OutputStream out,
62                                             OutputStreamOptions outputStreamOptions) {
63          if (outputStreamOptions == null) {
64              throw new IllegalArgumentException("Parameter outputStreamOptions can not be null");
65          }
66  
67          writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
68          closeStream = outputStreamOptions == OutputStreamOptions.CLOSE;
69      }
70  
71      @Override
72      public void auditStarted(AuditEvent event) {
73          // No code by default
74      }
75  
76      @Override
77      public void auditFinished(AuditEvent event) {
78          if (isXmlHeaderPrinted) {
79              writer.println("</suppressions>");
80          }
81  
82          writer.flush();
83          if (closeStream) {
84              writer.close();
85          }
86      }
87  
88      @Override
89      public void fileStarted(AuditEvent event) {
90          // No code by default
91      }
92  
93      @Override
94      public void fileFinished(AuditEvent event) {
95          // No code by default
96      }
97  
98      @Override
99      public void addError(AuditEvent event) {
100         final String xpathQuery = XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(event);
101         if (xpathQuery != null) {
102             printXmlHeader();
103 
104             final Path path = Path.of(event.getFileName());
105 
106             writer.println("  <suppress-xpath");
107             writer.print("       files=\"");
108             writer.print(path.getFileName());
109             writer.println(QUOTE_CHAR);
110 
111             if (event.getModuleId() == null) {
112                 final String checkName =
113                         PackageObjectFactory.getShortFromFullModuleNames(event.getSourceName());
114                 writer.print("       checks=\"");
115                 writer.print(checkName);
116             }
117             else {
118                 writer.print("       id=\"");
119                 writer.print(event.getModuleId());
120             }
121             writer.println(QUOTE_CHAR);
122 
123             writer.print("       query=\"");
124             writer.print(xpathQuery);
125 
126             writer.println("\"/>");
127         }
128     }
129 
130     @Override
131     public void addException(AuditEvent event, Throwable throwable) {
132         throw new UnsupportedOperationException("Operation is not supported");
133     }
134 
135     /**
136      * Prints XML header if only it was not printed before.
137      */
138     private void printXmlHeader() {
139         if (!isXmlHeaderPrinted) {
140             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
141             writer.println("<!DOCTYPE suppressions PUBLIC");
142             writer.println("    \"-//Checkstyle//DTD SuppressionXpathFilter "
143                     + "Configuration 1.2//EN\"");
144             writer.println("    \"https://checkstyle.org/dtds/"
145                     + "suppressions_1_2_xpath.dtd\">");
146             writer.println("<suppressions>");
147             isXmlHeaderPrinted = true;
148         }
149     }
150 
151     @Override
152     protected void finishLocalSetup() {
153         // No code by default
154     }
155 }