public TemplateFile(string filePath, TemplateFile baseFile) { FilePath = filePath; string virtualPath = filePath.Substring(Globals.ApplicationPath.Length).Replace('\\', '/'); if (StringUtil.StartsWith(virtualPath, '/')) virtualPath = "~" + virtualPath; else virtualPath = "~/" + virtualPath; VirtualPath = virtualPath; m_BaseFile = baseFile; }
private TemplateElement(string sourceTemplate, TemplateFile templateFile) { this.Document = this; this.TemplateFile = templateFile; Type = TemplateElementTypes.Document; Index = 0; Length = sourceTemplate.Length; SourceTemplate = sourceTemplate; if (sourceTemplate == string.Empty || Length == 0) return; ParseTemplateTag(Index, Length); }
public static TemplateElement CreateDocument(string template, TemplateFile templateFile) { TemplateElement doc = new TemplateElement(template, templateFile); return doc; }
public static TemplateElement CreateDocument(string template, TemplateFile templateFile) { TemplateElement doc = new TemplateElement(template, templateFile); return(doc); }
/// <summary> /// 导入指定目录下的模板文件为默认模板,并指定哪个目录为他的扩展模板(可以覆盖掉默认模板同名文件的内容) /// </summary> /// <param name="templateFiles"></param> /// <param name="watchDirectories"></param> /// <param name="directoryName"></param> /// <param name="skinDirectory"></param> private static void ImportTemplateFiles(Dictionary<string, TemplateFile> templateFiles, List<string> watchDirectories, string directoryName, string skinDirectory) { string path = IOUtil.JoinPath(Globals.ApplicationPath, directoryName); string[] files = Directory.GetFiles(path, "*.as?x", SearchOption.AllDirectories); string[] watcherPaths; //如果不支持模板,或者支持模板但默认的模板就在所有模板目录内,则跳过对默认模板目录的单独监视 if (string.IsNullOrEmpty(skinDirectory) || (string.IsNullOrEmpty(skinDirectory) == false && directoryName.StartsWith(skinDirectory, StringComparison.OrdinalIgnoreCase) == false) ) { watcherPaths = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); watchDirectories.Add(path); foreach (string p in watcherPaths) { if (StringUtil.EndsWithIgnoreCase(p, "\\.svn") || StringUtil.ContainsIgnoreCase(p, "\\.svn\\")) continue; watchDirectories.Add(p); } } string[] skins = null; //支持模板 if (string.IsNullOrEmpty(skinDirectory) == false) { skinDirectory = IOUtil.JoinPath(Globals.ApplicationPath, skinDirectory); if (Directory.Exists(skinDirectory)) { skins = Directory.GetDirectories(skinDirectory); watcherPaths = Directory.GetDirectories(skinDirectory, "*", SearchOption.AllDirectories); watchDirectories.Add(skinDirectory); foreach (string p in watcherPaths) { if (StringUtil.EndsWithIgnoreCase(p, "\\.svn") || StringUtil.ContainsIgnoreCase(p, "\\.svn\\")) continue; watchDirectories.Add(p); } } else skins = new string[0]; } foreach (string file in files) { if (StringUtil.ContainsIgnoreCase(file, "\\.svn\\")) continue; TemplateFile templateFile = new TemplateFile(file); templateFiles.Add(templateFile.VirtualPath, templateFile); if (skinDirectory != null) { foreach (string skin in skins) { if (StringUtil.EqualsIgnoreCase(MaxLabs.bbsMax.Consts.DefaultSkinID, Path.GetFileName(skin))) continue; string partFileName = file.Substring(path.Length); string skinFileName = IOUtil.JoinPath(skin, partFileName); TemplateFile skinFile = new TemplateFile(skinFileName, templateFile); templateFile.AddSkin(Path.GetFileName(skin), skinFile); templateFiles.Add(skinFile.VirtualPath, skinFile); } } } if (skinDirectory != null) { foreach (string skin in skins) { if (StringUtil.EqualsIgnoreCase(MaxLabs.bbsMax.Consts.DefaultSkinID, Path.GetFileName(skin))) continue; string[] skinFiles = Directory.GetFiles(skin, "*.as?x", SearchOption.AllDirectories); foreach (string file in skinFiles) { if (StringUtil.ContainsIgnoreCase(file, "\\.svn\\")) continue; TemplateFile skinFile = new TemplateFile(file); if (templateFiles.ContainsKey(skinFile.VirtualPath) == false) templateFiles.Add(skinFile.VirtualPath, skinFile); } } } }
private string OnMatchPager(Match match, string skinID) { if (match.Groups["param"].Success == false) { return(match.Value); } string param = match.Groups["param"].Value; string name = null; string skin = null; string ajaxpanelid = null; string ajaxloader = null; string buttonCount = null; string totalRecords = null; string pageSize = null; string pageNumber = null; string urlFormat = null; foreach (Match paramMatch in regex_param.Matches(param)) { string paramName = paramMatch.Groups[GN_PARAM_NAME].Value; string paramValue = paramMatch.Groups[GN_PARAM_VALUE].Value; switch (paramName.ToLower()) { case "id": case "name": name = paramValue.ToLower(); break; case "skin": skin = paramValue.ToLower(); break; case "ajaxpanelid": ajaxpanelid = paramValue.ToLower(); break; case "ajaxloader": ajaxloader = paramValue.ToLower(); break; case "buttoncount": buttonCount = paramValue; break; case "count": case "totalrecords": totalRecords = paramValue; break; case "pagesize": pageSize = paramValue; break; case "page": case "pagenumber": pageNumber = paramValue; break; case "urlformat": urlFormat = paramValue.Replace("\"", "\\\""); break; default: break; } } if (string.IsNullOrEmpty(name)) { name = string.Concat("__max_", ajaxLoaderIndex.ToString()); ajaxLoaderIndex++; } if (skin == null) { skin = string.Empty; } //if (string.IsNullOrEmpty(ajaxpanelid)) // ajaxpanelid = string.Empty; if (string.IsNullOrEmpty(ajaxloader)) { ajaxloader = string.Concat("__max_", ajaxLoaderIndex.ToString()); ajaxLoaderIndex++; } skin = GetIncludeFileVirtualPath(skin); TemplateFile skinFile = null; if (TemplateManager.GetTemplateFiles().TryGetValue(skin, out skinFile)) { StringBuilder builder = new StringBuilder(); builder.Append("{=$__SetCurrentPager("); AppendPagerStringParam(builder, name, false); AppendPagerStringParam(builder, ajaxpanelid, false); AppendPagerStringParam(builder, ajaxloader, false); AppendPagerNumberParam(builder, pageSize, false); AppendPagerNumberParam(builder, pageNumber, false); AppendPagerNumberParam(builder, totalRecords, false); AppendPagerNumberParam(builder, buttonCount, false); AppendPagerStringParam(builder, urlFormat, true); //.Append(name).Append("\", ") //.Append("\"").Append(ajaxpanelid).Append("\", ") //.Append("\"").Append(ajaxloader).Append("\", ") //.Append(pageSize).Append(", ") //.Append(pageNumber).Append(", ") //.Append(totalRecords).Append(", ") //.Append(buttonCount).Append(", ") //.Append(urlFormat.Replace("\"", "\\\"")).Append(")"); builder.Append(")}"); builder.Append(skinFile.GetFullTemplate(skinID)); return(builder.ToString()); } return("找不到所需的包含文件"); }
public string GetFullTemplate(string skinID) { m_IncludedSkinFile = false; m_IncludedFiles = new List <TemplateFile>(); string path = this.FilePath; if (m_BaseFile != null) { if (File.Exists(path) == false) { path = m_BaseFile.FilePath; } else { m_IncludedSkinFile = true; } } string content = File.ReadAllText(path, Encoding.Default); MatchEvaluator callback = delegate(Match match) { string includeVirtualPath = match.Groups[1].Value; includeVirtualPath = GetIncludeFileVirtualPath(includeVirtualPath); TemplateFile includeFile = null; if (TemplateManager.GetTemplateFiles().TryGetValue(includeVirtualPath, out includeFile)) { if (skinID != MaxLabs.bbsMax.Consts.DefaultSkinID) { includeFile = includeFile.GetSkin(skinID); } string includePath = IOUtil.MapPath(includeVirtualPath); //includeFile.ResetWatcher(); m_IncludedFiles.Add(includeFile); string result = includeFile.GetFullTemplate(skinID); if (m_IncludedSkinFile == false && includeFile.m_IncludedSkinFile) { m_IncludedSkinFile = true; } //开始处理包含文件的参数 string includeParams = match.Groups["param"].Value; MatchCollection matchs = Pool <TemplateAttributeListRegex> .Instance.Matches(includeParams); foreach (Match param in matchs) { //应该计算索引位置,否则会存在误判和错误替换 result = result.Replace("$" + param.Groups["name"].Value, param.Groups["value"].Value); } return(result); } else { return(string.Empty); } }; //content = regex_pager.Replace(content, delegate(Match match) { // return OnMatchPager(match, skinID); //}); content = regex_pager4.Replace(content, delegate(Match match) { return(OnMatchPager(match, skinID)); }); content = s_MatchNotes.Replace(content, string.Empty); content = s_MatchInclude.Replace(content, callback); content = s_MatchPreInclude.Replace(content, callback); content = regex_fast_css.Replace(content, new MatchEvaluator(ReplaceFast)); content = regex_fast_js.Replace(content, new MatchEvaluator(ReplaceFast)); content = content.Trim(); return(content); //StringBuilder sb = new StringBuilder(); //string vpath = path.Substring(Globals.GetPath(SystemDirecotry.Root).Length); //sb.AppendLine("<!--begin ").Append(vpath).AppendLine("-->"); //sb.Append(content.Trim()); //sb.AppendLine("<!--end ").Append(vpath).AppendLine("-->"); //return sb.ToString(); }
/// <summary> /// 解析指定路径下的模板文件,并返回模板解析结果的路径,如果所给的路径不是模板文件路径,则返回原路径 /// </summary> /// <param name="virtualPath">模板文件的虚拟路径,支持.aspx和.ascx文件</param> /// <returns></returns> public static string ParseTemplate(string path) { string virtualPath = null; string queryString = null; int queryStart = path.IndexOf('?'); if (queryStart >= 0) { virtualPath = path.Substring(0, queryStart); queryString = path.Substring(queryStart); } else { virtualPath = path; } //if (virtualPath.StartsWith(SkinedPath, StringComparison.OrdinalIgnoreCase)) //{ // string skinID = User.Current.SkinID; // virtualPath = "~/max-templates/default/" + skinID + virtualPath.Substring(1); //} TemplateFile templateFile = GetTemplateFile(virtualPath); if (templateFile == null) { return(path);//virtualPath + queryString; } Skin skin = s_IsPreParsing == false ? Context.Current.Skin : s_PreParseSkin; bool useSkin; string skinID; if (skin == null) { useSkin = false; skinID = MaxLabs.bbsMax.Consts.DefaultSkinID; } else { useSkin = skin.SkinID != MaxLabs.bbsMax.Consts.DefaultSkinID; skinID = skin.SkinID; } if (useSkin) { templateFile = templateFile.GetSkin(skin.SkinID); } if (templateFile.IsParsed == false) { lock (templateFile) { if (templateFile.IsParsed == false) { using (TemplateParser parser = new TemplateParser()) { //string forumPath = "~/max-templates/" + skinID + "/forums/"; ////UrlFormat urlFormat = s_IsPreParsing ? s_PreParseUrlFormat : AllSettings.Current.FriendlyUrlSettings.UrlFormat; //if (templateFile.VirtualPath.StartsWith(forumPath, StringComparison.OrdinalIgnoreCase)) //{ // parser.GenerateV30AspxCode(skinID, templateFile, Config.Current.TemplateImports); //} //else //{ parser.GenerateAspxCode(skinID, templateFile, Config.Current.TemplateImports); //} templateFile.IsParsed = true; } } } } return(templateFile.ParsedFileVirtualPath + queryString); }
public void AddSkin(string name, TemplateFile skin) { if (m_Skins == null) m_Skins = CollectionsUtil.CreateCaseInsensitiveHashtable(); m_Skins.Add(name, skin); }
/// <summary> /// 导入指定目录下的模板文件为默认模板,并指定哪个目录为他的扩展模板(可以覆盖掉默认模板同名文件的内容) /// </summary> /// <param name="templateFiles"></param> /// <param name="watchDirectories"></param> /// <param name="directoryName"></param> /// <param name="skinDirectory"></param> private static void ImportTemplateFiles(Dictionary <string, TemplateFile> templateFiles, List <string> watchDirectories, string directoryName, string skinDirectory) { string path = IOUtil.JoinPath(Globals.ApplicationPath, directoryName); string[] files = Directory.GetFiles(path, "*.as?x", SearchOption.AllDirectories); string[] watcherPaths; //如果不支持模板,或者支持模板但默认的模板就在所有模板目录内,则跳过对默认模板目录的单独监视 if (string.IsNullOrEmpty(skinDirectory) || (string.IsNullOrEmpty(skinDirectory) == false && directoryName.StartsWith(skinDirectory, StringComparison.OrdinalIgnoreCase) == false) ) { watcherPaths = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); watchDirectories.Add(path); foreach (string p in watcherPaths) { if (StringUtil.EndsWithIgnoreCase(p, "\\.svn") || StringUtil.ContainsIgnoreCase(p, "\\.svn\\")) { continue; } watchDirectories.Add(p); } } string[] skins = null; //支持模板 if (string.IsNullOrEmpty(skinDirectory) == false) { skinDirectory = IOUtil.JoinPath(Globals.ApplicationPath, skinDirectory); if (Directory.Exists(skinDirectory)) { skins = Directory.GetDirectories(skinDirectory); watcherPaths = Directory.GetDirectories(skinDirectory, "*", SearchOption.AllDirectories); watchDirectories.Add(skinDirectory); foreach (string p in watcherPaths) { if (StringUtil.EndsWithIgnoreCase(p, "\\.svn") || StringUtil.ContainsIgnoreCase(p, "\\.svn\\")) { continue; } watchDirectories.Add(p); } } else { skins = new string[0]; } } foreach (string file in files) { if (StringUtil.ContainsIgnoreCase(file, "\\.svn\\")) { continue; } TemplateFile templateFile = new TemplateFile(file); templateFiles.Add(templateFile.VirtualPath, templateFile); if (skinDirectory != null) { foreach (string skin in skins) { if (StringUtil.EqualsIgnoreCase(MaxLabs.bbsMax.Consts.DefaultSkinID, Path.GetFileName(skin))) { continue; } string partFileName = file.Substring(path.Length); string skinFileName = IOUtil.JoinPath(skin, partFileName); TemplateFile skinFile = new TemplateFile(skinFileName, templateFile); templateFile.AddSkin(Path.GetFileName(skin), skinFile); templateFiles.Add(skinFile.VirtualPath, skinFile); } } } if (skinDirectory != null) { foreach (string skin in skins) { if (StringUtil.EqualsIgnoreCase(MaxLabs.bbsMax.Consts.DefaultSkinID, Path.GetFileName(skin))) { continue; } string[] skinFiles = Directory.GetFiles(skin, "*.as?x", SearchOption.AllDirectories); foreach (string file in skinFiles) { if (StringUtil.ContainsIgnoreCase(file, "\\.svn\\")) { continue; } TemplateFile skinFile = new TemplateFile(file); if (templateFiles.ContainsKey(skinFile.VirtualPath) == false) { templateFiles.Add(skinFile.VirtualPath, skinFile); } } } } }
private void GenerateAspxCodeHead2(TemplateFile templateFile, string[] templateImports, string skinID) { foreach (string nameSpace in templateImports) { m_CodeHead += "<%@ Import Namespace=\"" + nameSpace + "\" %>"; } m_CodeHead += "<%\r\n DirectoryVirtualPath = \"" + templateFile.VirtualDirectoryPath + "\";\r\nthis.SubmitFillUsers(); %>" + Environment.NewLine; m_CodeHead += "<script runat=\"server\">" + Environment.NewLine; m_CodeHead += "protected override void OnLoad(System.EventArgs e)" + Environment.NewLine; m_CodeHead += "{" + Environment.NewLine; m_CodeHead += "this.DirectoryVirtualPath = \"" + templateFile.VirtualDirectoryPath + "\";" + Environment.NewLine; m_CodeHead += "base.OnLoad(e);" + Environment.NewLine; m_CodeHead += "}"; if (m_DeclaredVariables.Count > 0) { foreach (KeyValuePair<Type, string> variable in m_DeclaredVariables) { string typeName = ReflectionUtil.GetCSharpTypeName(variable.Key); m_CodeHead += Environment.NewLine + typeName + " " + variable.Value + " = new " + typeName + "();"; } } m_CodeHead += Environment.NewLine + "</script>"; }
private void GenerateAspxCodeHead1(TemplateFile templateFile) { m_CodeHead += "<%@ "; if (templateFile.IsControl) m_CodeHead += "Control Language=\"C#\" "; else m_CodeHead += "Page Language=\"C#\" "; string inherits = null; if(m_Document.ChildNodes.Count > 0) { TemplateElement firstNode = m_Document.ChildNodes[0]; if(firstNode.Type == TemplateElementTypes.Tag) { string tagName = (string)firstNode.Items[TemplateElement.KEY_NAME]; if (string.Compare(tagName, "page", true) == 0) { foreach (TemplateElement element in firstNode.ChildNodes[0].ChildNodes) { string name = (string)element.Items[TemplateElement.KEY_NAME]; if (string.Compare(name, "inherits", true) == 0) inherits = element.SourceTemplate.Substring(element.Index, element.Length); } } } } if (inherits == null) { m_CodeHead += "Inherits=\"" + templateFile.CodeFileInherits + "\" %>"; ParseCodeFile(templateFile.CodeFileInherits); } else { m_CodeHead += "Inherits=\"" + inherits + "\" %>"; ParseCodeFile(inherits); } }
public void GenerateAspxCode(string skinID, TemplateFile templateFile, string[] templateImports) { string template = templateFile.GetFullTemplate(skinID); string checkString = ReadCheckString(templateFile); string newCheckString = GetNewCheckString(template); if (checkString != newCheckString) { #region 需要重新解析模板 m_Document = TemplateElement.CreateDocument(template, templateFile); m_CodeHead = new StringBuffer(); m_CodeBody = new StringBuffer(template.Length); GenerateAspxCodeHead1(templateFile); GenerateAspxCode(m_Document, new ScopeData(null)); GenerateAspxCodeHead2(templateFile, templateImports, skinID); string aspxCode = string.Concat("<%--", newCheckString, "--%>\r\n", m_CodeHead.ToString(), m_CodeBody.ToString()); try { using (StreamWriter writer = new StreamWriter(templateFile.ParsedFilePath, false, Encoding.UTF8)) { writer.Write(aspxCode); } } catch (Exception ex) { LogHelper.CreateErrorLog(ex, templateFile.FileName + "写入文件时出错"); } LogHelper.CreateDebugLog(templateFile.FileName + "发生了重新解析,并写入磁盘"); #endregion } TryBuildTempWebConfig(); }
private string ReadCheckString(TemplateFile templateFile) { string checkString = null; if (File.Exists(templateFile.ParsedFilePath) == false) return null; using (FileStream stream = File.Open(templateFile.ParsedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (StreamReader reader = new StreamReader(stream, Encoding.Default, true)) { checkString = reader.ReadLine(); } } if (string.IsNullOrEmpty(checkString) == false && StringUtil.StartsWith(checkString, "<%--") && StringUtil.EndsWith(checkString, "--%>")) { return checkString.Substring(4, checkString.Length - 8); } return null; }