From dbe5473cfc8d199d745293f4fa75cb3cad14a653 Mon Sep 17 00:00:00 2001 From: Bert Date: Sat, 4 Nov 2017 17:19:40 +0100 Subject: [PATCH 1/2] suppress error message (input file is not available) #1277 --- .../java/org/sonar/cxx/sensors/other/CxxOtherSensor.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 cd139cb1d1..37b18f43ca 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 @@ -48,6 +48,7 @@ * @author jorge costa, stefan weiser */ public class CxxOtherSensor extends CxxReportSensor { + static final int MAX_STYLESHEETS = 10; private static final Logger LOG = Loggers.get(CxxOtherSensor.class); public static final String REPORT_PATH_KEY = "other.reportPath"; public static final String KEY = "other"; @@ -118,7 +119,7 @@ protected String getSensorKey() { public void transformFiles(final File baseDir, SensorContext context) { boolean goOn = true; - for (int i = 1; (i < 10) && goOn; i++) { + for (int i = 1; (i < MAX_STYLESHEETS) && goOn; 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); @@ -156,7 +157,9 @@ public void transformFiles(final File baseDir, SensorContext context) { private static boolean checkInput(String inputKey, String outputKey, @Nullable List inputs, @Nullable List outputs) { if ((inputs == null) || (inputs.isEmpty())) { - LOG.error(inputKey + " file is not defined."); + if (LOG.isDebugEnabled()) { + LOG.debug(inputKey + " file is not defined."); + } return false; } From 0476d7f8e1946e4d5d77e603a3045042deeb8729 Mon Sep 17 00:00:00 2001 From: Bert Date: Sun, 5 Nov 2017 20:16:32 +0100 Subject: [PATCH 2/2] improve input check --- .../cxx/sensors/other/CxxOtherSensor.java | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 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 37b18f43ca..a299ded359 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 @@ -48,7 +48,7 @@ * @author jorge costa, stefan weiser */ public class CxxOtherSensor extends CxxReportSensor { - static final int MAX_STYLESHEETS = 10; + 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"; public static final String KEY = "other"; @@ -135,11 +135,15 @@ public void transformFiles(final File baseDir, SensorContext context) { String[] outputStrings = context.settings().getStringArray(outputKey); List outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[] {}); - if (stylesheet == null) { - LOG.error(stylesheetKey + " is not defined."); + if (stylesheet == null && inputKey==null && outputKey==null) { goOn = false; } else { - goOn = checkInput(inputKey, outputKey, inputs, outputs); + if (stylesheet == null) { + LOG.error(stylesheetKey + " is not defined."); + goOn = false; + } else { + goOn = checkInput(inputKey, outputKey, inputs, outputs); + } } if (goOn) { @@ -156,23 +160,44 @@ public void transformFiles(final File baseDir, SensorContext context) { private static boolean checkInput(String inputKey, String outputKey, @Nullable List inputs, @Nullable List outputs) { - if ((inputs == null) || (inputs.isEmpty())) { - if (LOG.isDebugEnabled()) { - LOG.debug(inputKey + " file is not defined."); - } + return isValidInput(inputKey, inputs) && isValidOutput(outputKey, outputs) && hasCorrectSize(inputs, outputs); + } + + /** + * @param inputs + * @param outputs + * @return + */ + 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; + } + /** + * @param outputKey + * @param outputs + * @return + */ + private static boolean isValidOutput(String outputKey, @Nullable List outputs) { if ((outputs == null) || (outputs.isEmpty())) { LOG.error(outputKey + " is not defined."); return false; } + return true; + } - if (inputs.size() != outputs.size()) { - LOG.error("Number of source XML files is not equal to the the number of output files."); + /** + * @param inputKey + * @param inputs + */ + private static boolean isValidInput(String inputKey, @Nullable List inputs) { + if ((inputs == null) || (inputs.isEmpty())) { + LOG.error(inputKey + " file is not defined."); return false; - } - + } return true; }