#region " © Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar"
// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.
#endregion
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
namespace FileHelpers
{
/// This is the class that handles the errors of the engines process.
/// All the engines and DataStorages contains a ErrorManager.
#if NET_2_0
[DebuggerDisplay("{ErrorsDescription()}. ErrorMode: {ErrorMode.ToString()}")]
#endif
public sealed class ErrorManager
{
/// Initializes a new instance of the class.
public ErrorManager()
{
}
/// Initializes a new instance of the class. with the specified .
/// Indicates the error behavior of the class.
public ErrorManager(ErrorMode mode)
{
mErrorMode = mode;
}
#if NET_2_0
private string ErrorsDescription()
{
if (ErrorCount == 1)
return ErrorCount.ToString() + " Error";
else if (ErrorCount == 0)
return "No Errors";
else
return ErrorCount.ToString() + " Errors";
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
ArrayList mErrorsArray = new ArrayList();
/// Is an array of that contains the errors of the last operation in this class.
#if NET_2_0
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
#endif
public ErrorInfo[] Errors
{
get { return (ErrorInfo[]) mErrorsArray.ToArray(typeof (ErrorInfo)); }
}
#if NET_2_0
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
private ErrorMode mErrorMode = ErrorMode.ThrowException;
/// Indicates the behavior of the when it found an error.
public ErrorMode ErrorMode
{
get { return mErrorMode; }
set { mErrorMode = value; }
}
/// Number of contained errors.
public int ErrorCount
{
get { return mErrorsArray.Count; }
}
/// Indicates if contains one or more errors.
public bool HasErrors
{
get { return mErrorsArray.Count > 0; }
}
/// Clears the error collection.
public void ClearErrors()
{
mErrorsArray.Clear();
}
/// Add the specified ErrorInfo to the contained collection.
///
internal void AddError(ErrorInfo error)
{
mErrorsArray.Add(error);
}
/// Add the specified ErrorInfo to the contained collection.
internal void AddErrors(ErrorManager errors)
{
mErrorsArray.AddRange(errors.mErrorsArray);
}
// public void ProcessError(Exception ex, string line)
// {
// }
/// Saves the contained errors to the specified file.
/// The file that contains the errors.
public void SaveErrors(string fileName)
{
string header;
if (ErrorCount > 0)
header = "FileHelpers - Errors Saved ";
else
header = "FileHelpers - NO Errors Found ";
header += "at " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
header += StringHelper.NewLine + "LineNumber | LineString |ErrorDescription";
SaveErrors(fileName, header);
}
/// Saves the contained errors to the specified file.
/// The file that contains the errors.
/// The header line of the errors file.
public void SaveErrors(string fileName, string header)
{
FileHelperEngine engine = new FileHelperEngine(typeof (ErrorInfo));
if (header.IndexOf(StringHelper.NewLine) == header.LastIndexOf(StringHelper.NewLine))
header += StringHelper.NewLine;
engine.HeaderText = header;
engine.WriteFile(fileName, Errors);
}
/// Load errors from a file.
/// The file that contains the errors.
public static ErrorInfo[] LoadErrors(string fileName)
{
FileHelperEngine engine = new FileHelperEngine(typeof (ErrorInfo));
return (ErrorInfo[]) engine.ReadFile(fileName);
}
}
}