-
Notifications
You must be signed in to change notification settings - Fork 360
/
XlstSensor.java
140 lines (122 loc) · 5.25 KB
/
XlstSensor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* C++ Community Plugin (cxx plugin)
* Copyright (C) 2010-2023 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.prejobs;
import java.io.File;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.FilenameUtils;
import org.sonar.api.batch.Phase;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.scanner.sensor.ProjectSensor;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.sensors.utils.CxxUtils;
@Phase(name = Phase.Name.PRE)
public class XlstSensor implements ProjectSensor {
public static final String OTHER_XSLT_KEY = "sonar.cxx.xslt.";
public static final String STYLESHEET_KEY = ".stylesheet";
public static final String INPUT_KEY = ".inputs";
public static final String OUTPUT_KEY = ".outputs";
private static final String MISSING_VALUE = "XLST: '{}' value is not defined.";
private static final Logger LOG = Loggers.get(XlstSensor.class);
private static final int MAX_STYLESHEETS = 10;
private SensorContext context;
private static void transformFile(Source stylesheetFile, File input, File output) throws TransformerException {
var factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
var transformer = factory.newTransformer(stylesheetFile);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new StreamSource(input), new StreamResult(output));
}
@Override
public void describe(SensorDescriptor descriptor) {
descriptor
.name("XSL transformation(s)")
.onlyWhenConfiguration(conf -> conf.hasKey(OTHER_XSLT_KEY + 1 + STYLESHEET_KEY));
}
@Override
public void execute(SensorContext context) {
this.context = context;
for (var i = 1; i <= MAX_STYLESHEETS; i++) {
var paramError = false;
String stylesheetKey = OTHER_XSLT_KEY + i + STYLESHEET_KEY;
String inputKey = OTHER_XSLT_KEY + i + INPUT_KEY;
String outputKey = OTHER_XSLT_KEY + i + OUTPUT_KEY;
if (!context.config().hasKey(stylesheetKey)
&& !context.config().hasKey(inputKey)
&& !context.config().hasKey(outputKey)) {
break; // no or last item
}
String stylesheet = context.config().get(stylesheetKey).orElse("");
if (stylesheet.isEmpty()) {
LOG.error(MISSING_VALUE, stylesheetKey);
paramError = true;
}
List<File> inputs = CxxUtils.getFiles(context, inputKey);
if (inputs.isEmpty()) {
LOG.error(MISSING_VALUE, inputKey);
paramError = true;
}
String outputs = context.config().get(outputKey).orElse("");
if (outputs.isEmpty()) {
LOG.error(MISSING_VALUE, outputKey);
paramError = true;
}
if (paramError) {
break;
}
LOG.debug("XLST: Converting '{}' with '{}' to '{}'.", inputs, stylesheet, outputs);
transformFileList(context.fileSystem().baseDir().getAbsolutePath(), stylesheet, inputs, outputs);
}
}
private void transformFileList(String baseDir, String stylesheet, List<File> inputs, String outputs) {
for (var j = 0; j < inputs.size(); j++) {
try {
var inputStream = this.getClass().getResourceAsStream("/xsl/" + stylesheet);
Source stylesheetFile;
if (inputStream != null) {
stylesheetFile = new StreamSource(inputStream);
} else {
stylesheetFile = new StreamSource(new File(CxxUtils.resolveAntPath(baseDir, stylesheet)));
}
var inputFile = inputs.get(j);
var outputFile = createOutputFile(inputFile.getPath(), outputs);
transformFile(stylesheetFile, inputFile, outputFile);
} catch (TransformerException | NullPointerException e) {
CxxUtils.validateRecovery("Cannot XLS transform files", e, context.config());
}
}
}
private File createOutputFile(String path, String outputs) {
if (outputs.contains("*")) {
outputs = outputs.replace("*", FilenameUtils.getBaseName(path));
}
return new File(FilenameUtils.getFullPath(path), outputs);
}
}