View Javadoc

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