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.OutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.PrintWriter;
26  import java.nio.charset.StandardCharsets;
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 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       */
60      public XpathFileGeneratorAuditListener(OutputStream out,
61                                             OutputStreamOptions outputStreamOptions) {
62          writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
63          closeStream = outputStreamOptions == OutputStreamOptions.CLOSE;
64      }
65  
66      @Override
67      public void auditStarted(AuditEvent event) {
68          // No code by default
69      }
70  
71      @Override
72      public void auditFinished(AuditEvent event) {
73          if (isXmlHeaderPrinted) {
74              writer.println("</suppressions>");
75          }
76  
77          writer.flush();
78          if (closeStream) {
79              writer.close();
80          }
81      }
82  
83      @Override
84      public void fileStarted(AuditEvent event) {
85          // No code by default
86      }
87  
88      @Override
89      public void fileFinished(AuditEvent event) {
90          // No code by default
91      }
92  
93      @Override
94      public void addError(AuditEvent event) {
95          final String xpathQuery = XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(event);
96          if (xpathQuery != null) {
97              printXmlHeader();
98  
99              final File file = new File(event.getFileName());
100 
101             writer.println("<suppress-xpath");
102             writer.print("       files=\"");
103             writer.print(file.getName());
104             writer.println(QUOTE_CHAR);
105 
106             if (event.getModuleId() == null) {
107                 final String checkName =
108                         PackageObjectFactory.getShortFromFullModuleNames(event.getSourceName());
109                 writer.print("       checks=\"");
110                 writer.print(checkName);
111             }
112             else {
113                 writer.print("       id=\"");
114                 writer.print(event.getModuleId());
115             }
116             writer.println(QUOTE_CHAR);
117 
118             writer.print("       query=\"");
119             writer.print(xpathQuery);
120 
121             writer.println("\"/>");
122         }
123     }
124 
125     @Override
126     public void addException(AuditEvent event, Throwable throwable) {
127         throw new UnsupportedOperationException("Operation is not supported");
128     }
129 
130     /**
131      * Prints XML header if only it was not printed before.
132      */
133     private void printXmlHeader() {
134         if (!isXmlHeaderPrinted) {
135             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
136             writer.println("<!DOCTYPE suppressions PUBLIC");
137             writer.println("    \"-//Checkstyle//DTD SuppressionXpathFilter Experimental "
138                     + "Configuration 1.2//EN\"");
139             writer.println("    \"https://checkstyle.org/dtds/"
140                     + "suppressions_1_2_xpath_experimental.dtd\">");
141             writer.println("<suppressions>");
142             isXmlHeaderPrinted = true;
143         }
144     }
145 
146     @Override
147     protected void finishLocalSetup() {
148         // No code by default
149     }
150 }