DocSpace-buildtools/common/ASC.Core.Common/Notify/Patterns/PatternFormatter.cs

104 lines
3.8 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
2022-02-15 11:52:43 +00:00
namespace ASC.Notify.Patterns;
public abstract class PatternFormatter : IPatternFormatter
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
private readonly bool _doformat;
private readonly string _tagSearchPattern;
protected Regex RegEx { get; private set; }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected PatternFormatter() { }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected PatternFormatter(string tagSearchRegExp)
: this(tagSearchRegExp, false)
{
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected PatternFormatter(string tagSearchRegExp, bool formatMessage)
{
if (string.IsNullOrEmpty(tagSearchRegExp))
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
throw new ArgumentException(nameof(tagSearchRegExp));
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
_tagSearchPattern = tagSearchRegExp;
RegEx = new Regex(_tagSearchPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
_doformat = formatMessage;
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public string[] GetTags(IPattern pattern)
2022-03-09 17:15:51 +00:00
{
ArgumentNullException.ThrowIfNull(pattern);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
var findedTags = new List<string>(SearchTags(pattern.Body));
Array.ForEach(SearchTags(pattern.Subject), tag => { if (!findedTags.Contains(tag)) findedTags.Add(tag); });
return findedTags.ToArray();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public void FormatMessage(INoticeMessage message, ITagValue[] tagsValues)
2022-03-09 17:15:51 +00:00
{
ArgumentNullException.ThrowIfNull(message);
ArgumentNullException.ThrowIfNull(message.Pattern);
ArgumentNullException.ThrowIfNull(tagsValues);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
BeforeFormat(message, tagsValues);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
message.Subject = FormatText(_doformat ? message.Subject : message.Pattern.Subject, tagsValues);
message.Body = FormatText(_doformat ? message.Body : message.Pattern.Body, tagsValues);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
AfterFormat(message);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected abstract string FormatText(string text, ITagValue[] tagsValues);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected virtual void BeforeFormat(INoticeMessage message, ITagValue[] tagsValues) { }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected virtual void AfterFormat(INoticeMessage message) { }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected virtual string[] SearchTags(string text)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(_tagSearchPattern))
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
return Array.Empty<string>();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
var maches = RegEx.Matches(text);
var findedTags = new List<string>(maches.Count);
foreach (Match mach in maches)
{
var tag = mach.Groups["tagName"].Value;
if (!findedTags.Contains(tag))
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
findedTags.Add(tag);
2019-05-15 14:56:09 +00:00
}
}
2022-02-15 11:52:43 +00:00
return findedTags.ToArray();
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}