Beispiel #1
0
 /// <summary>
 ///
 /// </summary>
 public TemplatePage()
 {
     if (HttpContext.Current != null)
     {
         dc = new HttpDataContrainer();
     }
     else
     {
         dc = new NormalDataContrainer();
     }
 }
Beispiel #2
0
        internal static string ResolveEntityProperties(IDataContrainer dc, string templateHtml)
        {
            const string keyParttern = "\\$\\{([a-zA-Z][a-zA-Z0-9_]*)\\.([A-Z_a-z][a-zA-Z0-9_]*)\\}";
            IDictionary <string, string>   entityKeys   = new Dictionary <string, string>();
            IDictionary <string, Variable> entityValues = new Dictionary <string, Variable>();

            string       entityName, proName;
            Variable     var;
            PropertyInfo pro;
            object       varValue;

            templateHtml = Regex.Replace(templateHtml, keyParttern, m =>
            {
                entityName = m.Groups[1].Value;
                proName    = m.Groups[2].Value;

                if (!entityValues.Keys.Contains(entityName))
                {
                    varValue = dc.GetVariable(entityName);
                    entityValues.Add(entityName, varValue != null ? (Variable)varValue : default(Variable));
                }

                var = entityValues[entityName];
                if (var.Value != null)
                {
                    //解析属性的值
                    pro = var.Type.GetProperty(entityName,
                                               BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (pro != null)
                    {
                        return((pro.GetValue(var.Value, null) ?? "").ToString());
                    }

                    string message = "";
                    int i          = 0;
                    foreach (
                        PropertyInfo p in
                        var.Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
                                               BindingFlags.IgnoreCase))
                    {
                        message += (++i == 1 ? "" : ",") + p.Name;
                    }

                    throw new NotSupportedException("不支持的属性调用${" + var.Key + "." + proName + "}\n" + var.Key +
                                                    "支持可选的属性:" + message + "\n使用\"${" + var.Key + ".属性}\"进行调用!");
                }
                return(String.Empty);
            });
            return(templateHtml);
        }
Beispiel #3
0
        public static string Complie(IDataContrainer dc, string html, object data)
        {
            string outHtml;

            //======= 设置变量 ======//
            outHtml = SetToVariable(dc, html);

            //======= 求方法 =======//
            if (data != null)
            {
                outHtml = EvalMethodToVar(dc, outHtml, data);
            }

            // outHtml = EntityVariable(dc, outHtml);

            return outHtml;
        }
Beispiel #4
0
        public static string Complie(IDataContrainer dc, string html, object data)
        {
            string outHtml;

            //======= 设置变量 ======//
            outHtml = SetToVariable(dc, html);

            //======= 求方法 =======//
            if (data != null)
            {
                outHtml = EvalMethodToVar(dc, outHtml, data);
            }

            // outHtml = EntityVariable(dc, outHtml);

            return(outHtml);
        }
Beispiel #5
0
        public static string EntityVariable(IDataContrainer dc, string content)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern = "(/*/*)\\$([a-zA-Z][a-zA-Z0-9_]*)=>([a-zA-Z0-9_]+)(\\s+\\B)*"; //设置表达式

            string outHtml = content,
                   varName,
                   entityName;


            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "")
                {
                    return(String.Empty);
                }

                //获取变量及表达式
                varName    = m.Groups[2].Value;
                entityName = m.Groups[3].Value;

                object obj = dc.GetVariable(entityName);
                if (obj != null && obj is Variable)
                {
                    Variable var = (Variable)obj;
                    obj          = var.Value;
                    Type type    = var.Type;

                    //解析属性的值
                    PropertyInfo[] pros = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo pro in pros)
                    {
                        outHtml = Regex.Replace(outHtml,
                                                String.Format("\\$\\{0}{1}\\.{1}{2}", "\\{", varName, pro.Name, "\\}"),
                                                m2 => { return((pro.GetValue(obj, null) ?? "").ToString()); }, RegexOptions.IgnoreCase);
                    }
                }
                return(String.Empty);
            }, RegexOptions.Singleline);
            return(outHtml);
        }
Beispiel #6
0
        public static string EntityVariable(IDataContrainer dc, string content)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern = "(/*/*)\\$([a-zA-Z][a-zA-Z0-9_]*)=>([a-zA-Z0-9_]+)(\\s+\\B)*"; //设置表达式

            string outHtml = content,
                varName,
                entityName;

            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "") return String.Empty;

                //获取变量及表达式
                varName = m.Groups[2].Value;
                entityName = m.Groups[3].Value;

                object obj = dc.GetVariable(entityName);
                if (obj != null && obj is Variable)
                {
                    Variable var = (Variable) obj;
                    obj = var.Value;
                    Type type = var.Type;

                    //解析属性的值
                    PropertyInfo[] pros = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo pro in pros)
                    {
                        outHtml = Regex.Replace(outHtml,
                            String.Format("\\$\\{0}{1}\\.{1}{2}", "\\{", varName, pro.Name, "\\}"),
                            m2 => { return (pro.GetValue(obj, null) ?? "").ToString(); }, RegexOptions.IgnoreCase);
                    }
                }
                return String.Empty;
            }, RegexOptions.Singleline);
            return outHtml;
        }
Beispiel #7
0
        /// <summary>
        /// 执行方法并将返回值赋予变量
        /// </summary>
        /// <param name="data"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string EvalMethodToVar(IDataContrainer dc, string content, object data)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern =
                "(/*/*)\\$([a-zA-Z][a-zA-Z0-9_]*)\\s*=>\\s*([a-zA-Z][a-zA-Z0-9_]*)\\((([^\\)]|\\\\\\))*)\\)(\\s+\\B)*"; //设置表达式

            string outHtml,
                varName,
                methodName,
                paramArray;

            Type type = data.GetType();
            MethodInfo method;
            Type[] parameterTypes;
            object[] parameters;
            int parametersNum;

            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "") return String.Empty;

                //获取变量及表达式
                varName = m.Groups[2].Value;
                methodName = m.Groups[3].Value;
                paramArray = m.Groups[4].Value.Replace("\\,", "__CSP__");

                parameters = paramArray.Trim().Length == 0 ? new object[0] : paramArray.Split(',');
                parametersNum = parameters.Length;

                //查找是否存在方法(方法参数均为string类型)
                parameterTypes = new Type[parametersNum];
                for (int i = 0; i < parametersNum; i++)
                {
                    parameterTypes[i] = typeof (String);
                    parameters[i] =
                        Regex.Replace(parameters[i].ToString(), "\\B\"|\"\\B", String.Empty).Replace("__CSP__", ",");
                }

                method = type.GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase,
                    null,
                    parameterTypes,
                    null);

                if (method != null)
                {
                    Type returnType = method.ReturnType;
                    if (returnType != typeof (void))
                    {
                        object result = method.Invoke(data, parameters);
                        if (result != null)
                        {
                            if (returnType.IsPrimitive || returnType == typeof (String))
                            {
                                dc.DefineVariable(varName, (result ?? "").ToString());
                            }
                            else
                            {
                                dc.DefineVariable(varName,
                                    new Variable {Key = varName, Value = result, Type = returnType});
                            }
                        }
                    }
                }

                /*
                else
                {
                    //throw new MissingMethodException("在对象中找不到");

                    Config.DC.DefineVar(varName, "<span style=\"color:red\">在对象中找不到方法<b>"+methodName+"</b>"+
                        (parametersNum==0?"":",参数"+parametersNum.ToString()+"个")
                        +"。</span>");
                }
                */

                return String.Empty;
            }, RegexOptions.Singleline);
            return outHtml;
        }
Beispiel #8
0
        internal static string ResolveEntityProperties(IDataContrainer dc, string templateHtml)
        {
            const string keyParttern = "\\$\\{([a-zA-Z][a-zA-Z0-9_]*)\\.([A-Z_a-z][a-zA-Z0-9_]*)\\}";
            IDictionary<string, string> entityKeys = new Dictionary<string, string>();
            IDictionary<string, Variable> entityValues = new Dictionary<string, Variable>();

            string entityName, proName;
            Variable var;
            PropertyInfo pro;
            object varValue;

            templateHtml = Regex.Replace(templateHtml, keyParttern, m =>
            {
                entityName = m.Groups[1].Value;
                proName = m.Groups[2].Value;

                if (!entityValues.Keys.Contains(entityName))
                {
                    varValue = dc.GetVariable(entityName);
                    entityValues.Add(entityName, varValue != null ? (Variable) varValue : default(Variable));
                }

                var = entityValues[entityName];
                if (var.Value != null)
                {
                    //解析属性的值
                    pro = var.Type.GetProperty(entityName,
                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (pro != null)
                    {
                        return (pro.GetValue(var.Value, null) ?? "").ToString();
                    }

                    string message = "";
                    int i = 0;
                    foreach (
                        PropertyInfo p in
                            var.Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
                                                   BindingFlags.IgnoreCase))
                    {
                        message += (++i == 1 ? "" : ",") + p.Name;
                    }

                    throw new NotSupportedException("不支持的属性调用${" + var.Key + "." + proName + "}\n" + var.Key +
                                                    "支持可选的属性:" + message + "\n使用\"${" + var.Key + ".属性}\"进行调用!");
                }
                return String.Empty;
            });
            return templateHtml;
        }
Beispiel #9
0
        /// <summary>
        /// 赋值到变量
        /// </summary>
        /// <param name="expression"></param>
        public static string SetToVariable(IDataContrainer dc, string content)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern =
                "(/*/*)\\$([_a-zA-Z][a-zA-Z0-9_]*)\\s*=\\s*(\"(\\\\\"|[^\"\\\\\\n])*\"|[^<>\\s\\\\\\n\"\\$/]+)(\\s+\\B)*";
                //设置表达式
            const string specialVarPattern = "(item|cache|query):(.+)"; //特殊数据

            string outHtml,
                varName,
                varValue,
                varKey;

            Match valueMatch;

            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "") return String.Empty;

                //获取变量及表达式
                varName = m.Groups[2].Value;
                varValue = m.Groups[3].Value;

                if (Regex.IsMatch(varValue, specialVarPattern, RegexOptions.IgnoreCase))
                {
                    valueMatch = Regex.Match(varValue, specialVarPattern, RegexOptions.IgnoreCase);
                    varKey = valueMatch.Groups[2].Value;

                    switch (valueMatch.Groups[1].Value.ToLower())
                    {
                        case "item":
                            dc.DefineVariable(varName, HttpContext.Current.Items[varKey]);
                            break;
                        case "cache":
                            dc.DefineVariable(varName, HttpRuntime.Cache[varKey]);
                            break;
                        case "query":
                            dc.DefineVariable(varName, HttpContext.Current.Request[varKey]);
                            break;
                    }
                }
                else
                {
                    string varRealValue = Regex.Replace(varValue, "\\B\"|\"\\B", String.Empty);

                    //如果为字符,否则读取指定值的变量
                    if (Regex.IsMatch(varValue, "\\B\"|\"\\B"))
                    {
                        dc.DefineVariable(varName, varRealValue);
                    }
                    else
                    {
                        object obj = dc.GetVariable(varRealValue);
                        if (obj != null)
                        {
                            if (obj is Variable)
                            {
                                dc.DefineVariable(varName, (Variable) obj);
                            }
                            else
                            {
                                dc.DefineVariable(varName, obj.ToString());
                            }
                        }
                        else
                        {
                            string message = "";
                            int i = 0;
                            foreach (string key in dc.GetDefineVariable().Keys)
                            {
                                message += (++i == 1 ? "" : "," + key);
                            }
                            throw new NotSupportedException("数据引用键错误:" + m.Value + "\n"
                                                            +
                                                            (message != ""
                                                                ? "受支持可引用的数据键包括" + message + "\n使用\"$" + varName +
                                                                  "=>键\"进行调用!"
                                                                : ""));
                        }
                    }
                }

                return String.Empty;
            }, RegexOptions.Singleline);
            return outHtml;
        }
Beispiel #10
0
        /// <summary>
        /// 赋值到变量
        /// $v = form('key')
        /// </summary>
        public static string SetToVariable(IDataContrainer dc, string content)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern =
                "(/*/*|#*)\\$([_a-zA-Z][a-zA-Z0-9_]*)\\s*=\\s*(\"(\"|[^\"\\n])*\"|[^<>\\s\\n\"\\$/]+)(\\s+\\B)*";
            //设置表达式
            const string specialVarPattern = "(item|cache|query|form)\\([\"']*(.+?)[\"']*\\)"; //特殊数据

            string outHtml,
                   varName,
                   varValue,
                   varKey;

            Match valueMatch;

            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "")
                {
                    return(String.Empty);
                }


                //获取变量及表达式
                varName  = m.Groups[2].Value;
                varValue = m.Groups[3].Value;

                if (Regex.IsMatch(varValue, specialVarPattern, RegexOptions.IgnoreCase))
                {
                    valueMatch = Regex.Match(varValue, specialVarPattern, RegexOptions.IgnoreCase);
                    varKey     = valueMatch.Groups[2].Value;

                    switch (valueMatch.Groups[1].Value.ToLower())
                    {
                    case "item":
                        dc.DefineVariable(varName, HttpContext.Current.Items[varKey]);
                        break;

                    case "cache":
                        dc.DefineVariable(varName, HttpRuntime.Cache[varKey]);
                        break;

                    case "query":
                        dc.DefineVariable(varName, HttpContext.Current.Request[varKey]);
                        break;

                    case "form":
                        dc.DefineVariable(varName, HttpContext.Current.Request.Form[varKey]);
                        break;
                    }
                }
                else
                {
                    string varRealValue = Regex.Replace(varValue, "\\B\"|\"\\B", String.Empty);

                    //如果为字符,否则读取指定值的变量
                    if (Regex.IsMatch(varValue, "\\B\"|\"\\B"))
                    {
                        dc.DefineVariable(varName, varRealValue);
                    }
                    else
                    {
                        object obj = dc.GetVariable(varRealValue);
                        if (obj != null)
                        {
                            if (obj is Variable)
                            {
                                dc.DefineVariable(varName, (Variable)obj);
                            }
                            else
                            {
                                dc.DefineVariable(varName, obj.ToString());
                            }
                        }
                        else
                        {
                            string message = "";
                            int i          = 0;
                            foreach (string key in dc.GetDefineVariable().Keys)
                            {
                                message += (++i == 1 ? "" : "," + key);
                            }
                            throw new NotSupportedException("数据引用键错误:" + m.Value + "\n"
                                                            +
                                                            (message != ""
                                                                ? "受支持可引用的数据键包括" + message + "\n使用\"$" + varName +
                                                             "=>键\"进行调用!"
                                                                : ""));
                        }
                    }
                }

                return(String.Empty);
            }, RegexOptions.Singleline);
            return(outHtml);
        }
Beispiel #11
0
        /// <summary>
        /// 执行方法并将返回值赋予变量
        /// </summary>
        /// <param name="data"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string EvalMethodToVar(IDataContrainer dc, string content, object data)
        {
            //正则模式,支持以下
            // //$menu="123456\" f" sdf
            // $menu=12 fsdf
            // $menu=item:123456
            // $menu=>getmenu(test,item,get)
            const string expressionPattern =
                "(/*/*)\\$([a-zA-Z][a-zA-Z0-9_]*)\\s*=>\\s*([a-zA-Z][a-zA-Z0-9_]*)\\((([^\\)]|\\\\\\))*)\\)(\\s+\\B)*"; //设置表达式

            string outHtml,
                   varName,
                   methodName,
                   paramArray;

            Type       type = data.GetType();
            MethodInfo method;

            Type[]   parameterTypes;
            object[] parameters;
            int      parametersNum;


            outHtml = Regex.Replace(content, expressionPattern, m =>
            {
                //注释
                if (m.Groups[1].Value != "")
                {
                    return(String.Empty);
                }


                //获取变量及表达式
                varName    = m.Groups[2].Value;
                methodName = m.Groups[3].Value;
                paramArray = m.Groups[4].Value.Replace("\\,", "__CSP__");

                parameters    = paramArray.Trim().Length == 0 ? new object[0] : paramArray.Split(',');
                parametersNum = parameters.Length;

                //查找是否存在方法(方法参数均为string类型)
                parameterTypes = new Type[parametersNum];
                for (int i = 0; i < parametersNum; i++)
                {
                    parameterTypes[i] = typeof(String);
                    parameters[i]     =
                        Regex.Replace(parameters[i].ToString(), "\\B\"|\"\\B", String.Empty).Replace("__CSP__", ",");
                }

                method = type.GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase,
                    null,
                    parameterTypes,
                    null);

                if (method != null)
                {
                    Type returnType = method.ReturnType;
                    if (returnType != typeof(void))
                    {
                        object result = method.Invoke(data, parameters);
                        if (result != null)
                        {
                            if (returnType.IsPrimitive || returnType == typeof(String))
                            {
                                dc.DefineVariable(varName, (result ?? "").ToString());
                            }
                            else
                            {
                                dc.DefineVariable(varName,
                                                  new Variable {
                                    Key = varName, Value = result, Type = returnType
                                });
                            }
                        }
                    }
                }

                /*
                 * else
                 * {
                 *  //throw new MissingMethodException("在对象中找不到");
                 *
                 *  Config.DC.DefineVar(varName, "<span style=\"color:red\">在对象中找不到方法<b>"+methodName+"</b>"+
                 *      (parametersNum==0?"":",参数"+parametersNum.ToString()+"个")
                 +"。</span>");
                 * }
                 */

                return(String.Empty);
            }, RegexOptions.Singleline);
            return(outHtml);
        }