1 /** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd; 5 6 import java.io.File; 7 import java.util.Collections; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 public class RuleContext { 12 13 private Report report = new Report(); 14 private File sourceCodeFile; 15 private String sourceCodeFilename; 16 private SourceType sourceType; 17 private final Map<String, Object> attributes; 18 19 /** 20 * Default constructor. 21 */ 22 public RuleContext() { 23 attributes = Collections.synchronizedMap(new HashMap<String, Object>()); 24 } 25 26 /** 27 * Constructor which shares attributes with the given RuleContext. 28 */ 29 public RuleContext(RuleContext ruleContext) { 30 this.attributes = ruleContext.attributes; 31 } 32 33 public Report getReport() { 34 return report; 35 } 36 37 public void setReport(Report report) { 38 this.report = report; 39 } 40 41 public File getSourceCodeFile() { 42 return sourceCodeFile; 43 } 44 45 public void setSourceCodeFile(File sourceCodeFile) { 46 this.sourceCodeFile = sourceCodeFile; 47 } 48 49 public String getSourceCodeFilename() { 50 return sourceCodeFilename; 51 } 52 53 public void setSourceCodeFilename(String filename) { 54 this.sourceCodeFilename = filename; 55 } 56 57 public void excludeLines(Map<Integer, String> lines) { 58 report.exclude(lines); 59 } 60 61 public SourceType getSourceType() { 62 return this.sourceType; 63 } 64 65 public void setSourceType(SourceType t) { 66 this.sourceType = t; 67 } 68 69 /** 70 * Set an attribute value on the RuleContext, if it does not already exist. 71 * <p> 72 * Attributes can be shared between RuleContext instances. This operation 73 * is thread-safe. 74 * <p> 75 * Attribute values should be modified directly via the reference provided. 76 * It is not necessary to call <code>setAttribute(String, Object)</code> to 77 * update an attribute value. Modifications made to the attribute value 78 * will automatically be seen by other threads. Because of this, you must 79 * ensure the attribute values are themselves thread safe. 80 * 81 * @param name The attribute name. 82 * @param value The attribute value. 83 * @exception IllegalArgumentException if <code>name</code> or <code> value</code> are <code>null</code> 84 * @return <code>true</code> if the attribute was set, <code>false</code> otherwise. 85 */ 86 public boolean setAttribute(String name, Object value) { 87 if (name == null) { 88 throw new IllegalArgumentException("Parameter 'name' cannot be null."); 89 } 90 if (value == null) { 91 throw new IllegalArgumentException("Parameter 'value' cannot be null."); 92 } 93 synchronized (this.attributes) { 94 if (!this.attributes.containsKey(name)) { 95 this.attributes.put(name, value); 96 return true; 97 } else { 98 return false; 99 } 100 } 101 } 102 103 /** 104 * Get an attribute value on the RuleContext. 105 * <p> 106 * Attributes can be shared between RuleContext instances. This operation 107 * is thread-safe. 108 * <p> 109 * Attribute values should be modified directly via the reference provided. 110 * It is not necessary to call <code>setAttribute(String, Object)</code> to 111 * update an attribute value. Modifications made to the attribute value 112 * will automatically be seen by other threads. Because of this, you must 113 * ensure the attribute values are themselves thread safe. 114 * 115 * @param name The attribute name. 116 * @return The current attribute value, or <code>null</code> if the attribute does not exist. 117 */ 118 public Object getAttribute(String name) { 119 return this.attributes.get(name); 120 } 121 122 /** 123 * Remove an attribute value on the RuleContext. 124 * <p> 125 * Attributes can be shared between RuleContext instances. This operation 126 * is thread-safe. 127 * <p> 128 * Attribute values should be modified directly via the reference provided. 129 * It is not necessary to call <code>setAttribute(String, Object)</code> to 130 * update an attribute value. Modifications made to the attribute value 131 * will automatically be seen by other threads. Because of this, you must 132 * ensure the attribute values are themselves thread safe. 133 * 134 * @param name The attribute name. 135 * @return The current attribute value, or <code>null</code> if the attribute does not exist. 136 */ 137 public Object removeAttribute(String name) { 138 return this.attributes.remove(name); 139 } 140 }