001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.jsp.rules;
004:
005: import java.text.MessageFormat;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import net.sourceforge.pmd.CommonAbstractRule;
010: import net.sourceforge.pmd.RuleContext;
011: import net.sourceforge.pmd.RuleViolation;
012: import net.sourceforge.pmd.ast.Node;
013: import net.sourceforge.pmd.jsp.ast.ASTAttribute;
014: import net.sourceforge.pmd.jsp.ast.ASTAttributeValue;
015: import net.sourceforge.pmd.jsp.ast.ASTCData;
016: import net.sourceforge.pmd.jsp.ast.ASTCommentTag;
017: import net.sourceforge.pmd.jsp.ast.ASTCompilationUnit;
018: import net.sourceforge.pmd.jsp.ast.ASTContent;
019: import net.sourceforge.pmd.jsp.ast.ASTDeclaration;
020: import net.sourceforge.pmd.jsp.ast.ASTDoctypeDeclaration;
021: import net.sourceforge.pmd.jsp.ast.ASTDoctypeExternalId;
022: import net.sourceforge.pmd.jsp.ast.ASTElExpression;
023: import net.sourceforge.pmd.jsp.ast.ASTElement;
024: import net.sourceforge.pmd.jsp.ast.ASTJspComment;
025: import net.sourceforge.pmd.jsp.ast.ASTJspDeclaration;
026: import net.sourceforge.pmd.jsp.ast.ASTJspDirective;
027: import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
028: import net.sourceforge.pmd.jsp.ast.ASTJspExpression;
029: import net.sourceforge.pmd.jsp.ast.ASTJspExpressionInAttribute;
030: import net.sourceforge.pmd.jsp.ast.ASTJspScriptlet;
031: import net.sourceforge.pmd.jsp.ast.ASTText;
032: import net.sourceforge.pmd.jsp.ast.ASTUnparsedText;
033: import net.sourceforge.pmd.jsp.ast.ASTValueBinding;
034: import net.sourceforge.pmd.jsp.ast.JspParserVisitor;
035: import net.sourceforge.pmd.jsp.ast.SimpleNode;
036:
037: public abstract class AbstractJspRule extends CommonAbstractRule
038: implements JspParserVisitor {
039:
040: @Override
041: public void setUsesTypeResolution() {
042: // No Type resolution for JSP rules?
043: }
044:
045: /**
046: * Adds a violation to the report.
047: *
048: * @param data
049: * the RuleContext
050: * @param node
051: * the node that produces the violation
052: */
053: protected final void addViolation(Object data, SimpleNode node) {
054: RuleContext ctx = (RuleContext) data;
055: ctx.getReport().addRuleViolation(
056: new RuleViolation(this , ctx, node));
057: }
058:
059: /**
060: * Adds a violation to the report.
061: *
062: * @param data
063: * the RuleContext
064: * @param node
065: * the node that produces the violation
066: * @param msg
067: * specific message to put in the report
068: */
069: protected final void addViolationWithMessage(Object data,
070: SimpleNode node, String msg) {
071: RuleContext ctx = (RuleContext) data;
072: ctx.getReport().addRuleViolation(
073: new RuleViolation(this , ctx, node, msg));
074: }
075:
076: /**
077: * Adds a violation to the report.
078: *
079: * @param data
080: * the RuleContext
081: * @param node
082: * the node that produces the violation
083: * @param embed
084: * a variable to embed in the rule violation message
085: */
086: protected final void addViolation(Object data, SimpleNode node,
087: String embed) {
088: RuleContext ctx = (RuleContext) data;
089: ctx.getReport().addRuleViolation(
090: new RuleViolation(this , ctx, node, MessageFormat
091: .format(getMessage(), embed)));
092: }
093:
094: /**
095: * Adds a violation to the report.
096: *
097: * @param data
098: * the RuleContext
099: * @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,
106: Object[] args) {
107: RuleContext ctx = (RuleContext) data;
108: ctx.getReport().addRuleViolation(
109: new RuleViolation(this , ctx, (SimpleNode) node,
110: MessageFormat.format(getMessage(), args)));
111: }
112:
113: public void apply(List acus, RuleContext ctx) {
114: visitAll(acus, ctx);
115: }
116:
117: protected void visitAll(List acus, RuleContext ctx) {
118: for (Iterator i = acus.iterator(); i.hasNext();) {
119: SimpleNode node = (SimpleNode) i.next();
120: visit(node, ctx);
121: }
122: }
123:
124: //
125: // The following APIs are identical to those in JspParserVisitorAdapter.
126: // Due to Java single inheritance, it preferred to extend from the more
127: // complex Rule base class instead of from relatively simple Visitor.
128: //
129:
130: public Object visit(SimpleNode node, Object data) {
131: node.childrenAccept(this , data);
132: return null;
133: }
134:
135: public Object visit(ASTCompilationUnit node, Object data) {
136: return visit((SimpleNode) node, data);
137: }
138:
139: public Object visit(ASTContent node, Object data) {
140: return visit((SimpleNode) node, data);
141: }
142:
143: public Object visit(ASTJspDirective node, Object data) {
144: return visit((SimpleNode) node, data);
145: }
146:
147: public Object visit(ASTJspDirectiveAttribute node, Object data) {
148: return visit((SimpleNode) node, data);
149: }
150:
151: public Object visit(ASTJspScriptlet node, Object data) {
152: return visit((SimpleNode) node, data);
153: }
154:
155: public Object visit(ASTJspExpression node, Object data) {
156: return visit((SimpleNode) node, data);
157: }
158:
159: public Object visit(ASTJspDeclaration node, Object data) {
160: return visit((SimpleNode) node, data);
161: }
162:
163: public Object visit(ASTJspComment node, Object data) {
164: return visit((SimpleNode) node, data);
165: }
166:
167: public Object visit(ASTText node, Object data) {
168: return visit((SimpleNode) node, data);
169: }
170:
171: public Object visit(ASTUnparsedText node, Object data) {
172: return visit((SimpleNode) node, data);
173: }
174:
175: public Object visit(ASTElExpression node, Object data) {
176: return visit((SimpleNode) node, data);
177: }
178:
179: public Object visit(ASTValueBinding node, Object data) {
180: return visit((SimpleNode) node, data);
181: }
182:
183: public Object visit(ASTCData node, Object data) {
184: return visit((SimpleNode) node, data);
185: }
186:
187: public Object visit(ASTElement node, Object data) {
188: return visit((SimpleNode) node, data);
189: }
190:
191: public Object visit(ASTAttribute node, Object data) {
192: return visit((SimpleNode) node, data);
193: }
194:
195: public Object visit(ASTAttributeValue node, Object data) {
196: return visit((SimpleNode) node, data);
197: }
198:
199: public Object visit(ASTJspExpressionInAttribute node, Object data) {
200: return visit((SimpleNode) node, data);
201: }
202:
203: public Object visit(ASTCommentTag node, Object data) {
204: return visit((SimpleNode) node, data);
205: }
206:
207: public Object visit(ASTDeclaration node, Object data) {
208: return visit((SimpleNode) node, data);
209: }
210:
211: public Object visit(ASTDoctypeDeclaration node, Object data) {
212: return visit((SimpleNode) node, data);
213: }
214:
215: public Object visit(ASTDoctypeExternalId node, Object data) {
216: return visit((SimpleNode) node, data);
217: }
218: }
|