Source code: com/puppycrawl/tools/checkstyle/checks/xpath/XPathCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.checks.xpath;
20
21 import java.util.Iterator;
22
23 import org.jaxen.BaseXPath;
24 import org.jaxen.JaxenException;
25 import org.jaxen.XPath;
26
27 import antlr.ASTFactory;
28
29 import com.puppycrawl.tools.checkstyle.api.Check;
30 import com.puppycrawl.tools.checkstyle.api.DetailAST;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32
33 /**
34 * Checks for an XPath in the root AST. Path elements are named
35 * according to token types. Attributes of an element are bean
36 * properties.
37 * Requires jaxen, http://jaxen.sourceforge.net and
38 * saxpath, http://sourceforge.net/projects/saxpath/.
39 * Idea shamelessly stolen from the equivalent PMD rule (pmd.sourceforge.net).
40 * @author Rick Giles
41 */
42 public class XPathCheck extends Check
43 {
44 /** XPath for this check */
45 private XPath mXPath;
46
47 /** error message */
48 private String mMessage = "illegal.xpath";
49
50 /** @see com.puppycrawl.tools.checkstyle.api.Check#getDefaultTokens() */
51 public int[] getDefaultTokens()
52 {
53 return new int[0];
54 }
55
56 /** @see com.puppycrawl.tools.checkstyle.api.Check */
57 public void beginTree(DetailAST aAST)
58 {
59 if (mXPath != null) {
60 final ASTFactory factory = new ASTFactory();
61 factory.setASTNodeType(DetailAST.class.getName());
62 // TODO: Need to resolve if need a fake root node....
63 final DetailAST root =
64 (DetailAST) factory.create(TokenTypes.EOF, "ROOT");
65 root.setFirstChild(aAST);
66 try {
67 final Iterator it = mXPath.selectNodes(aAST).iterator();
68 while (it.hasNext()) {
69 final DetailAST node = (DetailAST) it.next();
70 log(
71 node.getLineNo(),
72 node.getColumnNo(),
73 mMessage,
74 new String[] {node.getText()});
75 }
76 }
77 catch (JaxenException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace();
80 }
81 }
82 }
83
84 /**
85 * Sets the error message for this check.
86 * @param aMessage error message for this check.
87 */
88 public void setMessage(String aMessage)
89 {
90 mMessage = aMessage;
91 }
92
93 /**
94 * Sets the XPath for this check.
95 * @param aXPath the XPath for this check.
96 * @throws JaxenException if there is an error.
97 */
98 public void setXPath(String aXPath)
99 throws JaxenException
100 {
101 mXPath = new BaseXPath(aXPath, new DocumentNavigator());
102 }
103 }