2013-04-25 01:21:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2013-05-03 12:44:51 +00:00
|
|
|
|
namespace Mustache
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Associates parameters to their argument values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal sealed class ArgumentCollection
|
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
private readonly Dictionary<TagParameter, IArgument> _argumentLookup;
|
2013-04-25 01:21:00 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of an ArgumentCollection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ArgumentCollection()
|
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
_argumentLookup = new Dictionary<TagParameter, IArgument>();
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Associates the given parameter to the key placeholder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parameter">The parameter to associate the key with.</param>
|
2014-05-21 21:02:31 +00:00
|
|
|
|
/// <param name="key">The argument.</param>
|
2013-04-25 01:21:00 +00:00
|
|
|
|
/// <remarks>If the key is null, the default value of the parameter will be used.</remarks>
|
2014-05-21 21:02:31 +00:00
|
|
|
|
public void AddArgument(TagParameter parameter, IArgument argument)
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
_argumentLookup.Add(parameter, argument);
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the key that will be used to find the substitute value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parameterName">The name of the parameter.</param>
|
|
|
|
|
public string GetKey(TagParameter parameter)
|
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
IArgument argument;
|
|
|
|
|
if (_argumentLookup.TryGetValue(parameter, out argument) && argument != null)
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
return argument.GetKey();
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Substitutes the key placeholders with their respective values.
|
|
|
|
|
/// </summary>
|
2013-08-17 03:35:46 +00:00
|
|
|
|
/// <param name="keyScope">The key/value pairs in the current lexical scope.</param>
|
|
|
|
|
/// <param name="contextScope">The key/value pairs in current context.</param>
|
2013-04-25 01:21:00 +00:00
|
|
|
|
/// <returns>A dictionary associating the parameter name to the associated value.</returns>
|
2013-08-17 03:35:46 +00:00
|
|
|
|
public Dictionary<string, object> GetArguments(Scope keyScope, Scope contextScope)
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> arguments = new Dictionary<string,object>();
|
2014-05-21 21:02:31 +00:00
|
|
|
|
foreach (KeyValuePair<TagParameter, IArgument> pair in _argumentLookup)
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
|
|
|
|
object value;
|
|
|
|
|
if (pair.Value == null)
|
|
|
|
|
{
|
|
|
|
|
value = pair.Key.DefaultValue;
|
|
|
|
|
}
|
2016-09-19 15:26:03 +00:00
|
|
|
|
|
2013-04-25 01:21:00 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
value = pair.Value.GetValue(keyScope, contextScope);
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
arguments.Add(pair.Key.Name, value);
|
|
|
|
|
}
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
2013-08-17 03:35:46 +00:00
|
|
|
|
|
2013-10-28 19:58:50 +00:00
|
|
|
|
public Dictionary<string, object> GetArgumentKeyNames()
|
2013-08-17 03:35:46 +00:00
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
return _argumentLookup.ToDictionary(p => p.Key.Name, p => (object)GetKey(p.Key));
|
2013-08-17 03:35:46 +00:00
|
|
|
|
}
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|