View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.rule;
5   
6   import java.text.MessageFormat;
7   
8   import net.sourceforge.pmd.Rule;
9   import net.sourceforge.pmd.RuleContext;
10  import net.sourceforge.pmd.RuleViolation;
11  import net.sourceforge.pmd.lang.ast.Node;
12  import net.sourceforge.pmd.util.StringUtil;
13  
14  public abstract class AbstractRuleViolationFactory implements RuleViolationFactory {
15  
16  	private static final Object[] NO_ARGS = new Object[0];
17  
18  	private String cleanup(String message, Object[] args) {
19  
20  		if (message != null) {
21  			// Escape PMD specific variable message format, specifically the {
22  			// in the ${, so MessageFormat doesn't bitch.
23  			final String escapedMessage = StringUtil.replaceString(message,	"${", "$'{'");
24  			return MessageFormat.format(escapedMessage,	args != null ? args : NO_ARGS);
25  		} else {
26  			return message;
27  		}
28  	}
29  	
30  	public void addViolation(RuleContext ruleContext, Rule rule, Node node,	String message, Object[] args) {
31  		
32  		String formattedMessage = cleanup(message, args);
33  		
34  		ruleContext.getReport().addRuleViolation(createRuleViolation(rule, ruleContext, node, formattedMessage));
35  	}
36  
37  	public void addViolation(RuleContext ruleContext, Rule rule, Node node,	String message, int beginLine, int endLine, Object[] args) {
38  		
39  		String formattedMessage = cleanup(message, args);
40  		
41  		ruleContext.getReport().addRuleViolation(
42  				createRuleViolation(rule, ruleContext, node, formattedMessage, beginLine, endLine)
43  				);
44  	}
45  	
46  	protected abstract RuleViolation createRuleViolation(Rule rule,	RuleContext ruleContext, Node node, String message);
47  	
48  	protected abstract RuleViolation createRuleViolation(Rule rule,	RuleContext ruleContext, Node node, String message, int beginLine, int endLine);
49  }