internal static void FilledInTagList(string tagdef, ArrayList tagList, IResourceDependency res) { int idxBegin = 0, idxEnd = 0; idxBegin = tagdef.IndexOf("#}"); idxEnd = tagdef.LastIndexOf("{#"); tagdef = tagdef.Substring(idxBegin+2, idxEnd - idxBegin-2).Trim(); Regex regEx = new Regex(TagBase.TagDefinitionPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); idxBegin = idxEnd = 0; MatchCollection mc = regEx.Matches(tagdef, idxBegin); HtmlTextTag htmlTag = null; while (mc.Count > 0) { Match m = mc[0]; idxEnd = m.Index; if (idxEnd > idxBegin) { htmlTag = new HtmlTextTag(tagdef.Substring(idxBegin, idxEnd - idxBegin)); tagList.Add(htmlTag); } TagBase tag = new TagBase(m.Value, m.Index, ref tagdef); //Util.Debug(tag.Category); if (tag.Category == TagCategory.AutoTag) { AutoItem item = AutoItem.Parse(tag); if (res.GetType() == typeof(PagerTag)) { item.CallerTag = (PagerTag)res; } tagList.Add(item); } else if (tag.Category == TagCategory.DefineTag) { //数据库标签定义格式为 {#%FieldName%#} if (res != null) { htmlTag = new HtmlTextTag(res.GetDefinition(tag.OuterDefineText).ToString()); tagList.Add(htmlTag); } } else if (tag.Category == TagCategory.CustomTag) { #region 数据定义标签内包含数据定义标签 DbStoredCustomTag dbTag = DbStoredCustomTag.Parse(tag); dbTag.SetResourceDependency(res); if (dbTag.IsExist == true) { htmlTag = new HtmlTextTag(dbTag.GetTagValue().ToString()); tagList.Add(htmlTag); } #endregion } else if (tag.Category == TagCategory.SystemTag) { string pageURLKey = ",FirstUrl,PreUrl,NextUrl,LastUrl,CurUrl,"; string tagName = tag.TagName.Trim('$'); if (pageURLKey.IndexOf("," + tagName + ",", StringComparison.InvariantCultureIgnoreCase) != -1) { tagName = tagName.ToLower(); PageUrl url = new PageUrl(tag.OuterDefineText); url.IsAutoItem = (res != null && res.GetType() == typeof(AutoItem)); url.SetResourceDependency(res); switch (tagName) { case "firsturl": url.UrlCategory = PageUrlCategory.FirstPage; break; case "preurl": url.UrlCategory = PageUrlCategory.PrePage; break; case "nexturl": url.UrlCategory = PageUrlCategory.NextPage; break; case "lasturl": url.UrlCategory = PageUrlCategory.LastPage; break; case "cururl": url.UrlCategory = PageUrlCategory.ThisPage; break; default: url.UrlCategory = PageUrlCategory.ThisPage; break; } tagList.Add(url); } else { SystemTag sys = new SystemTag(tag.OuterDefineText); //Util.Debug(false, res.GetType().ToString()); sys.SetResourceDependency(res); tagList.Add(sys); } } else { tagList.Add(tag); } //Util.Debug(false, "#####\n" + tag.OuterDefineText + "\n#####"); //TagList.Add(tag); idxBegin = tag.DefinedIndexEnd; mc = regEx.Matches(tagdef, idxBegin); } if (idxBegin < tagdef.Length) { htmlTag = new HtmlTextTag(tagdef.Substring(idxBegin)); tagList.Add(htmlTag); } }
/// <summary> /// 解析模板内容 /// </summary> public void SetTaggedObjectCollection(string tptContent) { StringBuilder sb = new StringBuilder(tptContent.Length); Regex regEx = new Regex(TagBase.TagDefinitionPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); int idxBegin = 0, idxEnd = 0; //Util.Debug(false, "------------------------begin---------------"); MatchCollection mc = regEx.Matches(tptContent, idxBegin); while (mc.Count > 0) { Match m = mc[0]; idxEnd = m.Index; if (idxEnd > idxBegin) { //Util.Debug(false, tptContent.Substring(idxBegin, idxEnd - idxBegin)); sb.Append(tptContent.Substring(idxBegin, idxEnd - idxBegin)); } TagBase tag = new TagBase(m.Value, m.Index, ref tptContent); //Util.Debug(false, "#####\n" + tag.OuterDefineText + " " + tag.Category.ToString() + "\n#####"); //OleDbHelper.AppendToFile("~/debug.log", "parse Tag..." + tag.OuterDefineText + " \r\n " + tag.Category.ToString() + Environment.NewLine + Environment.NewLine); #region 处理标签内容 if (tag.Category == TagCategory.CustomTag) { DbStoredCustomTag dbTag = DbStoredCustomTag.Parse(tag); dbTag.SetResourceDependency(this.GetResourceDependency()); //Util.Debug(false, "tpp: " + this.GetResourceDependency() + dbTag.OuterDefineText); if (dbTag.IsExist == true) { dbTag.Caller = this; sb.Append(dbTag.GetTagValue()); if (!this.TagTrack.ContainsKey("B" + dbTag.IDentity)) { this.TagTrack.Add("B" + dbTag.IDentity, dbTag.PublishDate.ToString("yyyyMMddHHmmss")); } } } else if (tag.Category == TagCategory.DataListTag) { //{#List:Repeater#} ListTag list = ListTag.Parse(tag, this.GetResourceDependency()); //OleDbHelper.AppendToFile("~/debug.log", System.Environment.NewLine + " Res:" // + this.GetResourceDependency().ToString()); if (this.GeneratorListContent == true) { #region 直接获取列表标签内容 if (list.GetPageCount() < 2) { sb.Append(list.GetTagValue()); } else { string pagedAliaContent = (string.IsNullOrEmpty(list.IDentity)) ? new Guid().ToString() : list.IDentity; SetDynamicPagedAlia(pagedAliaContent, list); sb.Append(pagedAliaContent); } #endregion } else { //设置列表标签占位符号 string pagedAliaContent = (string.IsNullOrEmpty(list.IDentity)) ? new Guid().ToString() : list.IDentity; SetDynamicPagedAlia(pagedAliaContent, list); sb.Append(pagedAliaContent); } } else if (tag.Category == TagCategory.PagerTag) { //{#Pager#} PagerTag pager = PagerTag.Parse(tag); string pagerAlia = (string.IsNullOrEmpty(pager.IDentity)) ? new Guid().ToString() : pager.IDentity; SetDynamicPagerDependency(pagerAlia, pager); sb.Append(pagerAlia); } else if (tag.Category == TagCategory.ServerTag) { //服务端标签定义格式为 } else if (tag.Category == TagCategory.SystemTag) { //系统标签(函数)定义格式为 {#$ $#} {# ... () ... #} // 系统标签: {#$this.PrimaryNews["Content"]$#} if (tag.OuterDefineText.StartsWith("{#$this.") && this._resDependency != null) { sb.Append(this.GetResourceDependency().GetDefinition(tag.OuterDefineText)); } else { SystemTag sysTag = new SystemTag(tag.TagDefinition); sysTag.SetResourceDependency(GetResourceDependency()); sb.Append(sysTag.ToString()); } //Util.Debug(false, tag.OuterDefineText); //OleDbHelper.AppendToFile("~/log.txt", tag.OuterDefineText); } else if (tag.Category == TagCategory.DefineTag) { //数据库标签定义格式为 {#%FieldName%#} if (this._resDependency != null) { //Util.Debug(false, tag.OuterDefineText); sb.Append(this.GetResourceDependency().GetDefinition(tag.OuterDefineText)); } } #endregion idxBegin = tag.DefinedIndexEnd; //Util.Debug(false, tptContent.Substring(idxBegin, 5)); //Util.Debug(m.Value); mc = regEx.Matches(tptContent, idxBegin); } if (idxBegin < tptContent.Length) { sb.Append(tptContent.Substring(idxBegin)); //Util.Debug(false, tptContent.Substring(idxBegin)); } this.ParsedResult = sb.ToString(); //Util.Debug(false, "------------------------End---------------"); //OleDbHelper.SetTextFileContent(@"~/@docs/temp.html", "utf-8", // HtmlTextGenerator.GetRightHtmlText("/templet/1/", sb.ToString())); }
internal static void FilledInTagList(string tagdef, ArrayList tagList, IResourceDependency res) { int idxBegin = 0, idxEnd = 0; idxBegin = tagdef.IndexOf("#}"); idxEnd = tagdef.LastIndexOf("{#"); tagdef = tagdef.Substring(idxBegin + 2, idxEnd - idxBegin - 2).Trim(); Regex regEx = new Regex(TagBase.TagDefinitionPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); idxBegin = idxEnd = 0; MatchCollection mc = regEx.Matches(tagdef, idxBegin); HtmlTextTag htmlTag = null; while (mc.Count > 0) { Match m = mc[0]; idxEnd = m.Index; if (idxEnd > idxBegin) { htmlTag = new HtmlTextTag(tagdef.Substring(idxBegin, idxEnd - idxBegin)); tagList.Add(htmlTag); } TagBase tag = new TagBase(m.Value, m.Index, ref tagdef); //Util.Debug(tag.Category); if (tag.Category == TagCategory.AutoTag) { AutoItem item = AutoItem.Parse(tag); if (res.GetType() == typeof(PagerTag)) { item.CallerTag = (PagerTag)res; } tagList.Add(item); } else if (tag.Category == TagCategory.DefineTag) { //数据库标签定义格式为 {#%FieldName%#} if (res != null) { htmlTag = new HtmlTextTag(res.GetDefinition(tag.OuterDefineText).ToString()); tagList.Add(htmlTag); } } else if (tag.Category == TagCategory.CustomTag) { #region 数据定义标签内包含数据定义标签 DbStoredCustomTag dbTag = DbStoredCustomTag.Parse(tag); dbTag.SetResourceDependency(res); if (dbTag.IsExist == true) { htmlTag = new HtmlTextTag(dbTag.GetTagValue().ToString()); tagList.Add(htmlTag); } #endregion } else if (tag.Category == TagCategory.SystemTag) { string pageURLKey = ",FirstUrl,PreUrl,NextUrl,LastUrl,CurUrl,"; string tagName = tag.TagName.Trim('$'); if (pageURLKey.IndexOf("," + tagName + ",", StringComparison.InvariantCultureIgnoreCase) != -1) { tagName = tagName.ToLower(); PageUrl url = new PageUrl(tag.OuterDefineText); url.IsAutoItem = (res != null && res.GetType() == typeof(AutoItem)); url.SetResourceDependency(res); switch (tagName) { case "firsturl": url.UrlCategory = PageUrlCategory.FirstPage; break; case "preurl": url.UrlCategory = PageUrlCategory.PrePage; break; case "nexturl": url.UrlCategory = PageUrlCategory.NextPage; break; case "lasturl": url.UrlCategory = PageUrlCategory.LastPage; break; case "cururl": url.UrlCategory = PageUrlCategory.ThisPage; break; default: url.UrlCategory = PageUrlCategory.ThisPage; break; } tagList.Add(url); } else { SystemTag sys = new SystemTag(tag.OuterDefineText); //Util.Debug(false, res.GetType().ToString()); sys.SetResourceDependency(res); tagList.Add(sys); } } else { tagList.Add(tag); } //Util.Debug(false, "#####\n" + tag.OuterDefineText + "\n#####"); //TagList.Add(tag); idxBegin = tag.DefinedIndexEnd; mc = regEx.Matches(tagdef, idxBegin); } if (idxBegin < tagdef.Length) { htmlTag = new HtmlTextTag(tagdef.Substring(idxBegin)); tagList.Add(htmlTag); } }
/// <summary> /// 转换描述文本为表达式数组 /// </summary> /// <remarks> /// a: {#$ TrimHTML(%Title%) $#} /// b: {#$ Repeat(5,%Title%) $#} /// c: {#$ Length(TrimHTML(%Title%)) $#} /// d: {#$ Length(TrimHTML(%Title%)) > 3 ? "太长" : "OK" $#} /// e: {#$ Replace(TrimHTML(%Title%), "电子"," ") $#} /// f: {#$ ReplaceX(TrimHTML(%Title%), "\w","") $#} /// </remarks> public static List<InnerExpression> GetInnerExpressions(string ExpPaper, IResourceDependency Res) { //OleDbHelper.AppendToFile("~/debug.txt", "\n\n 解析:" + ExpPaper); List<InnerExpression> expList = new List<InnerExpression>(); int lenT = ExpPaper.Length; int cursor = 0, iflag = 0; char chr = ExpPaper[cursor]; string strTemp = "", strExpression = ""; ReservedWords words = null; while (cursor < lenT) { #region 字符扫描 chr = ExpPaper[cursor]; if (!ReservedWords.IsReservedChar(chr)) { ++cursor; continue; } else { if (cursor > iflag) { strTemp = ExpPaper.Substring(iflag, cursor - iflag).Trim(); } iflag = cursor; words = new ReservedWords(chr); if (words.IsBraceChar()) { #region 配对字符解析 ReservedWords.MoveToCharBrace(chr, ReservedWords.GetBraceChar(chr), ref cursor, ref ExpPaper); if (chr == '(') { //Function strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1); //OleDbHelper.AppendToFile("~/debug.txt", "\n 函数体:" + strExpression); if (strTemp.Length == 0) strTemp = null; expList.Add(new InnerExpression(strTemp, strExpression, Res)); } else if (chr == '?') { strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim(); #region 跳出双引号里面的 : 操作符号 if (strExpression.IndexOf('"') != -1 && strExpression[0] != '"') { ReservedWords.MoveToCharBrace('"', '"', ref cursor, ref ExpPaper); ReservedWords.MoveToCharBrace(ExpPaper[cursor], ':', ref cursor, ref ExpPaper); strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim(); } #endregion #region 跳出单引号里面的 : 操作符号 if (strExpression.IndexOf('\'') != -1 && strExpression[0] != '\'') { ReservedWords.MoveToCharBrace('\'', '\'', ref cursor, ref ExpPaper); ReservedWords.MoveToCharBrace(ExpPaper[cursor], ':', ref cursor, ref ExpPaper); strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim(); } #endregion if (strTemp.Length > 0) { expList.Add(new InnerExpression(strTemp)); } expList.Add(new InnerExpression("?")); expList.Add(new InnerExpression(strExpression)); expList.Add(new InnerExpression(":")); //Util.Debug(false, ExpPaper.Substring(cursor)); } else if (chr == '[') { // {#$["首页","新闻","动态","联系"][2]$#} = "动态" #region 数组情况 if (cursor < lenT - 1) { char aIdx = ExpPaper[cursor + 1]; while (aIdx == '[') { cursor++; ReservedWords.MoveToCharBrace(aIdx, ReservedWords.GetBraceChar(aIdx), ref cursor, ref ExpPaper); if (cursor < (lenT - 1)) { aIdx = ExpPaper[cursor + 1]; } else { break; } } strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1); expList.Add(new InnerExpression(strExpression, ',', Res)); } else { #region 获取数组下标操作TODO strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1); expList.Add(new InnerExpression(strExpression, Res)); #endregion } #endregion } else if (chr == '$') { #region 内置系统标签 strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1); SystemTag sysTag = new SystemTag(string.Concat("{#", strExpression, "#}")); sysTag.SetResourceDependency(Res); //OleDbHelper.AppendToFile("~/debug.log", System.Environment.NewLine + string.Concat("{#", strExpression, "#}", "\n", sysTag.ToString())); expList.Add(new InnerExpression(sysTag.ToString())); #endregion } else if (chr == '"' || chr == '\'') { strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1); //Util.Debug(false, "Find String:" + strExpression); expList.Add(new InnerExpression(strExpression)); } #endregion iflag = cursor + 1; } else if (words.IsOperator()) { strExpression = strTemp; if (strExpression.Length > 0) { InnerExpression exp = new InnerExpression(strExpression); expList.Add(exp); } #region 处理操作符号 ParseOperator: char chrNext = ExpPaper[cursor + 1]; if ((chr == '+' || chr == '-') && char.IsNumber(chrNext)) { #region 正负号处理 ++cursor; if (cursor < lenT) { ReservedWords.MoveToCharInRange(ref cursor, ref ExpPaper, ' ', '*', '/', '%', '+', '-', '>', '<', '=', '!', '&', '^', '|'); expList.Add(new InnerExpression(ExpPaper.Substring(iflag, cursor - iflag))); #region 如遇操作符 if (cursor < lenT && ExpPaper[cursor] != ' ') { iflag = cursor; chr = ExpPaper[cursor]; //Util.Debug(false, "new char: = [" + chr.ToString() + "]"); goto ParseOperator; } #endregion } #endregion } else { // *= += -= ++ -- <> if (ReservedWords.IsCharInRange(chrNext, '=', '+', '-', '>')) { expList.Add(new InnerExpression(chr.ToString() + chrNext.ToString())); ++cursor; } else { expList.Add(new InnerExpression(chr.ToString())); } } #endregion iflag = cursor + 1; } else { if (strTemp.Length > 0) { expList.Add(new InnerExpression(strTemp)); } //Util.Debug(false, "11 - [" + strTemp.Trim() + "]" + "chr=[" + chr.ToString() + "]"); } } ++cursor; #endregion } if (iflag < cursor) { expList.Add(new InnerExpression(ExpPaper.Substring(iflag, cursor - iflag).Trim())); } //#region 解析结果查看 //foreach (InnerExpression ext in expList) //{ // //Util.Debug(false, string.Concat("Exp定义:", ext.TagDefinition)); // OleDbHelper.AppendToFile("~/debug.log", System.Environment.NewLine + string.Concat("Exp定义:", ext.TagDefinition)); //} //#endregion return expList; }
/// <summary> /// 获取改参数的真实值 /// </summary> public object GetValue() { //Util.Debug(false, "Get Param Value:[" + this.ParamDefine + "] Is Fun:" + IsFunction.ToString()); if (this.ParamDefine == null) return null; if (IsExpression == true) { InnerExpression exp = new InnerExpression(this.ParamDefine, GetResourceDependency()); return exp.GetValue(); } else if (IsFunction == true) { int idx = this.ParamDefine.IndexOf('('); string funName = (idx == 0) ? null : this.ParamDefine.Substring(0, idx); //Util.Debug(false, this.ParamDefine.Substring(idx + 1).TrimEnd(')')); InnerFunction fun = new InnerFunction(funName, this.ParamDefine.Substring(idx+1).TrimEnd(')'), this.GetResourceDependency()); return fun.Execute(); } else { #region 字符型 if (this.ParamDefine.StartsWith("\"") && this.ParamDefine.EndsWith("\"")) { return this.ParamDefine.Trim('"'); } else if (this.ParamDefine.StartsWith("'") && this.ParamDefine.EndsWith("'")) { return this.ParamDefine.Trim('\''); } #endregion if (this.ParamDefine.StartsWith("%") && this.ParamDefine.EndsWith("%")) { #region 已定义资源数据 string define = string.Concat("{#", this.ParamDefine, "#}"); if (this.Res != null && Res.IsDefined(define)) { return Res.GetDefinition(define); } else { return ""; } #endregion } else if (this.ParamDefine.StartsWith("$") && this.ParamDefine.EndsWith("$")) { #region 系统标签 SystemTag sysTag = new SystemTag(string.Concat("{#", this.ParamDefine, "#}")); sysTag.SetResourceDependency(GetResourceDependency()); return sysTag.ToString(); #endregion } else { #region 布尔及数字型 if (string.Compare(this.ParamDefine, "true", true) == 0) { return true; } else if (string.Compare(this.ParamDefine, "false", true) == 0) { return false; } else { if (Regex.IsMatch(this.ParamDefine, @"^[\d,]+$", RegexOptions.Compiled)) { try { double dec = double.Parse(this.ParamDefine); return dec; } catch (Exception) { return 0; } } else { return this.ParamDefine; } } #endregion } } }