1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
8 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
9 |
| import net.sourceforge.pmd.ast.SimpleNode; |
10 |
| import net.sourceforge.pmd.jaxen.DocumentNavigator; |
11 |
| import org.jaxen.BaseXPath; |
12 |
| import org.jaxen.JaxenException; |
13 |
| import org.jaxen.SimpleVariableContext; |
14 |
| import org.jaxen.XPath; |
15 |
| |
16 |
| import java.io.PrintStream; |
17 |
| import java.io.PrintWriter; |
18 |
| import java.text.MessageFormat; |
19 |
| import java.util.Iterator; |
20 |
| import java.util.List; |
21 |
| import java.util.Map.Entry; |
22 |
| |
23 |
| public class XPathRule extends AbstractRule { |
24 |
| |
25 |
| private XPath xpath; |
26 |
| |
27 |
324
| public Object visit(ASTCompilationUnit compilationUnit, Object data) {
|
28 |
324
| try {
|
29 |
324
| initializeXPathExpression();
|
30 |
324
| List results = xpath.selectNodes(compilationUnit);
|
31 |
324
| for (Iterator i = results.iterator(); i.hasNext();) {
|
32 |
189
| SimpleNode n = (SimpleNode) i.next();
|
33 |
189
| String msg = getMessage();
|
34 |
189
| if (n instanceof ASTVariableDeclaratorId && getBooleanProperty("pluginname")) {
|
35 |
8
| msg = MessageFormat.format(msg, new Object[]{n.getImage()});
|
36 |
| } |
37 |
189
| addViolation(data, n, msg);
|
38 |
| } |
39 |
| } catch (JaxenException ex) { |
40 |
0
| throwJaxenAsRuntime(ex);
|
41 |
| } |
42 |
324
| return data;
|
43 |
| } |
44 |
| |
45 |
324
| private void initializeXPathExpression() throws JaxenException {
|
46 |
324
| if (xpath != null) {
|
47 |
236
| return;
|
48 |
| } |
49 |
88
| xpath = new BaseXPath(getStringProperty("xpath"), new DocumentNavigator());
|
50 |
88
| if (properties.size() > 1) {
|
51 |
7
| SimpleVariableContext vc = new SimpleVariableContext();
|
52 |
7
| for (Iterator i = properties.entrySet().iterator(); i.hasNext();) {
|
53 |
14
| Entry e = (Entry) i.next();
|
54 |
14
| if (!"xpath".equals(e.getKey())) {
|
55 |
7
| vc.setVariableValue((String)e.getKey(), e.getValue());
|
56 |
| } |
57 |
| } |
58 |
7
| xpath.setVariableContext(vc);
|
59 |
| } |
60 |
| } |
61 |
| |
62 |
0
| private static void throwJaxenAsRuntime(final JaxenException ex) {
|
63 |
0
| throw new RuntimeException() {
|
64 |
0
| public void printStackTrace() {
|
65 |
0
| super.printStackTrace();
|
66 |
0
| ex.printStackTrace();
|
67 |
| } |
68 |
0
| public void printStackTrace(PrintWriter writer) {
|
69 |
0
| super.printStackTrace(writer);
|
70 |
0
| ex.printStackTrace(writer);
|
71 |
| } |
72 |
0
| public void printStackTrace(PrintStream stream) {
|
73 |
0
| super.printStackTrace(stream);
|
74 |
0
| ex.printStackTrace(stream);
|
75 |
| } |
76 |
0
| public String getMessage() {
|
77 |
0
| return super.getMessage() + ex.getMessage();
|
78 |
| } |
79 |
| }; |
80 |
| } |
81 |
| } |