View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.jsp.rules;
5   
6   import java.text.MessageFormat;
7   import java.util.Iterator;
8   import java.util.List;
9   
10  import net.sourceforge.pmd.CommonAbstractRule;
11  import net.sourceforge.pmd.RuleContext;
12  import net.sourceforge.pmd.RuleViolation;
13  import net.sourceforge.pmd.ast.Node;
14  import net.sourceforge.pmd.jsp.ast.ASTAttribute;
15  import net.sourceforge.pmd.jsp.ast.ASTAttributeValue;
16  import net.sourceforge.pmd.jsp.ast.ASTCData;
17  import net.sourceforge.pmd.jsp.ast.ASTCommentTag;
18  import net.sourceforge.pmd.jsp.ast.ASTCompilationUnit;
19  import net.sourceforge.pmd.jsp.ast.ASTContent;
20  import net.sourceforge.pmd.jsp.ast.ASTDeclaration;
21  import net.sourceforge.pmd.jsp.ast.ASTDoctypeDeclaration;
22  import net.sourceforge.pmd.jsp.ast.ASTDoctypeExternalId;
23  import net.sourceforge.pmd.jsp.ast.ASTElExpression;
24  import net.sourceforge.pmd.jsp.ast.ASTElement;
25  import net.sourceforge.pmd.jsp.ast.ASTHtmlScript;
26  import net.sourceforge.pmd.jsp.ast.ASTJspComment;
27  import net.sourceforge.pmd.jsp.ast.ASTJspDeclaration;
28  import net.sourceforge.pmd.jsp.ast.ASTJspDirective;
29  import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
30  import net.sourceforge.pmd.jsp.ast.ASTJspExpression;
31  import net.sourceforge.pmd.jsp.ast.ASTJspExpressionInAttribute;
32  import net.sourceforge.pmd.jsp.ast.ASTJspScriptlet;
33  import net.sourceforge.pmd.jsp.ast.ASTText;
34  import net.sourceforge.pmd.jsp.ast.ASTUnparsedText;
35  import net.sourceforge.pmd.jsp.ast.ASTValueBinding;
36  import net.sourceforge.pmd.jsp.ast.JspParserVisitor;
37  import net.sourceforge.pmd.jsp.ast.SimpleNode;
38  
39  public abstract class AbstractJspRule extends CommonAbstractRule implements
40  		JspParserVisitor {
41  
42  	@Override
43  	public void setUsesTypeResolution() {
44  		// No Type resolution for JSP rules?
45  	}
46  
47  	/**
48  	 * Adds a violation to the report.
49  	 *
50  	 * @param data
51  	 *            the RuleContext
52  	 * @param node
53  	 *            the node that produces the violation
54  	 */
55  	protected final void addViolation(Object data, SimpleNode node) {
56  		RuleContext ctx = (RuleContext)data;
57  		ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node));
58  	}
59  
60  	/**
61  	 * Adds a violation to the report.
62  	 *
63  	 * @param data
64  	 *            the RuleContext
65  	 * @param node
66  	 *            the node that produces the violation
67  	 * @param msg
68  	 *            specific message to put in the report
69  	 */
70  	protected final void addViolationWithMessage(Object data, SimpleNode node,
71  			String msg) {
72  		RuleContext ctx = (RuleContext)data;
73  		ctx.getReport().addRuleViolation(
74  				new RuleViolation(this, ctx, node, msg));
75  	}
76  
77  	/**
78  	 * Adds a violation to the report.
79  	 *
80  	 * @param data
81  	 *            the RuleContext
82  	 * @param node
83  	 *            the node that produces the violation
84  	 * @param embed
85  	 *            a variable to embed in the rule violation message
86  	 */
87  	protected final void addViolation(Object data, SimpleNode node, String embed) {
88  		RuleContext ctx = (RuleContext)data;
89  		ctx.getReport().addRuleViolation(
90  				new RuleViolation(this, ctx, node, MessageFormat.format(
91  						getMessage(), embed)));
92  	}
93  
94  	/**
95  	 * Adds a violation to the report.
96  	 *
97  	 * @param data
98  	 *            the RuleContext
99  	 * @param node
100 	 *            the node that produces the violation, may be null, in which
101 	 *            case all line and column info will be set to zero
102 	 * @param args
103 	 *            objects to embed in the rule violation message
104 	 */
105 	protected final void addViolation(Object data, Node node, Object[] args) {
106 		RuleContext ctx = (RuleContext)data;
107 		ctx.getReport().addRuleViolation(
108 				new RuleViolation(this, ctx, (SimpleNode)node, MessageFormat
109 						.format(getMessage(), args)));
110 	}
111 
112 	public void apply(List acus, RuleContext ctx) {
113 		visitAll(acus, ctx);
114 	}
115 
116 	protected void visitAll(List acus, RuleContext ctx) {
117 		for (Iterator i = acus.iterator(); i.hasNext();) {
118 			SimpleNode node = (SimpleNode)i.next();
119 			visit(node, ctx);
120 		}
121 	}
122 
123 	//
124 	// The following APIs are identical to those in JspParserVisitorAdapter.
125 	// Due to Java single inheritance, it preferred to extend from the more
126 	// complex Rule base class instead of from relatively simple Visitor.
127 	//
128 
129 	public Object visit(SimpleNode node, Object data) {
130 		node.childrenAccept(this, data);
131 		return null;
132 	}
133 
134 	public Object visit(ASTCompilationUnit node, Object data) {
135 		return visit((SimpleNode)node, data);
136 	}
137 
138 	public Object visit(ASTContent node, Object data) {
139 		return visit((SimpleNode)node, data);
140 	}
141 
142 	public Object visit(ASTJspDirective node, Object data) {
143 		return visit((SimpleNode)node, data);
144 	}
145 
146 	public Object visit(ASTJspDirectiveAttribute node, Object data) {
147 		return visit((SimpleNode)node, data);
148 	}
149 
150 	public Object visit(ASTJspScriptlet node, Object data) {
151 		return visit((SimpleNode)node, data);
152 	}
153 
154 	public Object visit(ASTJspExpression node, Object data) {
155 		return visit((SimpleNode)node, data);
156 	}
157 
158 	public Object visit(ASTJspDeclaration node, Object data) {
159 		return visit((SimpleNode)node, data);
160 	}
161 
162 	public Object visit(ASTJspComment node, Object data) {
163 		return visit((SimpleNode)node, data);
164 	}
165 
166 	public Object visit(ASTText node, Object data) {
167 		return visit((SimpleNode)node, data);
168 	}
169 
170 	public Object visit(ASTUnparsedText node, Object data) {
171 		return visit((SimpleNode)node, data);
172 	}
173 
174 	public Object visit(ASTElExpression node, Object data) {
175 		return visit((SimpleNode)node, data);
176 	}
177 
178 	public Object visit(ASTValueBinding node, Object data) {
179 		return visit((SimpleNode)node, data);
180 	}
181 
182 	public Object visit(ASTCData node, Object data) {
183 		return visit((SimpleNode)node, data);
184 	}
185 
186 	public Object visit(ASTElement node, Object data) {
187 		return visit((SimpleNode)node, data);
188 	}
189 
190 	public Object visit(ASTAttribute node, Object data) {
191 		return visit((SimpleNode)node, data);
192 	}
193 
194 	public Object visit(ASTAttributeValue node, Object data) {
195 		return visit((SimpleNode)node, data);
196 	}
197 
198 	public Object visit(ASTJspExpressionInAttribute node, Object data) {
199 		return visit((SimpleNode)node, data);
200 	}
201 
202 	public Object visit(ASTCommentTag node, Object data) {
203 		return visit((SimpleNode)node, data);
204 	}
205 
206 	public Object visit(ASTDeclaration node, Object data) {
207 		return visit((SimpleNode)node, data);
208 	}
209 
210 	public Object visit(ASTDoctypeDeclaration node, Object data) {
211 		return visit((SimpleNode)node, data);
212 	}
213 
214 	public Object visit(ASTDoctypeExternalId node, Object data) {
215 		return visit((SimpleNode)node, data);
216 	}
217 
218 	public Object visit(ASTHtmlScript node, Object data) {
219 		return visit((SimpleNode)node, data);
220 	}
221 }