ln.templates/FormContext.cs

255 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using System.Linq;
using System.ComponentModel;
using System.Resources;
namespace ln.templates
{
//public class FormContext
//{
//private Dictionary<string, object> vars = new Dictionary<string, object>();
//protected MemoryStream contentStream, headStream;
//public Template SharpForm { get; }
//public Request Request { get; }
//public Stream ContentStream => contentStream;
//public TextWriter ContentWriter { get; protected set; }
//public Stream HeadStream => headStream;
//public TextWriter HeadWriter { get; protected set; }
//public FormContext Parent { get; }
//public bool UsesClonedVars { get; }
//public bool IsRootContext => Parent == null;
//public FormContext SubFrameContext { get; protected set; }
//public FormContext(Template sharpForm,Request request,object o)
//{
// Parent = null;
// SharpForm = sharpForm;
// Request = request;
// contentStream = new MemoryStream();
// ContentWriter = new StreamWriter(contentStream);
// headStream = new MemoryStream();
// HeadWriter = new StreamWriter(headStream);
// vars["o"] = o;
// vars["this"] = o;
// vars["StrMax"] = GetType().GetMethod("StrMax");
//}
//public FormContext(FormContext parent,bool cloneVars = false,bool independentContent = false,FormContext subFrameContext = null)
//{
// Parent = parent;
// SharpForm = parent.SharpForm;
// Request = parent.Request;
// contentStream = independentContent ? new MemoryStream() : parent.contentStream;
// ContentWriter = new StreamWriter(ContentStream);
// headStream = parent.headStream;
// HeadWriter = parent.HeadWriter;
// UsesClonedVars = cloneVars;
// SubFrameContext = subFrameContext == null ? parent.SubFrameContext : subFrameContext;
// if (cloneVars){
// foreach (KeyValuePair<string,object> var in parent.vars)
// {
// this.vars.Add(var.Key, var.Value);
// }
// }
// parent.ContentWriter.Flush();
//}
//public byte[] ContentBytes
//{
// get
// {
// ContentWriter.Flush();
// return contentStream.ToArray();
// }
//}
//public byte[] HeadBytes
//{
// get
// {
// HeadWriter.Flush();
// return headStream.ToArray();
// }
//}
//public String Content => Encoding.UTF8.GetString(ContentBytes);
//public String Head => Encoding.UTF8.GetString(HeadBytes);
//public void Insert(FormContext context, bool allToHead = false)
//{
// if (context.headStream != headStream)
// {
// HeadWriter.Flush();
// byte[] headBytes = context.HeadBytes;
// headStream.Write(headBytes, 0, headBytes.Length);
// }
// ContentWriter.Flush();
// byte[] contentBytes = context.ContentBytes;
// if (allToHead)
// headStream.Write(contentBytes, 0, contentBytes.Length);
// else
// contentStream.Write(contentBytes, 0, contentBytes.Length);
//}
//public bool HasVar(String name)
//{
// if (vars.ContainsKey(name))
// return true;
// if (!UsesClonedVars && (Parent != null))
// return Parent.HasVar(name);
// return false;
//}
//public object Get(String name){
// if (vars.ContainsKey(name))
// return vars[name];
// else if (Parent != null)
// return Parent.Get(name);
// else
// return null;
//}
//public void Set(String name,object value){
// if (!UsesClonedVars && (Parent != null) && (Parent.HasVar(name)))
// {
// Parent.Set(name, value);
// } else {
// this.vars[name] = value;
// }
//}
//public object EvaluateExpression(Token[] tokens)
//{
// if (tokens.Length == 0)
// {
// return "";
// }
// Stack<Token> tokenStack = new Stack<Token>(tokens);
// object currentValue = null;
// Token currentToken = null;
// while (tokenStack.Count > 0){
// currentToken = tokenStack.Pop();
// if (currentToken is PathToken)
// {
// PathToken pathToken = (PathToken)currentToken;
// Response response = Request.SubRequest(pathToken.Path);
// if (response.Reference != null)
// currentValue = response.Reference;
// else
// currentValue = Encoding.UTF8.GetString(response.ContentBytes);
// } else if (currentToken is KeywordToken)
// {
// KeywordToken keywordToken = (KeywordToken)currentToken;
// currentValue = keywordToken.Evaluate(this);
// }
// if (tokenStack.Count > 0){
// currentToken = tokenStack.Pop();
// throw new NotImplementedException("Currently no expression operators are implemented, sorry");
// }
// }
// return currentValue;
//}
//public object EvaluateValue(String sValue)
//{
// Stack<String> vpath = new Stack<string>(sValue.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Reverse());
// if (vpath.Count > 0)
// {
// object value = null;
// string key = vpath.Pop();
// value = Get(key);
// while (vpath.Count > 0)
// {
// key = vpath.Pop();
// value = GetObjectFieldOrProperty(value, key);
// }
// return value;
// }
// return null;
//}
//public bool EvaluateBoolean(String expression)
//{
// bool invert = (expression[0] == '!');
// if (invert)
// expression = expression.Substring(1);
// object value = EvaluateValue(expression);
// if (value != null)
// {
// if (
// ((value is bool) && (((bool)value))) ||
// ((value is string) && (!String.Empty.Equals(value))) ||
// ((value is int) && (((int)value) != 0)) ||
// ((value is long) && (((long)value) != 0)) ||
// ((value is float) && (((float)value) != 0)) ||
// ((value is double) && (((double)value) != 0))
// )
// {
// return !invert;
// }
// }
// return invert;
//}
//private object GetObjectFieldOrProperty(object o, string name)
//{
// if (o == null)
// {
// return null;
// }
// FieldInfo fieldInfo = o.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
// if (fieldInfo != null)
// {
// return fieldInfo.GetValue(o);
// }
// PropertyInfo propertyInfo = o.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
// if (propertyInfo != null)
// {
// return propertyInfo.GetValue(o);
// }
// throw new KeyNotFoundException(String.Format("object of type {0} has no field/property named {1}", o.GetType().FullName, name));
//}
//public static string StrMax(string s,int len,string suffix = "")
//{
// if (len < s.Length)
// return s.Substring(0, len) + suffix;
// return s;
//}
// }
}