/// <summary> /// Replaces a tokenized input string with replacement values. Wildcard support is optional. /// </summary> /// <param name="tokenizedValue"> The input string containing tokenized values. </param> /// <param name="optionalWildcardTokenReplacementStrategy"> An optional wildcard token replacement strategy. </param> /// <returns> A string value with all possible replacements made. </returns> public string ExpandTokens(string tokenizedValue, IWildcardTokenReplacementStrategy optionalWildcardTokenReplacementStrategy) { if (this.DataTypeFascade.IsNullOrWhiteSpace(tokenizedValue)) { return(tokenizedValue); } // clean token collection this.PreviousExpansionTokens.Clear(); tokenizedValue = Regex.Replace(tokenizedValue, TokenizerRegEx, (m) => this.ReplacementMatcherEx(m, optionalWildcardTokenReplacementStrategy), RegexOptions.IgnorePatternWhitespace); return(tokenizedValue); }
/// <summary> /// Private method used to match and process tokenized regular expressions. /// </summary> /// <param name="match"> The regular express match object. </param> /// <param name="wildcardTokenReplacementStrategy"> The wildcard token replacement strategy to use in the event a predefined token replacement strategy lookup failed. </param> /// <returns> The token-resolved string value. </returns> private string ReplacementMatcherEx(Match match, IWildcardTokenReplacementStrategy wildcardTokenReplacementStrategy) { // ${ .token.token.token_end (`arg0`, ..) } string firstToken, rawToken = null; string[] tokens; string[] argumentList = null; object tokenLogicalValue, tokenReplacementValue = null; ITokenReplacementStrategy tokenReplacementStrategy; bool keyNotFound, tryWildcard; rawToken = match.Groups[1].Success ? match.Groups[1].Value : null; argumentList = match.Groups[2].Success ? GetArgs(match.Groups[2].Value) : null; if (SolderLegacyInstanceAccessor.DataTypeFascadeLegacyInstance.IsNullOrWhiteSpace(rawToken)) return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token missing"); // break any token paths into token list tokens = rawToken.Split(new char[] { TOKENIZER_LOGICAL_PROPERTY_PATH_CHAR }, StringSplitOptions.RemoveEmptyEntries); if ((object)tokens == null || tokens.Length <= 0) return null; firstToken = tokens[0]; tokens = tokens.Skip(1).ToArray(); // add to token collection this.PreviousExpansionTokens.Add(firstToken); keyNotFound = !this.TokenReplacementStrategies.TryGetValue(firstToken, out tokenReplacementStrategy); tryWildcard = keyNotFound && (object)wildcardTokenReplacementStrategy != null; if (keyNotFound && !tryWildcard) return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token unknown"); try { if (!tryWildcard) tokenReplacementValue = tokenReplacementStrategy.Evaluate(argumentList); else tokenReplacementValue = wildcardTokenReplacementStrategy.Evaluate(firstToken, argumentList); } catch (Exception ex) { return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, string.Format("function exception {{" + Environment.NewLine + "{0}" + Environment.NewLine + "}}", SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetErrors(ex, 0))); } if ((object)tokens == null || tokens.Length <= 0) return tokenReplacementValue.SafeToString(); tokenLogicalValue = tokenReplacementValue; foreach (string token in tokens) { // only do logical lookup here if (!SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetLogicalPropertyValue(tokenLogicalValue, token, out tokenLogicalValue)) return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, string.Format("logical property expansion failed {{{0}}}", token)); } return tokenLogicalValue.SafeToString(); }
/// <summary> /// Replaces a tokenized input string with replacement values. Wildcard support is optional. /// </summary> /// <param name="tokenizedValue"> The input string containing tokenized values. </param> /// <param name="optionalWildcardTokenReplacementStrategy"> An optional wildcard token replacement strategy. </param> /// <returns> A string value with all possible replacements made. </returns> public string ExpandTokens(string tokenizedValue, IWildcardTokenReplacementStrategy optionalWildcardTokenReplacementStrategy) { if (SolderLegacyInstanceAccessor.DataTypeFascadeLegacyInstance.IsNullOrWhiteSpace(tokenizedValue)) return tokenizedValue; // clean token collection this.PreviousExpansionTokens.Clear(); tokenizedValue = Regex.Replace(tokenizedValue, TokenizerRegEx, (m) => this.ReplacementMatcherEx(m, optionalWildcardTokenReplacementStrategy), RegexOptions.IgnorePatternWhitespace); return tokenizedValue; }
/// <summary> /// Private method used to match and process tokenized regular expressions. /// </summary> /// <param name="match"> The regular express match object. </param> /// <param name="wildcardTokenReplacementStrategy"> The wildcard token replacement strategy to use in the event a predefined token replacement strategy lookup failed. </param> /// <returns> The token-resolved string value. </returns> private string ReplacementMatcherEx(Match match, IWildcardTokenReplacementStrategy wildcardTokenReplacementStrategy) { // ${ .token.token.token_end (`arg0`, ..) } string firstToken, rawToken = null; string[] tokens; string[] argumentList = null; object tokenLogicalValue, tokenReplacementValue = null; ITokenReplacementStrategy tokenReplacementStrategy; bool keyNotFound, tryWildcard; rawToken = match.Groups[1].Success ? match.Groups[1].Value : null; argumentList = match.Groups[2].Success ? this.GetArgs(match.Groups[2].Value) : null; if (this.DataTypeFascade.IsNullOrWhiteSpace(rawToken)) { return(GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token missing")); } // break any token paths into token list tokens = rawToken.Split(new char[] { TOKENIZER_LOGICAL_PROPERTY_PATH_CHAR }, StringSplitOptions.RemoveEmptyEntries); if ((object)tokens == null || tokens.Length <= 0) { return(null); } firstToken = tokens[0]; tokens = tokens.Skip(1).ToArray(); // add to token collection this.PreviousExpansionTokens.Add(firstToken); keyNotFound = !this.TokenReplacementStrategies.TryGetValue(firstToken, out tokenReplacementStrategy); tryWildcard = keyNotFound && (object)wildcardTokenReplacementStrategy != null; if (keyNotFound && !tryWildcard) { return(GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token unknown")); } try { if (!tryWildcard) { tokenReplacementValue = tokenReplacementStrategy.Evaluate(argumentList); } else { tokenReplacementValue = wildcardTokenReplacementStrategy.Evaluate(firstToken, argumentList); } } catch (Exception ex) { return(GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, string.Format("function exception {{" + Environment.NewLine + "{0}" + Environment.NewLine + "}}", this.ReflectionFascade.GetErrors(ex, 0)))); } if ((object)tokens == null || tokens.Length <= 0) { return(tokenReplacementValue.SafeToString()); } tokenLogicalValue = tokenReplacementValue; foreach (string token in tokens) { // only do logical lookup here if (!this.ReflectionFascade.GetLogicalPropertyValue(tokenLogicalValue, token, out tokenLogicalValue)) { return(GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, string.Format("logical property expansion failed {{{0}}}", token))); } } return(tokenLogicalValue.SafeToString()); }
/// <summary> /// Private method used to match and process tokenized regular expressions. /// </summary> /// <param name="match"> The regular express match object. </param> /// <param name="wildcardTokenReplacementStrategy"> The wildcard token replacement strategy to use in the event a predefined token replacement strategy lookup failed. </param> /// <returns> The token-resolved string value. </returns> private string ReplacementMatcherEx(Match match, IWildcardTokenReplacementStrategy wildcardTokenReplacementStrategy) { // ${ token (`arg0`, ..) } string token = null; string[] argumentList = null; object replacementValue = null; ITokenReplacementStrategy tokenReplacementStrategy; bool keyNotFound, tryWildcard; token = match.Groups[1].Success ? match.Groups[1].Value : null; argumentList = match.Groups[2].Success ? GetArgs(match.Groups[2].Value) : null; if (DataType.IsNullOrWhiteSpace(token)) return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token missing"); // add to token collection this.PreviousExpansionTokens.Add(token); keyNotFound = !this.TokenReplacementStrategies.TryGetValue(token, out tokenReplacementStrategy); tryWildcard = keyNotFound && (object)wildcardTokenReplacementStrategy != null; if (keyNotFound && !tryWildcard) return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, "token unknown"); try { if (!tryWildcard) replacementValue = tokenReplacementStrategy.Evaluate(argumentList); else replacementValue = wildcardTokenReplacementStrategy.Evaluate(token, argumentList); } catch (Exception ex) { return GetOriginalValueOrThrowExecption(this.StrictMatching, match.Value, string.Format("function exception {{{0}}}", ex.Message)); } return replacementValue.SafeToString(); }