using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
namespace FileFlows.BasicNodes.Functions;
///
/// Flow element that matches the working file or the original file against a regular expression pattern
///
public class PatternMatch : Node
{
///
public override int Inputs => 1;
///
public override int Outputs => 2;
///
public override FlowElementType Type => FlowElementType.Logic;
///
public override string Icon => "fas fa-equals";
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/pattern-match";
///
public override bool FailureNode => true;
private Dictionary _Variables;
///
public override Dictionary Variables => _Variables;
///
/// Constructs a new instance of the Pattern Match object
///
public PatternMatch()
{
_Variables = new Dictionary()
{
{ "PatternMatch", "match from pattern" }
};
}
///
/// Gets or sets the pattern to match against
///
[DefaultValue("")]
[Text(1)]
[Required]
public string Pattern { get; set; }
///
public override int Execute(NodeParameters args)
{
if (string.IsNullOrEmpty(Pattern))
return 1; // no pattern, matches everything
try
{
var rgx = new Regex(Pattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (rgx.IsMatch(args.WorkingFile))
{
args.Variables["PatternMatch"] = rgx.Match(args.WorkingFile).Value;
return 1;
}
if (rgx.IsMatch(args.FileName))
{
args.Variables["PatternMatch"] = rgx.Match(args.FileName).Value;
return 1;
}
return 2;
}
catch (Exception ex)
{
args.Logger?.ELog("Pattern error: " + ex.Message);
return -1;
}
}
}