using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using RoboSharp.DefaultConfigurations; namespace RoboSharp { /// /// Setup the ErrorToken and the path to RoboCopy.exe. /// /// /// /// public class RoboSharpConfiguration : ICloneable { #region Constructors /// /// Create new LoggingOptions with Default Settings /// public RoboSharpConfiguration() { } /// /// Clone a RoboSharpConfiguration Object /// /// RoboSharpConfiguration object to clone public RoboSharpConfiguration(RoboSharpConfiguration options) { errorToken = options.errorToken; errorTokenRegex = options.errorTokenRegex; roboCopyExe = options.roboCopyExe; #region < File Tokens > newFileToken = options.newFileToken; olderToken = options.olderToken; newerToken = options.newerToken; sameToken = options.sameToken; extraToken = options.extraToken; mismatchToken = options.mismatchToken; failedToken = options.failedToken; #endregion #region < Directory Tokens > newerDirToken = options.newerDirToken; extraDirToken = options.extraDirToken; existingDirToken = options.existingDirToken; #endregion } /// public RoboSharpConfiguration Clone() => new RoboSharpConfiguration(this); object ICloneable.Clone() => Clone(); #endregion private static readonly IDictionary defaultConfigurations = new Dictionary() { {"en", new RoboSharpConfig_EN() }, //en uses Defaults for LogParsing properties {"de", new RoboSharpConfig_DE() }, }; /// /// Error Token Identifier -- EN = "ERROR", DE = "FEHLER", etc
/// Leave as / Set to null to use system default. ///
public string ErrorToken { get { return errorToken ?? GetDefaultConfiguration().ErrorToken; } set { if (value != errorToken) ErrRegexInitRequired = true; errorToken = value; } } /// field backing property - Protected to allow DefaultConfig derived classes to set within constructor protected string errorToken = null; /// /// Regex to identify Error Tokens with during LogLine parsing /// public Regex ErrorTokenRegex { get { if (ErrRegexInitRequired) goto RegenRegex; //Regex Generation Required else if (errorTokenRegex != null) return errorTokenRegex; //field already assigned -> return the field else { //Try get default, if default has regex defined, use that. errorTokenRegex = GetDefaultConfiguration().errorTokenRegex; if (errorTokenRegex != null) return errorTokenRegex; } // Generate a new Regex Statement RegenRegex: errorTokenRegex = ErrorTokenRegexGenerator(ErrorToken); //new Regex($" {this.ErrorToken} " + @"(\d{1,3}) \(0x\d{8}\) "); ErrRegexInitRequired = false; return errorTokenRegex; } } /// Field backing property - Protected to allow DefaultConfig derived classes to set within constructor protected Regex errorTokenRegex; private bool ErrRegexInitRequired = false; /// /// Generate a new ErrorTokenRegex object from by insterting the into a standardized pattern. /// /// Language Specific /// internal static Regex ErrorTokenRegexGenerator(string errorToken) { Regex BaseErrTokenRegex = new Regex("(?.*?)\\s+IDENTIFIER\\s+(?[0-9]+)\\s+(?\\([0-9Xx]+\\))\\s+(?[\\w\\s]+(?!:))(?.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); string pattern = BaseErrTokenRegex.ToString().Replace("IDENTIFIER", errorToken); return new Regex(pattern, BaseErrTokenRegex.Options); } #region < Tokens for Log Parsing > #region < File Tokens > /// /// Log Lines starting with this string indicate : New File -> Source FILE Exists, Destination does not /// public string LogParsing_NewFile { get { return newFileToken ?? GetDefaultConfiguration().newFileToken ?? "New File"; } set { newFileToken = value; } } private string newFileToken; /// /// Log Lines starting with this string indicate : Destination File newer than Source /// public string LogParsing_OlderFile { get { return olderToken ?? GetDefaultConfiguration().olderToken ?? "Older"; } set { olderToken = value; } } private string olderToken; /// /// Log Lines starting with this string indicate : Source File newer than Destination /// public string LogParsing_NewerFile { get { return newerToken ?? GetDefaultConfiguration().newerToken ?? "Newer"; } set { newerToken = value; } } private string newerToken; /// /// Log Lines starting with this string indicate : Source FILE is identical to Destination File /// public string LogParsing_SameFile { get { return sameToken ?? GetDefaultConfiguration().sameToken ?? "same"; } set { sameToken = value; } } private string sameToken; /// /// Log Lines starting with this string indicate : EXTRA FILE -> Destination Exists, but Source does not /// public string LogParsing_ExtraFile { get { return extraToken ?? GetDefaultConfiguration().extraToken ?? "*EXTRA File"; } set { extraToken = value; } } private string extraToken; /// /// Log Lines starting with this string indicate : MISMATCH FILE /// public string LogParsing_MismatchFile { get { return mismatchToken ?? GetDefaultConfiguration().mismatchToken ?? "*Mismatch"; } // TODO: Needs Verification set { mismatchToken = value; } } private string mismatchToken; /// /// Log Lines starting with this string indicate : File Failed to Copy /// public string LogParsing_FailedFile { get { return failedToken ?? GetDefaultConfiguration().failedToken ?? "*Failed"; } // TODO: Needs Verification set { failedToken = value; } } private string failedToken; /// /// Log Lines starting with this string indicate : File was excluded by filters /// public string LogParsing_FileExclusion { get { return fileExcludedToken ?? GetDefaultConfiguration().fileExcludedToken ?? "named"; } // TODO: Needs Verification set { fileExcludedToken = value; } } private string fileExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by or filters /// public string LogParsing_AttribExclusion { get { return attribExcludedToken ?? GetDefaultConfiguration().attribExcludedToken ?? "attrib"; } set { attribExcludedToken = value; } } private string attribExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by filters /// public string LogParsing_MaxFileSizeExclusion { get { return maxfilesizeExcludedToken ?? GetDefaultConfiguration().maxfilesizeExcludedToken ?? "large"; } set { maxfilesizeExcludedToken = value; } } private string maxfilesizeExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by filters /// public string LogParsing_MinFileSizeExclusion { get { return minfilesizeExcludedToken ?? GetDefaultConfiguration().minfilesizeExcludedToken ?? "small"; } set { minfilesizeExcludedToken = value; } } private string minfilesizeExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by or filters /// public string LogParsing_MaxAgeOrAccessExclusion { get { return maxageoraccessExcludedToken ?? GetDefaultConfiguration().maxageoraccessExcludedToken ?? "too old"; } set { maxageoraccessExcludedToken = value; } } private string maxageoraccessExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by or filters /// public string LogParsing_MinAgeOrAccessExclusion { get { return minageoraccessExcludedToken ?? GetDefaultConfiguration().minageoraccessExcludedToken ?? "too new"; } set { minageoraccessExcludedToken = value; } } private string minageoraccessExcludedToken; /// /// Log Lines starting with this string indicate : File was excluded by filters /// public string LogParsing_ChangedExclusion { get { return changedExcludedToken ?? GetDefaultConfiguration().changedExcludedToken ?? "changed"; } set { changedExcludedToken = value; } } private string changedExcludedToken; /// /// Log Lines starting with this string indicate : File was included by filters /// public string LogParsing_TweakedInclusion { get { return tweakedIncludedToken ?? GetDefaultConfiguration().tweakedIncludedToken ?? "tweaked"; } set { tweakedIncludedToken = value; } } private string tweakedIncludedToken; #endregion #region < Directory Tokens > /// /// Log Lines starting with this string indicate : New Dir -> Directory will be copied to Destination /// public string LogParsing_NewDir { get { return newerDirToken ?? GetDefaultConfiguration().newerDirToken ?? "New Dir"; } set { newerDirToken = value; } } private string newerDirToken; /// /// Log Lines starting with this string indicate : Extra Dir -> Does not exist in source /// public string LogParsing_ExtraDir { get { return extraDirToken ?? GetDefaultConfiguration().extraDirToken ?? "*EXTRA Dir"; } set { extraDirToken = value; } } private string extraDirToken; /// /// Existing Dirs do not have an identifier on the line. Instead, this string will be used when creating the object to indicate an Existing Directory. /// public string LogParsing_ExistingDir { get { return existingDirToken ?? GetDefaultConfiguration().existingDirToken ?? "Existing Dir"; } set { existingDirToken = value; } } private string existingDirToken; /// /// Log Lines starting with this string indicate : Folder was excluded by filters /// public string LogParsing_DirectoryExclusion { get { return dirExcludedToken ?? GetDefaultConfiguration().dirExcludedToken ?? "named"; } // TODO: Needs Verification set { dirExcludedToken = value; } } private string dirExcludedToken; #endregion #endregion /// /// Specify the path to RoboCopy.exe here. If not set, use the default copy. /// public string RoboCopyExe { get { return roboCopyExe ?? "Robocopy.exe"; } set { roboCopyExe = value; } } private string roboCopyExe = null; /// Default is retrieved from the OEMCodePage /// public System.Text.Encoding StandardOutputEncoding { get; set; } = System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage); /// Default is retrieved from the OEMCodePage /// public System.Text.Encoding StandardErrorEncoding { get; set; } = System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage); private RoboSharpConfiguration defaultConfig = null; private RoboSharpConfiguration GetDefaultConfiguration() { if (defaultConfig != null) return defaultConfig; // check for default with language Tag xx-YY (e.g. en-US) var currentLanguageTag = System.Globalization.CultureInfo.CurrentUICulture.IetfLanguageTag; if (defaultConfigurations.ContainsKey(currentLanguageTag)) { defaultConfig = defaultConfigurations[currentLanguageTag]; } else { // check for default with language Tag xx (e.g. en) var match = Regex.Match(currentLanguageTag, @"^\w+", RegexOptions.Compiled); if (match.Success) { var currentMainLanguageTag = match.Value; if (defaultConfigurations.ContainsKey(currentMainLanguageTag)) { defaultConfig = defaultConfigurations[currentMainLanguageTag]; } } } // no match, fallback to en return defaultConfig ?? defaultConfigurations["en"]; } } }