代码改变世界

cs配置文件的获取

2007-05-06 15:19  Clingingboy  阅读(1501)  评论(0编辑  收藏  举报
        类多了,看都看晕了.本着职责明确的思想,在n个类的配合下,我们终于得到了我们想要的结果.有时候还真是烦

我本意是不想看配置文件如何读取的,但我就不知不觉的遇到这个问题了,只有耐着性子看了.在此我们只先需要明白,他该做什么,不该做什么就可以了

CSConfiguration负责读取配置文件

下面是我遇到的经历

当我想看看如何获取SiteUrls.config的数据时,我就是找不到读取这个路径的url,且看SiteUrls的Instance方法

path为载入的SiteUrls.config配置文件

string path = csContext.PhysicalPath(siteUrlProvider.Attributes["path"]);
                    
string overridePath = csContext.PhysicalPath(siteUrlProvider.Attributes["overridePath"]);
                    Type type 
= Type.GetType(siteUrlProvider.Type);

                    XmlDocument doc 
= null;
                    
                    
if(File.Exists(overridePath))
                    
{
                        doc 
= Merger.MergeXmlFiles(path, overridePath);
                    }

                    
else
                    
{
                        doc 
= new XmlDocument();
                        doc.Load(path);
                    }

于是我找到了Provider类


                    CSConfiguration config 
= CSConfiguration.GetConfig();
                    
                    Provider siteUrlProvider 
= config.Providers["SiteUrlsDataProvider"as Provider;

继续看CSConfiguration的Providers,其为一个哈希表,我们还不知道如何获取,CSConfiguration让我们看到了唯一的操作就是GetConfig

public Hashtable Providers get return providers; } }


让我们再看GetConfig方法,下面的代码有点眉目了,我们终于看到了communityserver.config配置文件,好象跟我们想要的SiteUrls没关系,我们只要一个SiteUrls的路径而已.
string path = null;
                
string overridePath = null;
                XmlDocument doc 
= null;
                HttpContext context 
= HttpContext.Current;
                
if (context != null)
                
{
                    path 
= context.Server.MapPath("~/communityserver.config");
                    overridePath 
= context.Server.MapPath("~/communityserver_override.config");
                }

                
else
                
{
                    path 
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "communityserver.config");
                    overridePath 
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "communityserver_override.config");
                }


                
                
if(File.Exists(overridePath))
                
{
                   doc 
= Merger.MergeXmlFiles(path, overridePath);
                }

                
else
                
{
                    doc 
= new XmlDocument();
                    doc.Load(path);    
                }

再看下来,开始真正实例化了

//实例化
                config = new CSConfiguration(doc);

接着我们去看构造函数干了什么

        public CSConfiguration(XmlDocument doc)
        
{
            XmlDoc 
= doc;
            LoadValuesFromConfigurationXml();
        }

再看LoadValuesFromConfigurationXml方法,看来真正做事情的是他

看到这里

foreach (XmlNode child in node.ChildNodes)
            
{

                
if (child.Name == "providers")
                    GetProviders(child, providers);

                
if (child.Name == "appLocation")
                    GetAppLocation(child);

                
if (child.Name == "extensionModules")
                    GetProviders(child, extensions);

                
if (child.Name == "roleConfiguration")
                    GetRoleConfiguration(child);

                
if (child.Name == "filterLanguages")
                    GetFilterLanguages(child);

                
if (child.Name == "editors")
                    GetEditors(child);
            }

打开配置文件看看发现以下节点

                <add 
                    
name = "SiteUrlsDataProvider" 
                    type 
= "CommunityServer.Urls.UrlsData, CommunityServer.Urls" 
                    path 
= "SiteUrls.config" 
            overridePath 
="SiteUrls_override.config"
                
/>

 internal void GetProviders(XmlNode node, Hashtable table) {

            
foreach (XmlNode provider in node.ChildNodes) {

                
switch (provider.Name) {
                    
case "add" :
                        table.Add(provider.Attributes[
"name"].Value, new Provider(provider.Attributes) );
                        
break;

                    
case "remove" :
                        table.Remove(provider.Attributes[
"name"].Value);
                        
break;

                    
case "clear" :
                        table.Clear();
                        
break;

                }


            }


        }

好了,我们应该差不多搞明白了.其他的应该类似.慢慢的记录.