using System;
using System.Collections;
using AnticipatingMinds.Genesis.CodeDOM;
namespace AnticipatingMinds.Genesis.KnowledgeManagement{
/// <summary>
/// Represents a rule violation found in the code.
/// </summary>
public class RuleViolation
{
/// <summary>
/// Represents a severity of the violation.
/// </summary>
public enum ViolationSeverity
{
/// <summary>
/// Error
/// </summary>
Error,
/// <summary>
/// Warning
/// </summary>
Warning,
/// <summary>
/// Suggestion
/// </summary>
Suggestion
}
/// <summary>
/// Represents a degree of violation correctability.
/// </summary>
public enum ViolationCorrectability
{
/// <summary>
/// No automatic correction is possible.
/// </summary>
None,
/// <summary>
/// Fully automatic correction is possible.
/// </summary>
Auto,
/// <summary>
/// Correction is possible but requires user input.
/// </summary>
Interactive
}
/// <summary>
/// Initializes new instance of the <see cref="RuleViolation"/> class.
/// </summary>
/// <param name="violatedRule">An instance of the rule that raised violation.</param>
/// <param name="locationAnchor">A code element that represents where rule occured. (Position of violation)</param>
public RuleViolation(Rule violatedRule,CodeElement locationAnchor):this(violatedRule,string.Empty, locationAnchor)
{
}
/// <summary>
/// Initializes new instance of the <see cref="RuleViolation"/> class.
/// </summary>
/// <param name="violatedRule">An instance of the rule that raised violation.</param>
/// <param name="description">Violation description.</param>
/// <param name="locationAnchor">A code element that represents where rule occured. (Position of violation)</param>
public RuleViolation(Rule violatedRule , string description,CodeElement locationAnchor)
{
this.violatedRule = violatedRule;
this.locationAnchor = locationAnchor;
this.description = description;
}
/// <summary>
/// Gets or sets flag whether the rule is fixed.
/// </summary>
public bool IsFixed
{
get
{
return isFixed;
}
set
{
isFixed = value;
}
}
/// <summary>
/// Gets an instance of the rule that has raised the violation.
/// </summary>
public Rule ViolatedRule
{
get
{
return violatedRule;
}
}
/// <summary>
/// Gets or sets violation severity.
/// </summary>
public ViolationSeverity Severity
{
get
{
return severity;
}
set
{
severity= value;
}
}
/// <summary>
/// Gets violation correctability.
/// </summary>
/// <remarks>
/// Correctability returned by this property is the best correctability found
/// in all possible corrections.
/// </remarks>
public ViolationCorrectability Correctability
{
get
{
if(Corrections.Count == 0)
return ViolationCorrectability.None;
//Return ability to correct base on the default correction.
//User will use default correction in case of correcting multiple
//violations and correctability should correspond to one promissed.
foreach(RuleViolationCorrection correction in Corrections)
if(correction.IsDefault)
if(!correction.IsInteractive)
return ViolationCorrectability.Auto;
else
return ViolationCorrectability.Interactive;
return ViolationCorrectability.Interactive;
}
}
/// <summary>
/// Adds possible correction description.
/// </summary>
/// <param name="name">Name of the correction</param>
/// <param name="isDefault">Is it a default correction.</param>
/// <param name="isInteractive">Is correction interactive.</param>
/// <returns>A new instance of <see cref="RuleViolationCorrection"/> class that
/// describes possible violation correction.</returns>
public RuleViolationCorrection AddCorrection(string name,bool isDefault,bool isInteractive)
{
return AddCorrection(name,isDefault,isInteractive,string.Empty);
}
/// <summary>
/// Adds possible correction description.
/// </summary>
/// <param name="name">Name of the correction</param>
/// <param name="isDefault">Is it a default correction.</param>
/// <param name="isInteractive">Is correction interactive.</param>
/// <param name="helpUrl">Correction help url.</param>
/// <returns>A new instance of <see cref="RuleViolationCorrection"/> class that
/// describes possible violation correction.</returns>
public RuleViolationCorrection AddCorrection(string name,bool isDefault,bool isInteractive,string helpUrl)
{
//Verify that there is no more than one default correction
if(isDefault)
{
foreach(RuleViolationCorrection violationCorrection in corrections)
if(violationCorrection.IsDefault)
throw new InvalidOperationException("Corrections collection already contains default correction. Only one default correction is allowed per violation");
}
RuleViolationCorrection correction = new RuleViolationCorrection(this,name,isDefault,isInteractive,helpUrl);
corrections.Add(correction);
return correction;
}
/// <summary>
/// Gets a collection of possible violation corrections.
/// </summary>
public RuleViolationCorrectionCollection Corrections
{
get
{
return corrections;
}
}
/// <summary>
/// Gets violation line number.
/// </summary>
public int Line
{
get
{
return locationAnchor == null ? -1:locationAnchor.SourcePosition.Line;
}
}
/// <summary>
/// Get violation column.
/// </summary>
public int Column
{
get
{
return locationAnchor == null ? -1:locationAnchor.SourcePosition.Column;
}
}
/// <summary>
/// Get violation absolute position in characters from the begining of the file.
/// </summary>
public int Position
{
get
{
return locationAnchor == null ? -1:locationAnchor.SourcePosition.Position;
}
}
/// <summary>
/// Get violation position file name.
/// </summary>
public string FileName
{
get
{
return locationAnchor == null ? string.Empty : locationAnchor.SourcePosition.FileName;
}
}
/// <summary>
/// Gets or sets violation description.
/// </summary>
/// <value>Violation description.</value>
public string Description
{
get
{
return description;
}
set
{
description = value == null ? string.Empty:value;
}
}
/// <summary>
/// Code Element that is serving as violation location anchor
/// </summary>
public CodeElement LocationAnchor
{
get
{
return locationAnchor;
}
set
{
locationAnchor = value;
}
}
/// <summary>
/// Holds data associated with violation.
/// </summary>
public IDictionary ViolationData
{
get
{
return violationData;
}
}
private Hashtable violationData = new Hashtable();
private Rule violatedRule;
private ViolationSeverity severity;
private string description;
private CodeElement locationAnchor;
private RuleViolationCorrectionCollection corrections = new RuleViolationCorrectionCollection();
bool isFixed = false;
}
}
|