/// <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); }
/// <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, true); VariableTag tag = new VariableTag(ownerTemplate, varExp); //解析属性列表 ParseElementAttributes(tag, match); container.AppendChild(tag); return tag; }
/// <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 : Utility.ConverToBoolean(this.RemoveHtml.GetTextValue()); bool textEncode = this.TextEncode == null ? false : Utility.ConverToBoolean(this.TextEncode.GetTextValue()); bool htmlEncode = this.HtmlEncode == null ? false : Utility.ConverToBoolean(this.HtmlEncode.GetTextValue()); bool xmlEncode = this.XmlEncode == null ? false : Utility.ConverToBoolean(this.XmlEncode.GetTextValue()); bool jsEncode = this.JsEncode == null ? false : Utility.ConverToBoolean(this.JsEncode.GetTextValue()); bool urlEncode = this.UrlEncode == null ? false : Utility.ConverToBoolean(this.UrlEncode.GetTextValue()); bool compressText = this.CompressText == null ? false : Utility.ConverToBoolean(this.CompressText.GetTextValue()); int length = this.Length == null ? 0 : Utility.ConverToInt32(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); Encoding charset = Encoding.UTF8; if (removeHtml) { text = Utility.RemoveHtmlCode(text); } if (length > 0) { text = Utility.CutString(text, length, charset, 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); } }
/// <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; }