Skip to content

Instantly share code, notes, and snippets.

@stimms
Forked from anonymous/gist:4320254
Created December 17, 2012 17:56
Show Gist options
  • Save stimms/4320360 to your computer and use it in GitHub Desktop.
Save stimms/4320360 to your computer and use it in GitHub Desktop.
public class TokenReplacementCheckSheetProcessor : BaseChecksheetDecorator, IChecksheetDecorator
{
//A list of all the token resolvers known to the container
public IEnumerable<ITokenResolver> Resolvers {get;set;}
//A cache of the tokens to replace in a single check sheet
//It is time consuming to search a word document for the
//tokens so we try to do it only once for each sort of checkseet
public ICheckSheetTokenCache CheckSheetTokenCache {get;set;}
public void ProcessChecksheet(int tagId, int checksheetId, Doc doc)
{
//check the cache to make sure that we don't already have a built cache for this check sheet
if(CheckSheetTokenCache.Any(x=>x.CheckSheetId == checkSheetId))
{
CheckSheetTokenCache.Add(BuildCacheEntry(checksheetId));
}
//pull the list of tokens in this check sheet from the cache
var tokens = CheckSheetTokenCache.Where(x=>x.CheckSheetId == checkSheetId).Single();
//give each resolver the tokens to resolve
foreach(var resolver in Resolvers)
foreach(var token in tokens)
{
var resolvedToken = resolver.TryResolve(token, tagId);
//replace the token in the document if we have resolved it
if(resolvedToken != null)
doc.ReplaceAll(token, resolvedToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment