⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlsdata.cs

📁 ASP.NET简洁论坛源代码 这是一个简单的论坛
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.IO;
using System.Text;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum
{
    public class UrlsData
    {
        #region private members

        private static string cacheKey = "UrlsData";
        private static UrlsData instance = null;
        private NameValueCollection _paths = null;
        private NameValueCollection _reversePaths = null;
        private ArrayList _tabUrls = null;
        private string _locationFilter = null;
        private LocationSet _locationSet = null;

        #endregion

        #region Instance

        public UrlsData(string siteUrlsXmlFile)
        {
            _paths = new NameValueCollection();
            _reversePaths = new NameValueCollection();
            _tabUrls = new ArrayList();

            Initialize(siteUrlsXmlFile);
        }
        public static UrlsData Instance()
        {
            if (Caches.Get(cacheKey) == null)
            {
                string file = WebContext.Current.PhysicalPath(Configuration.Instance.UrlsFile);
                instance = new UrlsData(file);
                Caches.Max(cacheKey, instance, new CacheDependency(file));
            }
            return instance;
        }

        #endregion

        #region Public Properties

        public LocationSet Locations
        {
            get { return _locationSet; }
        }
        public string LocationFilter
        {
            get
            {
                return _locationFilter;
            }
        }
        public NameValueCollection Paths
        {
            get { return this._paths; }
        }
        public NameValueCollection ReversePaths
        {
            get { return this._reversePaths; }
        }
        public ArrayList TabUrls
        {
            get
            {
                return _tabUrls;
            }
        }

        #endregion

        #region Private Methods

        private void Initialize(string siteUrlsXmlFile)
        {
            string globalPath = Globals.ApplicationPath;
            if (globalPath != null)
            {
                globalPath = globalPath.Trim();
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(siteUrlsXmlFile);

            XmlNode basePaths = doc.SelectSingleNode("SiteUrls/locations");
            _locationSet = CreateLocationSet(basePaths, globalPath);
            _locationFilter = _locationSet.Filter;

            CreateUrls(doc.SelectSingleNode("SiteUrls/urls"), CreateTransformers(doc.SelectSingleNode("SiteUrls/transformers")));
            CreateNavigation(doc.SelectSingleNode("SiteUrls/navigations"));   

        }
        private static ListDictionary CreateTransformers(XmlNode transformers)
        {
            ListDictionary transforms = new ListDictionary();
            foreach (XmlNode n in transformers.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment)
                {
                    string k = n.Attributes["key"].Value;
                    string v = n.Attributes["value"].Value;

                    if (!string.IsNullOrEmpty(k))
                    {
                        transforms[k] = v;
                    }
                }
            }

            return transforms;
        }
        protected virtual void CreateNavigation(XmlNode nav)
        {
            if (nav != null)
            {
                foreach (XmlNode node in nav.ChildNodes)
                {
                    if (node.NodeType != XmlNodeType.Comment)
                    {
                        XmlAttribute name = node.Attributes["name"];
                        XmlAttribute resourceUrl = node.Attributes["resourceUrl"];
                        XmlAttribute resourceName = node.Attributes["resourceName"];
                        XmlAttribute navigateUrl = node.Attributes["navigateUrl"];
                        XmlAttribute text = node.Attributes["text"];
                        XmlAttribute targetAttr = node.Attributes["target"];
                        XmlAttribute classAttr = node.Attributes["class"];
                        XmlAttribute roles = node.Attributes["roles"];

                        string rolesValue;
                        if (roles == null)
                        {
                            rolesValue = "所有人";  //TODO
                        }
                        else
                        {
                            rolesValue = roles.Value;
                        }

                        string urlValue;
                        if (resourceUrl == null)
                        {
                            urlValue = navigateUrl.Value;
                        }
                        else
                        {
                            urlValue = FormatUrl(resourceUrl.Value);
                        }

                        string target = string.Empty;
                        if (targetAttr != null)
                        {
                            target = targetAttr.Value;
                        }

                        string _class = string.Empty;
                        if (classAttr != null)
                        {
                            _class = classAttr.Value;
                        }

                        NavLink link = new NavLink(name.Value, (resourceName == null) ? null : resourceName.Value, (text == null) ? null : text.Value, urlValue, target, _class, rolesValue, null);
                        _tabUrls.Add(link);
                    }
                }
            }
        }
        private void CreateUrls(XmlNode urls, ListDictionary transforms)
        {
            foreach (XmlNode n in urls.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment)
                {
                    string name = n.Attributes["name"].Value;

                    XmlAttribute direct = n.Attributes["navigateUrl"];
                    if (direct != null)
                    {
                        _paths.Add(name, direct.Value);
                    }
                    else
                    {
                        string path = n.Attributes["path"].Value;

                        foreach (string key in transforms.Keys)
                        {
                            path = path.Replace(key, transforms[key].ToString());
                        }

                        string location = null;
                        XmlAttribute l = n.Attributes["location"];
                        if (l != null)
                            location = l.Value;

                        Location loc = _locationSet.FindLocationByName(location);

                        _paths.Add(name, loc.Path + path);

                        // Store the full path to avoid having key duplicates
                        //
                        string keyPath = loc.Path + path;
                        if (Globals.ApplicationPath.Length > 0)
                            keyPath = keyPath.Replace(Globals.ApplicationPath, "").ToLower();
                        _reversePaths.Add(keyPath, name);

                        XmlAttribute realpath = n.Attributes["realpath"];
                        XmlAttribute pattern = n.Attributes["pattern"];

                        //Store full paths like regular urls. 
                        if (realpath != null && pattern != null)
                        {
                            string p = (!pattern.Value.StartsWith("/") ? loc.Path : String.Empty) + pattern.Value;
                            string rp = (!realpath.Value.StartsWith("/") ? loc.PhysicalPath : String.Empty) + realpath.Value;

                            foreach (string key in transforms.Keys)
                            {
                                p = p.Replace(key, transforms[key].ToString());
                                rp = rp.Replace(key, transforms[key].ToString());
                            }

                            loc.Add(new ReWrittenUrl(location + "." + name, p, rp));
                        }
                    }

                }

            }
        }
        private LocationSet CreateLocationSet(XmlNode basePaths, string globalPath)
        {
            LocationSet ls = new LocationSet();
            foreach (XmlNode n in basePaths.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment)
                {
                    XmlAttribute name = n.Attributes["name"];
                    XmlAttribute path = n.Attributes["path"];
                    XmlAttribute physicalPath = n.Attributes["physicalPath"];
                    XmlAttribute exclude = n.Attributes["exclude"];
                    XmlAttribute ltype = n.Attributes["type"];

                    if (name != null && path != null)
                    {
                        string pp = null;
                        if (physicalPath != null)
                        {
                            pp = globalPath + physicalPath.Value;
                        }

                        bool isExeclude = (exclude != null && bool.Parse(exclude.Value));
                        Location l = null;
                        string vp = globalPath + path.Value;
                        if (ltype == null)
                        {
                            l = new Location(vp, pp, isExeclude);
                        }
                        else
                        {
                            Type t = Type.GetType(ltype.Value);
                            if (t == null)
                            {
                                //CSException csEx = new CSException(CSExceptionType.UnknownError, "Type for custom location could not be loaded " + ltype.Value);
                                //csEx.Log();
                                //throw csEx;
                            }

                            l = Activator.CreateInstance(t, new object[] { vp, pp, isExeclude }) as Location;
                            if (l == null)
                            {
                                //CSException csEx = new CSException(CSExceptionType.UnknownError, "Type for custom location could not be loaded as an instance of Location" + ltype.Value);
                                //csEx.Log();
                                //throw csEx;
                            }
                        }
                        ls.Add(name.Value, l);
                    }
                }
            }

            return ls;
        }

        #endregion //Private Methods

        public string FormatUrl(string name)
        {
            return FormatUrl(name, null);
        }
        public virtual string FormatUrl(string name, params object[] parameters)
        {
            if (parameters == null)
            {
                return this.Paths[name];
            }
            else
            {
                try
                {
                    return string.Format(Paths[name], parameters);
                }
                catch
                { }
                return string.Empty;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -