| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Text; |
| | 6 | | using System.Text.RegularExpressions; |
| | 7 | |
|
| | 8 | | namespace Xrm.Oss.XTL.Interpreter |
| | 9 | | { |
| | 10 | | public static class TokenMatcher |
| | 11 | | { |
| 2 | 12 | | private static Regex tokenRegex = new Regex(@"\${{([\s\S]*?(?=}}))}}", RegexOptions.Compiled | RegexOptions.Cult |
| | 13 | |
|
| | 14 | | public static List<string> MatchTokens(string templateText) |
| 30 | 15 | | { |
| 30 | 16 | | var tokens = tokenRegex.Matches(templateText) |
| 30 | 17 | | .Cast<Match>() |
| 58 | 18 | | .Select(m => m.Groups[1].Value) |
| 30 | 19 | | .Distinct() |
| 30 | 20 | | .ToList(); |
| | 21 | |
|
| 30 | 22 | | return tokens; |
| 30 | 23 | | } |
| | 24 | |
|
| | 25 | | public static string ProcessTokens(string templateText, Entity dataSource, OrganizationConfig config, IOrganizat |
| 30 | 26 | | { |
| 30 | 27 | | var tokens = MatchTokens(templateText); |
| | 28 | |
|
| 30 | 29 | | tokens.ForEach(token => |
| 57 | 30 | | { |
| 57 | 31 | | tracing.Trace($"Processing token '{token}'"); |
| 30 | 32 | |
|
| 57 | 33 | | var processor = new XTLInterpreter(token, dataSource, config, service, tracing); |
| 57 | 34 | | var processed = string.Empty; |
| 30 | 35 | |
|
| 30 | 36 | | try |
| 57 | 37 | | { |
| 57 | 38 | | processed = processor.Produce(); |
| 55 | 39 | | } |
| 32 | 40 | | catch (Exception ex) |
| 32 | 41 | | { |
| 32 | 42 | | tracing.Trace($"Exception while processing token {token}, replacing by empty string. Message: {ex.Me |
| 32 | 43 | | } |
| 30 | 44 | |
|
| 57 | 45 | | templateText = templateText.Replace($"${{{{{token}}}}}", processed); |
| 57 | 46 | | tracing.Trace($"Replacing token with '{processed}'"); |
| 57 | 47 | | }); |
| | 48 | |
|
| 30 | 49 | | return templateText; |
| 30 | 50 | | } |
| | 51 | | } |
| | 52 | | } |