100 lines
2.8 KiB
C#
100 lines
2.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections;
|
|
|
|
namespace ln.templates.elements
|
|
{
|
|
public class ConditionalElement : ContainerElement
|
|
{
|
|
private ContainerElement alternativeContainer;
|
|
|
|
public Expression Expression { get; private set; }
|
|
|
|
public ConditionalElement(ContainerElement container,Expression expression)
|
|
:base(container)
|
|
{
|
|
Expression = expression;
|
|
AlternativeContainer = new ContainerElement(null);
|
|
}
|
|
|
|
private bool IsTrue(object value)
|
|
{
|
|
return (
|
|
(value != null) &&
|
|
((value is int) && ((int)value != 0)) ||
|
|
((value is double) && ((double)value != 0.0)) ||
|
|
((value is bool) && ((bool)value)) ||
|
|
((value is string) && (!String.Empty.Equals(value)))
|
|
);
|
|
}
|
|
|
|
public override void Generate(TextWriter writer, Template.Context context)
|
|
{
|
|
if (Expression.IsTrue(context.ExpressionContext))
|
|
{
|
|
GenerateChildren(writer, context);
|
|
}
|
|
else if (AlternativeContainer != null)
|
|
{
|
|
AlternativeContainer.Generate(writer, context);
|
|
}
|
|
}
|
|
|
|
public ContainerElement AlternativeContainer {
|
|
get
|
|
{
|
|
return alternativeContainer;
|
|
}
|
|
set
|
|
{
|
|
if (alternativeContainer != null)
|
|
{
|
|
alternativeContainer.Container = null;
|
|
}
|
|
if (value != null)
|
|
{
|
|
value.Container = Container;
|
|
}
|
|
alternativeContainer = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class IteratorElement : ConditionalElement
|
|
{
|
|
public String IterName { get; private set; }
|
|
|
|
public IteratorElement(ContainerElement container,String iterName,Expression expression)
|
|
:base(container, expression)
|
|
{
|
|
IterName = iterName;
|
|
}
|
|
|
|
public override void Generate(TextWriter writer, Template.Context context)
|
|
{
|
|
object v = Expression.Evaluate(context.ExpressionContext);
|
|
|
|
if (v == null)
|
|
{
|
|
AlternativeContainer.Generate(writer, context);
|
|
}
|
|
else if (v is IEnumerable)
|
|
{
|
|
Template.Context icontext = new Template.Context(context,null);
|
|
|
|
bool b = false;
|
|
IEnumerable en = v as IEnumerable;
|
|
foreach (object o in en)
|
|
{
|
|
b = true;
|
|
icontext.ExpressionContext.SetMappedValue(IterName, o);
|
|
GenerateChildren(writer, icontext);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|