using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Web;
using System.Web.Caching;
namespace Luna.Common
{
public class SiteConfig
{
public static XmlDocument GetResourceXml()
{
XmlDocument doc = new XmlDocument();
ContentCache contentCache = ContentCache.Instantiate();
if (contentCache["ResourceMulit"] != null)
{
doc = (XmlDocument)contentCache["ResourceMulit"];
}
else
{
string file = HttpContext.Current.Server.MapPath("/App_Data/ResourceMulit.xml");
doc.Load(file);
CacheDependency dep = new CacheDependency(file, DateTime.Now);
contentCache.Insert("ResourceMulit", doc, dep);
}
return doc;
}
public static void RegsiterResource()
{
XmlDocument doc = SiteConfig.GetResourceXml();
XmlNode firstNode = doc.DocumentElement.FirstChild;
XmlNodeList RequiredScripts = firstNode.FirstChild.ChildNodes;
XmlNodeList RequiredStyles = firstNode.ChildNodes[1].ChildNodes;
Page currentPage = (Page)HttpContext.Current.Handler;
RegisterResource(RequiredScripts, RequiredStyles);
XmlNode secondNode = doc.DocumentElement.ChildNodes[1];
foreach (XmlNode item in secondNode.ChildNodes)
{
string pageSrc = item.Attributes["Src"].Value.ToLower();
if (currentPage.ResolveUrl(pageSrc) == HttpContext.Current.Request.Path.ToLower())
{
RegisterResource(item.FirstChild.ChildNodes, item.ChildNodes[1].ChildNodes);
}
}
}
public static void RegsiterResourceKey(XmlNode node, List<string> resourceNode, List<string> removeNode)
{
if (node != null)
{
XmlNode scriptsNodes = node.SelectSingleNode("descendant::Scripts");
foreach (XmlNode scriptNode in scriptsNodes.ChildNodes)
{
if (scriptNode.Attributes["Removed"] == null)
{
resourceNode.Add(scriptNode.Attributes["Name"].Value);
}
else
{
removeNode.Add(scriptNode.Attributes["Name"].Value);
}
}
}
}
public static void RegsiterCommon()
{
XmlDocument doc = SiteConfig.GetResourceXml();
//XmlNodeList RequiredStyles = firstNode.ChildNodes[1].ChildNodes;
List<Plugin> PluginList = RegsiterCommonResource(doc);
//RegisterResource(RequiredScripts, RequiredStyles);
//Pages
XmlNode currentPageNode = GetCurrentPageNode(doc);
XmlNode RequireNode = GetRequireNode(doc);
Dictionary<string, string> scriptList = new Dictionary<string, string>();
Dictionary<string, string> styleList = new Dictionary<string, string>();
List<string> removeNode = new List<string>();
List<string> resourceNode = new List<string>();
if (currentPageNode != null)
{
XmlAttribute isAuthenticated = currentPageNode.Attributes["IsAuthenticated"];
if (isAuthenticated != null)
{
if (Convert.ToBoolean(isAuthenticated.Value) && HttpContext.Current.Request.IsAuthenticated)
{
RegsiterResourceKey(currentPageNode, resourceNode, removeNode);
}
}
else
{
RegsiterResourceKey(currentPageNode, resourceNode, removeNode);
}
}
RegsiterResourceKey(RequireNode, resourceNode, removeNode);
List<string> filterResourceNode= resourceNode.FindAll(delegate(string node)
{
foreach (var item in removeNode)
{
return node != item;
}
return true;
});
foreach (var item in filterResourceNode)
{
Plugin plugin = GetCurrentPlugin(item, PluginList);
RegisterPlugin(scriptList, styleList, plugin);
}
}
public static void RegisterPlugin(Dictionary<string, string> scriptList, Dictionary<string, string> styleList, Plugin plugin)
{
Page currentPage = (Page)HttpContext.Current.Handler;
foreach (Style style in plugin.Styles)
{
if (!styleList.ContainsKey(style.Key))
{
styleList.Add(style.Key, style.Href);
currentPage.AddCss(style.Href);
}
}
foreach (Script script in plugin.Scripts)
{
if (!scriptList.ContainsKey(script.Key))
{
scriptList.Add(script.Key, script.Src);
currentPage.AddScript(script.Src);
}
}
if (!scriptList.ContainsKey(plugin.Name))
{
scriptList.Add(plugin.Name, plugin.Src);
currentPage.AddScript(plugin.Src);
}
}
public static Plugin GetCurrentPlugin(string scriptNode, List<Plugin> PluginList)
{
foreach (Plugin plugin in PluginList)
{
if (scriptNode == plugin.Name)
{
return plugin;
}
}
return PluginList[0];
}
private static XmlNode GetRequireNode(XmlDocument doc)
{
XmlNode node = doc.DocumentElement.SelectSingleNode("descendant::Required");
return node;
}
public static XmlNode GetCurrentPageNode(XmlDocument doc)
{
Page currentPage = (Page)HttpContext.Current.Handler;
XmlNode node = doc.DocumentElement.SelectSingleNode("descendant::Pages");
foreach (XmlNode item in node.ChildNodes)
{
string pageSrc = item.Attributes["Src"].Value.ToLower();
if (currentPage.ResolveUrl(pageSrc) == HttpContext.Current.Request.Path.ToLower())
{
return item;
}
}
return null;
}
public static List<Plugin> RegsiterCommonResource(XmlDocument doc)
{
XmlNode firstNode = doc.DocumentElement.SelectSingleNode("descendant::Common");
XmlNodeList PluginNodeList = firstNode.SelectSingleNode("descendant::Plugins").ChildNodes;
Page currentPage = (Page)HttpContext.Current.Handler;
List<Plugin> plugins = new List<Plugin>();
foreach (XmlNode item in PluginNodeList)
{
Plugin plugin = new Plugin();
plugin.Name = item.Attributes["Name"].Value;
plugin.Src = currentPage.ResolveUrl(item.Attributes["Src"].Value);
XmlNodeList scripts = item.SelectSingleNode("descendant::Scripts").ChildNodes;
foreach (XmlNode script in scripts)
{
string key = script.Attributes["Key"].Value;
string src = currentPage.ResolveUrl(script.Attributes["Src"].Value);
bool removed = false;
if (script.Attributes["Removed"] != null)
{
removed = Convert.ToBoolean(script.Attributes["Removed"].Value);
}
plugin.Scripts.Add(new Script(key, src,false));
}
XmlNode styles = item.SelectSingleNode("descendant::Styles");
if (styles != null)
{
foreach (XmlNode style in styles.ChildNodes)
{
plugin.Styles.Add(new Style(style.Attributes["Key"].Value,currentPage.ResolveUrl(style.Attributes["Href"].Value)));
}
}
plugins.Add(plugin);
}
return plugins;
}
private static void RegisterScript(string aa)
{
//foreach (XmlNode item in scriptsNode)
//{
// string key = item.Attributes["Name"].Value;
// RegisterScript(key);
//}
}
private static void RegisterResource(XmlNodeList scripts, XmlNodeList styles)
{
Page currentPage = (Page)HttpContext.Current.Handler;
foreach (XmlNode item in styles)
{
string href = currentPage.ResolveUrl(item.Attributes["Href"].Value);
LiteralControl control = new LiteralControl(string.Format("\n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", href));
currentPage.Header.Controls.Add(control);
}
foreach (XmlNode item in scripts)
{
string key = item.Attributes["Key"].Value;
string src = currentPage.ResolveUrl(item.Attributes["Src"].Value);
currentPage.AddScript(src);
//currentPage.ClientScript.RegisterClientScriptInclude(currentPage.GetType(), key, src);
}
}
}
}