From bd2464fb65ff90cfc85b8a680649554ca0557afa Mon Sep 17 00:00:00 2001 From: Dominik Ruf Date: Thu, 30 Nov 2017 16:16:04 +0100 Subject: [PATCH 1/4] fix #1277 ERROR: sonar.cxx.other.xslt.1.inputs file is not defined. --- .../cxx/sensors/other/CxxOtherSensor.java | 36 ++++++++++--------- .../cxx/sensors/other/CxxOtherSensorTest.java | 12 +++++++ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java index 1b91259102..25d0733fd4 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java @@ -24,10 +24,12 @@ import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; + import javax.annotation.Nullable; import javax.xml.stream.XMLStreamException; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; + import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.batch.sensor.SensorContext; @@ -35,8 +37,8 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.cxx.CxxLanguage; -import org.sonar.cxx.sensors.utils.CxxReportSensor; import org.sonar.cxx.sensors.utils.CxxUtils; +import org.sonar.cxx.sensors.utils.CxxReportSensor; import org.sonar.cxx.sensors.utils.StaxParser; /** @@ -45,7 +47,6 @@ * @author jorge costa, stefan weiser */ public class CxxOtherSensor extends CxxReportSensor { - private static final int MAX_STYLESHEETS = 10; private static final Logger LOG = Loggers.get(CxxOtherSensor.class); public static final String REPORT_PATH_KEY = "other.reportPath"; @@ -56,8 +57,7 @@ public class CxxOtherSensor extends CxxReportSensor { public static final String OUTPUT_KEY = ".outputs"; /** - * CxxOtherSensor for Other Sensor - * + * CxxOtherSensor for Other Sensor * @param language defines settings C or C++ */ public CxxOtherSensor(CxxLanguage language) { @@ -84,8 +84,8 @@ public void execute(SensorContext context) { } @Override - public void processReport(final SensorContext context, File report) throws XMLStreamException, IOException, - URISyntaxException, TransformerException { + public void processReport(final SensorContext context, File report) throws XMLStreamException, IOException, + URISyntaxException, TransformerException { LOG.debug("Parsing 'other' format"); StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { @@ -115,7 +115,7 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { @Override protected String getSensorKey() { return KEY; - } + } public void transformFiles(final File baseDir, SensorContext context) { boolean goOn = true; @@ -124,27 +124,28 @@ public void transformFiles(final File baseDir, SensorContext context) { String inputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + INPUT_KEY); String outputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + OUTPUT_KEY); - if (stylesheetKey == null) { + if (stylesheetKey==null) { LOG.error("'{}' is not defined.", OTHER_XSLT_KEY + i + STYLESHEET_KEY); break; } String stylesheet = resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null)); + List inputs = getReports(context.config(), baseDir, inputKey); String[] outputStrings = null; if (outputKey != null) { outputStrings = context.config().getStringArray(outputKey); } - List outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[]{}); + List outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[] {}); - if (stylesheet == null && inputKey == null && outputKey == null) { + if ((stylesheet == null) && (inputs.isEmpty()) && (outputs.isEmpty())) { goOn = false; } else { if (stylesheet == null) { LOG.error(stylesheetKey + " is not defined."); goOn = false; } else { - goOn = checkInput(inputKey, outputKey, inputs, outputs); + goOn = checkInput(inputKey, outputKey, inputs, outputs); } } @@ -161,9 +162,9 @@ public void transformFiles(final File baseDir, SensorContext context) { } private static boolean checkInput(String inputKey, String outputKey, @Nullable List inputs, - @Nullable List outputs) { + @Nullable List outputs) { return isValidInput(inputKey, inputs) && isValidOutput(outputKey, outputs) && hasCorrectSize(inputs, outputs); - } + } /** * @param inputs @@ -174,7 +175,7 @@ private static boolean hasCorrectSize(List inputs, List outputs) { if (inputs.size() != outputs.size()) { LOG.error("Number of source XML files is not equal to the the number of output files."); return false; - } + } return true; } @@ -184,14 +185,14 @@ private static boolean hasCorrectSize(List inputs, List outputs) { * @return */ private static boolean isValidOutput(@Nullable String outputKey, @Nullable List outputs) { - if ((outputKey == null) || (outputs == null) || (outputs.isEmpty())) { + if ((outputKey==null) ||(outputs == null) || (outputs.isEmpty())) { if (outputKey != null) { LOG.error(outputKey + " file is not defined."); } else { LOG.error("outputKey is not defined."); } return false; - } + } return true; } @@ -208,7 +209,7 @@ private static boolean isValidInput(@Nullable String inputKey, @Nullable List Date: Thu, 30 Nov 2017 16:26:02 +0100 Subject: [PATCH 2/4] fix #1277 ERROR: sonar.cxx.other.xslt.1.inputs file is not defined. --- .../org/sonar/cxx/sensors/other/CxxOtherSensorTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java index 2527cf7575..4f09c48bff 100644 --- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java +++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java @@ -178,9 +178,10 @@ public void shouldCreateMissingStylesheetMessage() { logTester.clear(); SensorContextTester context = SensorContextTester.create(fs.baseDir()); when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something"); - when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("something"); - - settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml"); + when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("outputs"); + + settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml"); + settings.setProperty("outputs", "outputs"); context.setSettings(settings); context.fileSystem().add(TestInputFileBuilder.create("ProjectKey", "sources/utils/code_chunks.cpp") From 5501d65986c552367a5ecbbe7663f6327db79df3 Mon Sep 17 00:00:00 2001 From: guwirth Date: Thu, 28 Dec 2017 16:30:45 +0100 Subject: [PATCH 3/4] fix #1277 - ERROR: sonar.cxx.other.xslt.1.inputs file is not defined - solution based on #1348: close #1348 --- .../cxx/sensors/other/CxxOtherSensor.java | 99 ++++++++----------- .../cxx/sensors/other/CxxOtherSensorTest.java | 28 +++--- 2 files changed, 55 insertions(+), 72 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java index 25d0733fd4..708aa9e055 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -47,6 +48,7 @@ * @author jorge costa, stefan weiser */ public class CxxOtherSensor extends CxxReportSensor { + private static final int MAX_STYLESHEETS = 10; private static final Logger LOG = Loggers.get(CxxOtherSensor.class); public static final String REPORT_PATH_KEY = "other.reportPath"; @@ -57,7 +59,8 @@ public class CxxOtherSensor extends CxxReportSensor { public static final String OUTPUT_KEY = ".outputs"; /** - * CxxOtherSensor for Other Sensor + * CxxOtherSensor for Other Sensor + * * @param language defines settings C or C++ */ public CxxOtherSensor(CxxLanguage language) { @@ -84,28 +87,21 @@ public void execute(SensorContext context) { } @Override - public void processReport(final SensorContext context, File report) throws XMLStreamException, IOException, - URISyntaxException, TransformerException { + public void processReport(final SensorContext context, File report) throws XMLStreamException, IOException, + URISyntaxException, TransformerException { LOG.debug("Parsing 'other' format"); - StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { - - /** - * {@inheritDoc} - */ - @Override - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); + StaxParser parser = new StaxParser((SMHierarchicCursor rootCursor) -> { + rootCursor.advance(); - SMInputCursor errorCursor = rootCursor.childElementCursor("error"); - while (errorCursor.getNext() != null) { - String file = errorCursor.getAttrValue("file"); - String line = errorCursor.getAttrValue("line"); - String id = errorCursor.getAttrValue("id"); - String msg = errorCursor.getAttrValue("msg"); + SMInputCursor errorCursor = rootCursor.childElementCursor("error"); + while (errorCursor.getNext() != null) { + String file = errorCursor.getAttrValue("file"); + String line = errorCursor.getAttrValue("line"); + String id = errorCursor.getAttrValue("id"); + String msg = errorCursor.getAttrValue("msg"); - saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg); - } + saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg); } }); @@ -115,56 +111,44 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { @Override protected String getSensorKey() { return KEY; - } + } public void transformFiles(final File baseDir, SensorContext context) { - boolean goOn = true; - for (int i = 1; (i < MAX_STYLESHEETS) && goOn; i++) { + for (int i = 1; i < MAX_STYLESHEETS; i++) { String stylesheetKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + STYLESHEET_KEY); String inputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + INPUT_KEY); String outputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + OUTPUT_KEY); - if (stylesheetKey==null) { - LOG.error("'{}' is not defined.", OTHER_XSLT_KEY + i + STYLESHEET_KEY); - break; - } - String stylesheet = resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null)); + String stylesheet = stylesheetKey == null ? null : resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null)); + List inputs = inputKey == null ? new ArrayList<>() : getReports(context.config(), baseDir, inputKey); + String[] outputStrings = outputKey == null ? null : context.config().getStringArray(outputKey); + List outputs = outputStrings == null ? new ArrayList<>() : Arrays.asList(outputStrings); + if ((stylesheet == null) && (inputs.isEmpty()) && (outputs.isEmpty())) { + break; // no or last item + } - List inputs = getReports(context.config(), baseDir, inputKey); - String[] outputStrings = null; - if (outputKey != null) { - outputStrings = context.config().getStringArray(outputKey); + if (stylesheet == null) { + LOG.error(stylesheetKey + " is not defined."); + break; } - List outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[] {}); - if ((stylesheet == null) && (inputs.isEmpty()) && (outputs.isEmpty())) { - goOn = false; - } else { - if (stylesheet == null) { - LOG.error(stylesheetKey + " is not defined."); - goOn = false; - } else { - goOn = checkInput(inputKey, outputKey, inputs, outputs); - } + if (!checkInput(inputKey, outputKey, inputs, outputs)) { + break; } - if (goOn) { - if (LOG.isDebugEnabled()) { - LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + "."); - } - File stylesheetFile = new File(stylesheet); - if (stylesheetFile.isAbsolute()) { - transformFileList(baseDir.getAbsolutePath(), stylesheetFile, inputs, outputs); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + "."); } + + transformFileList(baseDir.getAbsolutePath(), stylesheet, inputs, outputs); } } private static boolean checkInput(String inputKey, String outputKey, @Nullable List inputs, - @Nullable List outputs) { + @Nullable List outputs) { return isValidInput(inputKey, inputs) && isValidOutput(outputKey, outputs) && hasCorrectSize(inputs, outputs); - } + } /** * @param inputs @@ -175,7 +159,7 @@ private static boolean hasCorrectSize(List inputs, List outputs) { if (inputs.size() != outputs.size()) { LOG.error("Number of source XML files is not equal to the the number of output files."); return false; - } + } return true; } @@ -185,14 +169,14 @@ private static boolean hasCorrectSize(List inputs, List outputs) { * @return */ private static boolean isValidOutput(@Nullable String outputKey, @Nullable List outputs) { - if ((outputKey==null) ||(outputs == null) || (outputs.isEmpty())) { + if ((outputKey == null) || (outputs == null) || (outputs.isEmpty())) { if (outputKey != null) { LOG.error(outputKey + " file is not defined."); } else { LOG.error("outputKey is not defined."); } return false; - } + } return true; } @@ -209,16 +193,16 @@ private static boolean isValidInput(@Nullable String inputKey, @Nullable List inputs, List outputs) { + private void transformFileList(final String baseDir, String stylesheet, List inputs, List outputs) { for (int j = 0; j < inputs.size(); j++) { try { String normalizedOutputFilename = resolveFilename(baseDir, outputs.get(j)); - CxxUtils.transformFile(new StreamSource(stylesheetFile), inputs.get(j), new File(normalizedOutputFilename)); + CxxUtils.transformFile(new StreamSource(new File(stylesheet)), inputs.get(j), new File(normalizedOutputFilename)); } catch (TransformerException e) { String msg = new StringBuilder() .append("Cannot transform report files: '") @@ -231,4 +215,3 @@ private void transformFileList(final String baseDir, File stylesheetFile, List Date: Sat, 30 Dec 2017 14:13:05 +0100 Subject: [PATCH 4/4] rebase --- .../main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java index 708aa9e055..546d95d97a 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java @@ -25,12 +25,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import javax.annotation.Nullable; import javax.xml.stream.XMLStreamException; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; - import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.batch.sensor.SensorContext; @@ -38,8 +36,8 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.cxx.CxxLanguage; -import org.sonar.cxx.sensors.utils.CxxUtils; import org.sonar.cxx.sensors.utils.CxxReportSensor; +import org.sonar.cxx.sensors.utils.CxxUtils; import org.sonar.cxx.sensors.utils.StaxParser; /**