using System; using System.Xml.Linq; namespace DataFileAnalyzer { /// /// An error or a warning associated to a DataFile /// public class DataFileError { /// /// The error/warning codes /// public enum Codes { Generic = 0, BasicUpdate, NeedDependencies, ErrorComputingDependencies, WarningComputingDependencies, UnresolvedDependencies, MissingFile, NotInPerforce, Orphan, } /// /// The error code /// public Codes Code { get; set; } /// /// The error message /// public string Message { get; set; } /// /// Constructor /// public DataFileError(string _message) { Code = Codes.Generic; Message = _message; } /// /// Constructor /// public DataFileError(Codes _code) { Code = _code; Message = null; } /// /// Constructor /// public DataFileError(Codes _code, string _message) { Code = _code; Message = _message; } /// /// String conversion /// public override string ToString() { if (string.IsNullOrEmpty(Message)) return GetGenericMessage(Code); return Message; } /// /// Save the DataFileError to _element /// /// true if no error public bool Save(XElement _element) { _element.SetAttributeValue("Code", (int)Code); _element.SetAttributeValue("Message", Message); return true; } /// /// Load the DataFileError from _element /// /// true if no error public bool Load(XElement _element) { int codeInt; XAttribute attribute = _element.Attribute("Code"); if (attribute != null && int.TryParse(attribute.Value, out codeInt)) Code = (Codes)codeInt; else return false; attribute = _element.Attribute("Message"); if (attribute != null) Message = attribute.Value; return true; } /// /// Convert code to a string /// public static string GetGenericMessage(Codes _code) { switch (_code) { case Codes.Generic: return "Error."; case Codes.BasicUpdate: return "An error occurred while checking the file."; case Codes.NeedDependencies: return "Dependencies need to be recomputed."; case Codes.ErrorComputingDependencies: return "An error occurred when computing dependencies."; case Codes.WarningComputingDependencies: return "A warning was raised when computing dependencies."; case Codes.UnresolvedDependencies: return "File has unresolved dependencies."; case Codes.MissingFile: return "File is missing."; case Codes.NotInPerforce: return "File is not on Perforce"; case Codes.Orphan: return "File is orphan"; } return "Error."; } } }