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