Esempio n. 1
0
        /// <summary>
        /// 克隆当前元素到新的宿主模板
        /// </summary>
        /// <param name="ownerTemplate"></param>
        /// <returns></returns>
        internal override Element Clone(Template ownerTemplate)
        {
            VariableTag tag = new VariableTag(ownerTemplate, (VariableExpression)this.VarExpression.Clone(ownerTemplate));

            foreach (var att in this.Attributes)
            {
                tag.Attributes.Add(att.Clone(ownerTemplate));
            }
            foreach (IExpression exp in this.CallFunctions)
            {
                tag.CallFunctions.Add(exp.Clone(ownerTemplate));
            }
            return(tag);
        }
Esempio n. 2
0
        /// <summary>
        /// 构建变量元素
        /// </summary>
        /// <param name="ownerTemplate">宿主模板</param>
        /// <param name="container">标签的容器</param>
        /// <param name="match"></param>
        internal static VariableTag CreateVariableTag(Template ownerTemplate, Tag container, Match match)
        {
            string             prefix;
            Variable           variable   = CreateVariable(ownerTemplate, match, out prefix);
            VariableIdentity   variableId = new VariableIdentity(ownerTemplate, variable, prefix);
            VariableExpression varExp     = CreateVariableExpression(variableId, match);

            VariableTag tag = new VariableTag(ownerTemplate, varExp);

            //解析属性列表
            ParseElementAttributes(tag, match);
            container.AppendChild(tag);

            return(tag);
        }
Esempio n. 3
0
        /// <summary>
        /// 呈现本元素的数据
        /// </summary>
        /// <param name="writer"></param>
        public override void Render(System.IO.TextWriter writer)
        {
            VariableTag tag   = this;
            object      value = this.VarExpression.GetValue();

            //调用自定函数处理此变量的值
            if (this.CallFunctions.Count > 0)
            {
                foreach (var exp in this.CallFunctions)
                {
                    object method = exp.GetValue();
                    if (!Utility.IsNothing(method))
                    {
                        UserDefinedFunction func;
                        if (this.OwnerTemplate.UserDefinedFunctions.TryGetValue(method.ToString(), out func))
                        {
                            value = func(new object[] { value });
                        }
                    }
                }
            }

            if (Utility.IsNothing(value))
            {
                return;
            }

            bool   formated = false;
            string text     = string.Empty;
            string format   = this.Format == null ? string.Empty : this.Format.GetTextValue();

            if (value is string)
            {
                //字符串
                text = (string)value;
            }
            else
            {
                //非字符串.则判断处理format
                if (!string.IsNullOrEmpty(format))
                {
                    IFormattable iformat = value as IFormattable;
                    if (iformat != null)
                    {
                        text     = iformat.ToString(format, CultureInfo.InvariantCulture);
                        formated = true;
                    }
                }
                //非IFormattable接口.则直接取字符串处理
                if (!formated)
                {
                    //如果数据是IEnumerable,IEnumerator接口则进行数据拼凑
                    IEnumerator ie = null;
                    if (value is IEnumerable)
                    {
                        ie = ((IEnumerable)value).GetEnumerator();
                    }
                    else if (value is IEnumerator)
                    {
                        ie = (IEnumerator)value;
                    }
                    if (ie != null)
                    {
                        StringBuilder buffer = new StringBuilder();
                        ie.Reset();
                        while (ie.MoveNext())
                        {
                            if (buffer.Length != 0)
                            {
                                buffer.Append(",");
                            }
                            buffer.Append(ie.Current);
                        }
                        text = buffer.ToString();
                    }
                    else
                    {
                        text = value.ToString();
                    }
                }
            }

            if (text.Length > 0)
            {
                bool removeHtml   = this.RemoveHtml == null ? false : TypeConvert.ToBool(this.RemoveHtml.GetTextValue());
                bool textEncode   = this.TextEncode == null ? false : TypeConvert.ToBool(this.TextEncode.GetTextValue());
                bool htmlEncode   = this.HtmlEncode == null ? false : TypeConvert.ToBool(this.HtmlEncode.GetTextValue());
                bool xmlEncode    = this.XmlEncode == null ? false : TypeConvert.ToBool(this.XmlEncode.GetTextValue());
                bool jsEncode     = this.JsEncode == null ? false : TypeConvert.ToBool(this.JsEncode.GetTextValue());
                bool urlEncode    = this.UrlEncode == null ? false : TypeConvert.ToBool(this.UrlEncode.GetTextValue());
                bool compressText = this.CompressText == null ? false : TypeConvert.ToBool(this.CompressText.GetTextValue());

                int      length     = this.Length == null ? 0 : TypeConvert.ToInt32(this.Length.GetTextValue());
                string   appendText = this.AppendText == null ? string.Empty : this.AppendText.GetTextValue();
                Encoding charset    = this.Charset == null ? this.OwnerTemplate.Charset : Utility.GetEncodingFromCharset(this.Charset.GetTextValue(), this.OwnerTemplate.Charset);

                if (removeHtml)
                {
                    text = Utility.RemoveHtmlCode(text);
                }
                if (length > 0)
                {
                    text = Utility.CutString(text, length, appendText);
                }
                if (textEncode)
                {
                    text = Utility.TextEncode(text);
                }
                else if (htmlEncode)
                {
                    text = HttpUtility.HtmlEncode(text);
                }
                if (xmlEncode)
                {
                    text = Utility.XmlEncode(text);
                }
                if (jsEncode)
                {
                    text = Utility.JsEncode(text);
                }
                if (urlEncode)
                {
                    text = HttpUtility.UrlEncode(text, charset);
                }
                if (compressText)
                {
                    text = Utility.CompressText(text);
                }

                if (!formated && !string.IsNullOrEmpty(format))
                {
                    text = string.Format(format, text);
                }


                writer.Write(text);
            }
        }