//
// Rule.cs
//
// Author:
// Jacob Johnston (jacobj@inchoatethoughts.com)
//
// Copyright (C) 2009 Jacob Johnston
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System.ComponentModel;
using System.IO;
using System.Xml;
namespace ZenLibrary.RuleBase
{
#region class Rule
///
/// An abstract base class for custom rules. Custom rules should
/// inherit from this class and implement the abstract methods.
///
public abstract class Rule : INotifyPropertyChanged
{
private bool isEnabled = true;
///
/// Fires after the IsEnabled property changes.
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// The Name of the rule that will be displayed to the user.
///
public abstract string Name { get; }
///
/// The type of rule (Per Directory or Per File)
///
public abstract TestType TestType { get; }
///
/// The method that stores the test logic for the rule.
///
/// The directory info for the test (null if the TestType is FileScan)
/// The file info for the test.
/// Returns the result of the test (pass/fail). If the test fails, will contain a display string for the failure.
public abstract RuleTestResult RunTest(DirectoryInfo directoryInfo, FileInfo fileInfo);
///
/// Gets or Sets whether the rule is selected to be run.
///
public bool IsEnabled
{
get
{
return isEnabled;
}
set
{
isEnabled = value;
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsEnabled"));
// Changing the IsEnabled property triggers the config information to be resaved so this information
// is persisted between sessions.
Config.Instance.SaveConfig();
}
}
///
/// Writes the XML config element for the rule.
///
/// The XMLWriter used to output config information.
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement(this.GetType().ToString());
writer.WriteAttributeString("IsEnabled", isEnabled.ToString());
WriteCustomConfigAttributes(writer);
writer.WriteEndElement();
}
///
/// When overridden, contains logic to write custom rule attributes to the rule config element.
///
/// The XMLWriter used to output config information.
protected virtual void WriteCustomConfigAttributes(XmlWriter writer)
{
// Do nothing. Will be overridden in child classes.
}
///
/// Reads the XML config element for the rule.
///
/// The XMLReader used to input config information.
public void ReadXml(XmlReader reader)
{
if(reader.HasAttributes)
IsEnabled = bool.Parse(reader["IsEnabled"]);
ReadCustomConfigAttributes(reader);
reader.MoveToElement();
}
///
/// When overridden, contains logic to read custom rule attributes from the rule config element.
///
/// The XMLReader used to input config information.
protected virtual void ReadCustomConfigAttributes(XmlReader reader)
{
// Do nothing. Will be overridden in child classes.
}
}
#endregion
#region class ConfigurableRule
///
/// A special type of rule that allows for user configuration.
///
public abstract class ConfigurableRule : Rule
{
///
/// The method that will handle the configure button being pressed.
///
public abstract void Configure();
}
#endregion
#region enum TestType
///
/// The type of rule test to perform.
///
public enum TestType
{
///
/// Tests are performed on a per directory basis.
///
DirectoryScan,
///
/// Tests are performed on a per file basis.
///
FileScan
}
#endregion
}