-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utilities.cs
48 lines (46 loc) · 2.11 KB
/
Utilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.MetadirectoryServices;
namespace Granfeldt
{
static class StringHelpers
{
public static string ReplaceWithMVValueOrBlank(this string source, MVEntry mventry)
{
return ReplaceWithMVValueOrBlank(source, mventry, "");
}
public static string ReplaceWithHelperValuesOrBlank(this string source, IList<HelperValue> helpers)
{
if (helpers != null)
{
MatchCollection mc = Regex.Matches(source, @"(?<=#helper\:)(?<helpername>\w+)#", RegexOptions.Compiled);
foreach (Match match in mc)
{
string matchValue = match.Value.Trim('#');
string newValue = helpers.FirstOrDefault(x => x.Name.Equals(matchValue, StringComparison.OrdinalIgnoreCase)).GetValue;
Tracer.TraceInformation("replaced-helper-value-'{0}'-with-'{1}'", matchValue, newValue);
source = Regex.Replace(source, string.Format(@"#helper\:{0}", match.Value), newValue);
}
}
return source;
}
public static string ReplaceWithMVValueOrBlank(this string source, MVEntry mventry, string escapedCN)
{
source = Regex.Replace(source, @"#param:EscapedCN#", escapedCN ?? "", RegexOptions.IgnoreCase);
MatchCollection mc = Regex.Matches(source, @"(?<=#mv\:)(?<attrname>\w+)#", RegexOptions.Compiled);
foreach (Match match in mc)
{
string matchValue = match.Value.Trim('#');
string newValue = mventry[matchValue].IsPresent ? mventry[matchValue].Value : "";
Tracer.TraceInformation("replaced-'{0}'-with-'{1}'", matchValue, newValue);
source = Regex.Replace(source, string.Format(@"#mv\:{0}", match.Value), mventry[matchValue].Value);
}
return source;
}
}
}