/// <summary> /// Substitue paramerters in source string with values provided by the <see cref="IParameterProvider"/>. /// The followings are predefined parameters: /// {=DateTime}: Current datetime value. Default format is "MMMM d, yyyy \"at\" HH:mm ET". /// <see cref="IParameterProvider"/> can provide a custom format using the key "DateTimeFormat"; /// {=Newline}: New line character. /// {=Tab}: Tab. /// {=WhiteSpace}: White space. /// {=DoubleQuote}: Double quote. /// {SingleQuote}: Single quote. /// </summary> /// <param name="sSource">Source string with parameters defined in the format of {=paramertername}</param> /// <param name="oParameterProvider">Paramerters value provider</param> /// <returns>String with matched parameters replaced</returns> public static string ParseParameterizedString(string source, IParameterProvider parameterProvider) { string dateTimeFormat; string pre, post = String.Empty; int begin = 0, end = 0; StringBuilder strBuilder = new StringBuilder(); MatchCollection matches = Regex.Matches(source, TokenMapper.CodeBlock); if (matches.Count <= 0) { return source; } foreach (Match match in matches) { end = match.Index; if (begin < source.Length) { pre = source.Substring(begin, end - begin); begin = end + match.Length; post = source.Substring(begin); strBuilder.Append(pre); } try { switch (match.Result("$1").Trim()) { case TokenMapper.DateTime: dateTimeFormat = parameterProvider.GetProperty(DateTimeFormat); if (dateTimeFormat != null && dateTimeFormat.Trim() != String.Empty) try { strBuilder.Append(System.DateTime.Now.ToString(dateTimeFormat)); } catch { throw new System.ArgumentException("DateTimeFormat is not valid."); } else strBuilder.Append(System.DateTime.Now.ToString("MMMM d, yyyy \"at\" HH:mm ET")); break; case TokenMapper.Newline: strBuilder.AppendLine(); break; case TokenMapper.Tab: strBuilder.Append("\t"); break; case TokenMapper.WhiteSpace: strBuilder.Append(" "); break; case TokenMapper.DoubleQuote: strBuilder.Append("\""); break; case TokenMapper.SingleQuote: strBuilder.Append("'"); break; default: strBuilder.Append(parameterProvider.GetProperty(match.Result("$1").Trim())); break; } } catch (Exception exception) { throw new Exception(string.Format("Failed to retrieve value for key {0}.", match.Result("$1").Trim()), exception); } } strBuilder.Append(post); return strBuilder.ToString(); }