private void ProcessToken(TextSubstitutionProcessContext context, TextTokenInfo tokenInfo) { switch (tokenInfo.Kind) { case TextTokenKind.Text: OnProcessText(context, tokenInfo); break; case TextTokenKind.DelimitedText: OnProcessDelimitedText(context, tokenInfo); break; } ; }
/// <summary>Called to process delimited text.</summary> /// <param name="context">The processing context.</param> /// <param name="tokenInfo">The token being processed.</param> /// <remarks> /// The default implementation returns the replacement obj from the corresponding substitution rule. /// </remarks> protected virtual void OnProcessDelimitedText(TextSubstitutionProcessContext context, TextTokenInfo tokenInfo) { //Process the template and output whatever results var subContext = new TextSubstitutionContext(context.Options, tokenInfo.OriginalText); //Find the applicable rule var rule = (from r in Rules where r.CanProcess(subContext) select r).FirstOrDefault(); //Get the new text var text = (rule != null) ? rule.Process(subContext) : OnHandleRuleNotFound(context, tokenInfo); //Output if (!String.IsNullOrEmpty(text)) { context.Output.Append(text); } }
/// <summary>Processes the inputValue string and returns the string with the substitution rules applied.</summary> /// <param name="inputValue">The string to process.</param> /// <returns>The processed string.</returns> /// <remarks> /// If <paramref name="inputValue"/> is <see langword="null"/> or empty then an empty string is returned. /// </remarks> /// <example>Refer to <see cref="TextSubstitutionEngine"/> for an example.</example> public string Process(string inputValue) { if (String.IsNullOrWhiteSpace(inputValue)) { return(""); } var context = new TextSubstitutionProcessContext(m_options, inputValue); BeginProcess(context); //Enumerate the tokens var parser = new TextTokenParser(StartDelimiter, EndDelimiter); foreach (var tokenInfo in parser.Parse(inputValue)) { ProcessToken(context, tokenInfo); } ; return(EndProcess(context)); }
/// <summary>Called to process text that is not delimited.</summary> /// <param name="context">The processing context.</param> /// <param name="tokenInfo">The token being processed.</param> /// <remarks> /// The default implementation returns the original text. /// </remarks> protected virtual void OnProcessText(TextSubstitutionProcessContext context, TextTokenInfo tokenInfo) { //Just append to the output context.Output.Append(tokenInfo.OriginalText); }
/// <summary>Called when no matching rule can be found for a token.</summary> /// <param name="context">The processing context.</param> /// <param name="tokenInfo">The token being processed.</param> /// <returns>The text to use.</returns> /// <remarks> /// The default implementation returns the original token text inside the delimiters. /// </remarks> protected virtual string OnHandleRuleNotFound(TextSubstitutionProcessContext context, TextTokenInfo tokenInfo) { return(context.Options.StartDelimiter + tokenInfo.OriginalText + context.Options.EndDelimiter); }
/// <summary>Ends the processing of the output string.</summary> /// <param name="context">The processing context.</param> /// <returns>The string to return.</returns> /// <remarks> /// The default implementation returns the final output. /// </remarks> protected virtual string EndProcess(TextSubstitutionProcessContext context) { return(context.Output.ToString()); }
/// <summary>Begins processing of the inputValue string.</summary> /// <param name="context">The processing context.</param> /// <remarks> /// The default implementation does nothing. Derived classes can adjust the context before processing begins. /// </remarks> protected virtual void BeginProcess(TextSubstitutionProcessContext context) { }