1 | |
2 | /** |
3 | * |
4 | */ |
5 | package org.sourceforge.xradar.results; |
6 | |
7 | import java.io.InputStream; |
8 | import java.util.HashMap; |
9 | import java.util.List; |
10 | import java.util.Map; |
11 | import java.util.logging.Logger; |
12 | |
13 | import javax.xml.parsers.DocumentBuilder; |
14 | |
15 | import org.sourceforge.xradar.XRadarException; |
16 | import org.sourceforge.xradar.util.FileUtils; |
17 | import org.sourceforge.xradar.util.StreamUtils; |
18 | import org.sourceforge.xradar.util.XMLUtils; |
19 | import org.w3c.dom.Document; |
20 | import org.w3c.dom.Element; |
21 | import org.w3c.dom.NodeList; |
22 | import org.xml.sax.SAXException; |
23 | |
24 | /** |
25 | * |
26 | * @author Romain PELISSE, <belaran@gmail.com> |
27 | */ |
28 | public class AnalysingProcess { |
29 | |
30 | private static final Logger logger = Logger.getLogger(AnalysingProcess.class.getSimpleName()); |
31 | private List<Violation> violations; |
32 | |
33 | private Map<String,Analyser> alreadyLoaded = new HashMap<String,Analyser>(0); |
34 | private Map<String,InputStream> resourcesLoaded = new HashMap<String, InputStream>(0); |
35 | |
36 | private boolean isRuleHasNameAndQueryDefined(Rule rule) { |
37 | |
38 | return ! "".equals(rule.getName()) && ! "".equals(rule.getQuery()) ; |
39 | } |
40 | |
41 | public List<Violation> analyseResults(List<Rule> rules) throws XRadarResultsAnalyserException, XRadarException { |
42 | logger.finest("invoked"); |
43 | if ( violations == null ) |
44 | throw new XRadarResultsAnalyserException("initialization error, please set 'violations' through appropriate getter"); |
45 | logger.finest("called with violations=" + violations); |
46 | for (Rule rule : rules ) { |
47 | logger.finest("descriptor:" + rule.toString()); |
48 | if ( rule.getType() == null || "".equals(rule.getType() ) ) |
49 | throw new XRadarResultsAnalyserException("empty or null analyser:"); |
50 | Analyser analyser = this.getAnalyserInstance(rule); |
51 | violations.addAll(addRuleToViolations(analyser.analyse(new QueryData(rule.getQuery(),loadDataFile(rule.getFile()))),rule)); |
52 | } |
53 | return violations; |
54 | } |
55 | |
56 | private InputStream loadDataFile(String filename) throws XRadarException { |
57 | InputStream inputStream = null; |
58 | if (! resourcesLoaded.containsKey(filename) && FileUtils.exists(filename) ) { |
59 | inputStream = StreamUtils.fromFilenameToInputStream(filename); |
60 | resourcesLoaded.put(filename,inputStream); |
61 | } else |
62 | inputStream = resourcesLoaded.get(filename); |
63 | return inputStream; |
64 | } |
65 | |
66 | private List<Violation> addRuleToViolations(List<Violation> violations, Rule rule) { |
67 | for ( Violation violation : violations ) { |
68 | violation.setRule(rule); |
69 | } |
70 | return violations; |
71 | } |
72 | |
73 | public static String findClassNameForType(String type) { |
74 | if ( "xpath".equals(type) ) |
75 | return XPathQueryAnalyser.class.getCanonicalName(); |
76 | return type; |
77 | } |
78 | |
79 | public List<Violation> getViolations() { |
80 | return violations; |
81 | } |
82 | |
83 | public void setViolations(List<Violation> violations) { |
84 | this.violations = violations; |
85 | } |
86 | |
87 | /** |
88 | * <p>As analyser implementation are supposed to be rather 'stateless', |
89 | * they are only instatiate once, and this method provide access to the |
90 | * already instantiated Analyser instance (or create the appropriate |
91 | * instance.</p> |
92 | * |
93 | * @param type |
94 | * @return the instance associated with the type |
95 | * @throws XRadarResultsAnalyserException |
96 | */ |
97 | public Analyser getAnalyserInstance(Rule rule) throws XRadarResultsAnalyserException { |
98 | Analyser analyser = null; |
99 | |
100 | if ( ! this.alreadyLoaded.containsKey(rule.getType()) ) { |
101 | // Maybe this a predefined rule |
102 | if (!isRuleHasNameAndQueryDefined(rule)){ |
103 | RuleExport re = new RuleExport(); |
104 | alreadyLoaded.putAll(re.listRule(rule)); |
105 | for (int i = 0; i < alreadyLoaded.size(); i++) { |
106 | if (this.alreadyLoaded.containsKey(rule.getType())){ |
107 | analyser = this.alreadyLoaded.get(rule.getType()); |
108 | } |
109 | } |
110 | } |
111 | // |
112 | String type = AnalysingProcess.findClassNameForType(rule.getType()); |
113 | try { |
114 | analyser = (Analyser) this.getClass().getClassLoader().loadClass(type).newInstance(); |
115 | } catch (InstantiationException e) { |
116 | throw new XRadarResultsAnalyserException(e); |
117 | } catch (IllegalAccessException e) { |
118 | throw new XRadarResultsAnalyserException(e); |
119 | } catch (ClassNotFoundException e) { |
120 | throw new XRadarResultsAnalyserException(e); |
121 | } |
122 | } |
123 | else |
124 | analyser = this.alreadyLoaded.get(rule.getType()); |
125 | return analyser; |
126 | |
127 | } |
128 | } |