2013-04-18 23:26:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2013-05-03 12:44:51 +00:00
|
|
|
|
namespace Mustache
|
2013-04-18 23:26:58 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates text by substituting an object's values for placeholders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class Generator
|
|
|
|
|
{
|
|
|
|
|
private readonly IGenerator _generator;
|
2013-04-25 01:21:00 +00:00
|
|
|
|
private readonly List<EventHandler<KeyFoundEventArgs>> _foundHandlers;
|
|
|
|
|
private readonly List<EventHandler<KeyNotFoundEventArgs>> _notFoundHandlers;
|
2013-04-18 23:26:58 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of a Generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="generator">The text generator to wrap.</param>
|
|
|
|
|
internal Generator(IGenerator generator)
|
|
|
|
|
{
|
|
|
|
|
_generator = generator;
|
2013-04-25 01:21:00 +00:00
|
|
|
|
_foundHandlers = new List<EventHandler<KeyFoundEventArgs>>();
|
|
|
|
|
_notFoundHandlers = new List<EventHandler<KeyNotFoundEventArgs>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a key/property is found.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<KeyFoundEventArgs> KeyFound
|
|
|
|
|
{
|
|
|
|
|
add { _foundHandlers.Add(value); }
|
|
|
|
|
remove { _foundHandlers.Remove(value); }
|
2013-04-18 23:26:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a key/property is not found in the object graph.
|
|
|
|
|
/// </summary>
|
2013-04-25 01:21:00 +00:00
|
|
|
|
public event EventHandler<KeyNotFoundEventArgs> KeyNotFound
|
2013-04-18 23:26:58 +00:00
|
|
|
|
{
|
2013-04-25 01:21:00 +00:00
|
|
|
|
add { _notFoundHandlers.Add(value); }
|
|
|
|
|
remove { _notFoundHandlers.Remove(value); }
|
2013-04-18 23:26:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the text that is generated for the given object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The object to generate the text with.</param>
|
|
|
|
|
/// <returns>The text generated for the given object.</returns>
|
|
|
|
|
public string Render(object source)
|
|
|
|
|
{
|
|
|
|
|
return render(CultureInfo.CurrentCulture, source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the text that is generated for the given object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="provider">The format provider to use.</param>
|
|
|
|
|
/// <param name="source">The object to generate the text with.</param>
|
|
|
|
|
/// <returns>The text generated for the given object.</returns>
|
|
|
|
|
public string Render(IFormatProvider provider, object source)
|
|
|
|
|
{
|
|
|
|
|
if (provider == null)
|
|
|
|
|
{
|
|
|
|
|
provider = CultureInfo.CurrentCulture;
|
|
|
|
|
}
|
|
|
|
|
return render(provider, source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string render(IFormatProvider provider, object source)
|
|
|
|
|
{
|
|
|
|
|
KeyScope scope = new KeyScope(source);
|
2013-04-25 01:21:00 +00:00
|
|
|
|
foreach (EventHandler<KeyFoundEventArgs> handler in _foundHandlers)
|
|
|
|
|
{
|
|
|
|
|
scope.KeyFound += handler;
|
|
|
|
|
}
|
|
|
|
|
foreach (EventHandler<KeyNotFoundEventArgs> handler in _notFoundHandlers)
|
2013-04-18 23:26:58 +00:00
|
|
|
|
{
|
|
|
|
|
scope.KeyNotFound += handler;
|
|
|
|
|
}
|
|
|
|
|
StringWriter writer = new StringWriter(provider);
|
|
|
|
|
_generator.GetText(scope, writer);
|
|
|
|
|
return writer.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|