1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35 public class XpathFileGeneratorAuditListener
36 extends AbstractAutomaticBean
37 implements AuditListener {
38
39
40 private static final String QUOTE_CHAR = "\"";
41
42
43
44
45 private final PrintWriter writer;
46
47
48 private final boolean closeStream;
49
50
51 private boolean isXmlHeaderPrinted;
52
53
54
55
56
57
58
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
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
86 }
87
88 @Override
89 public void fileFinished(AuditEvent event) {
90
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
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
149 }
150 }