001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd;
004:
005: import java.io.File;
006: import java.util.Collections;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: public class RuleContext {
011:
012: private Report report = new Report();
013: private File sourceCodeFile;
014: private String sourceCodeFilename;
015: private SourceType sourceType;
016: private final Map<String, Object> attributes;
017:
018: /**
019: * Default constructor.
020: */
021: public RuleContext() {
022: attributes = Collections
023: .synchronizedMap(new HashMap<String, Object>());
024: }
025:
026: /**
027: * Constructor which shares attributes with the given RuleContext.
028: */
029: public RuleContext(RuleContext ruleContext) {
030: this .attributes = ruleContext.attributes;
031: }
032:
033: public Report getReport() {
034: return report;
035: }
036:
037: public void setReport(Report report) {
038: this .report = report;
039: }
040:
041: public File getSourceCodeFile() {
042: return sourceCodeFile;
043: }
044:
045: public void setSourceCodeFile(File sourceCodeFile) {
046: this .sourceCodeFile = sourceCodeFile;
047: }
048:
049: public String getSourceCodeFilename() {
050: return sourceCodeFilename;
051: }
052:
053: public void setSourceCodeFilename(String filename) {
054: this .sourceCodeFilename = filename;
055: }
056:
057: public void excludeLines(Map<Integer, String> lines) {
058: report.exclude(lines);
059: }
060:
061: public SourceType getSourceType() {
062: return this .sourceType;
063: }
064:
065: public void setSourceType(SourceType t) {
066: this .sourceType = t;
067: }
068:
069: /**
070: * Set an attribute value on the RuleContext, if it does not already exist.
071: * <p>
072: * Attributes can be shared between RuleContext instances. This operation
073: * is thread-safe.
074: * <p>
075: * Attribute values should be modified directly via the reference provided.
076: * It is not necessary to call <code>setAttribute(String, Object)</code> to
077: * update an attribute value. Modifications made to the attribute value
078: * will automatically be seen by other threads. Because of this, you must
079: * ensure the attribute values are themselves thread safe.
080: *
081: * @param name The attribute name.
082: * @param value The attribute value.
083: * @exception IllegalArgumentException if <code>name</code> or <code> value</code> are <code>null</code>
084: * @return <code>true</code> if the attribute was set, <code>false</code> otherwise.
085: */
086: public boolean setAttribute(String name, Object value) {
087: if (name == null) {
088: throw new IllegalArgumentException(
089: "Parameter 'name' cannot be null.");
090: }
091: if (value == null) {
092: throw new IllegalArgumentException(
093: "Parameter 'value' cannot be null.");
094: }
095: synchronized (this .attributes) {
096: if (!this .attributes.containsKey(name)) {
097: this .attributes.put(name, value);
098: return true;
099: } else {
100: return false;
101: }
102: }
103: }
104:
105: /**
106: * Get an attribute value on the RuleContext.
107: * <p>
108: * Attributes can be shared between RuleContext instances. This operation
109: * is thread-safe.
110: * <p>
111: * Attribute values should be modified directly via the reference provided.
112: * It is not necessary to call <code>setAttribute(String, Object)</code> to
113: * update an attribute value. Modifications made to the attribute value
114: * will automatically be seen by other threads. Because of this, you must
115: * ensure the attribute values are themselves thread safe.
116: *
117: * @param name The attribute name.
118: * @return The current attribute value, or <code>null</code> if the attribute does not exist.
119: */
120: public Object getAttribute(String name) {
121: return this .attributes.get(name);
122: }
123:
124: /**
125: * Remove an attribute value on the RuleContext.
126: * <p>
127: * Attributes can be shared between RuleContext instances. This operation
128: * is thread-safe.
129: * <p>
130: * Attribute values should be modified directly via the reference provided.
131: * It is not necessary to call <code>setAttribute(String, Object)</code> to
132: * update an attribute value. Modifications made to the attribute value
133: * will automatically be seen by other threads. Because of this, you must
134: * ensure the attribute values are themselves thread safe.
135: *
136: * @param name The attribute name.
137: * @return The current attribute value, or <code>null</code> if the attribute does not exist.
138: */
139: public Object removeAttribute(String name) {
140: return this.attributes.remove(name);
141: }
142: }
|