| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.sourceforge.xradar.ant; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import org.apache.tools.ant.BuildException; |
| 10 | import org.apache.tools.ant.Task; |
| 11 | import org.sourceforge.xradar.XRadarException; |
| 12 | import org.sourceforge.xradar.results.AnalysingProcess; |
| 13 | import org.sourceforge.xradar.results.Rule; |
| 14 | import org.sourceforge.xradar.results.Violation; |
| 15 | import org.sourceforge.xradar.results.XRadarResultsAnalyserException; |
| 16 | |
| 17 | /** |
| 18 | * |
| 19 | * <code> |
| 20 | * <xradar-analyser failOnError="true|false"> |
| 21 | * <rule name="rule1" type="xpath" query="//xpath"/> |
| 22 | * <rule name="rule2" type="xpath" query="//xpath"/> |
| 23 | * ... |
| 24 | * </xradar-analyser> |
| 25 | * </code> |
| 26 | * @author Romain PELISSE, belaran@gmail.com |
| 27 | * |
| 28 | * |
| 29 | * |
| 30 | */ |
| 31 | public class ResultsAnalyserTask extends Task { |
| 32 | |
| 33 | private List<Rule> rules; |
| 34 | private boolean failOnError = false; |
| 35 | |
| 36 | public void addRule(Rule rule) |
| 37 | { |
| 38 | if ( this.rules == null ) |
| 39 | this.rules = new ArrayList<Rule>(); |
| 40 | this.rules.add(rule); |
| 41 | } |
| 42 | |
| 43 | public boolean isFailOnError() { |
| 44 | return failOnError; |
| 45 | } |
| 46 | |
| 47 | public void setFailOnError(boolean failOnError) { |
| 48 | this.failOnError = failOnError; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void execute() throws BuildException { |
| 53 | validate(); |
| 54 | AnalysingProcess analysingEngine = new AnalysingProcess(); |
| 55 | analysingEngine.setViolations(new ArrayList<Violation>()); |
| 56 | try { |
| 57 | List<Violation> violations = analysingEngine.analyseResults(rules); |
| 58 | |
| 59 | for ( Violation violation : violations ) { |
| 60 | log(violation.toString()); |
| 61 | if ( failOnError ) |
| 62 | throw new BuildException("XRadar analyser violation:" + violation); |
| 63 | } |
| 64 | } catch (XRadarResultsAnalyserException e) { |
| 65 | throw new BuildException(e); |
| 66 | } catch (XRadarException e) { |
| 67 | throw new BuildException(e); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private void validate() throws BuildException { |
| 72 | if ( rules == null || rules.size() <= 0 ) |
| 73 | throw new BuildException("at least on nested element 'rule' is required."); |
| 74 | } |
| 75 | |
| 76 | public List<Rule> getRules() { |
| 77 | return rules; |
| 78 | } |
| 79 | |
| 80 | public void setRules(List<Rule> rules) { |
| 81 | this.rules = rules; |
| 82 | } |
| 83 | } |