fixed ParseServerVariables().
[mono.git] / mcs / class / System.Web / System.Web.Configuration / WebControlsSectionHandler.cs
1 //
2 // System.Web.Configuration.WebControlsSectionHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 using System;
11 using System.Configuration;
12 using System.IO;
13 using System.Web;
14 using System.Xml;
15
16 namespace System.Web.Configuration
17 {
18         class WebControlsConfig
19         {
20                 static WebControlsConfig instance;
21                 string scriptsVDir;
22                 string configFilePath;
23                 
24                 public WebControlsConfig (WebControlsConfig parent, object context)
25                 {
26                         configFilePath = context as string;
27                         if (parent == null)
28                                 return;
29                         
30                         scriptsVDir = parent.scriptsVDir;
31                         if (scriptsVDir != null)
32                                 configFilePath = parent.configFilePath;
33                 }
34                 
35                 public void SetClientScriptsLocation (string location, out string error)
36                 {
37                         error = null;
38                         if (location == null || location.Length == 0) {
39                                 error = "empty or null value for clientScriptsLocation";
40                                 return;
41                         }
42
43                         if (location [0] != '/')
44                                 location = "/" + location;
45
46                         string [] splitted = location.Split ('/');
47                         int end = splitted.Length;
48                         for (int i = 0; i < end; i++)
49                                 splitted [i] = HttpUtility.UrlEncode (splitted [i]);
50
51                         scriptsVDir = String.Join ("/", splitted);
52                 }
53
54                 public string ScriptsPhysicalDirectory {
55                         get { return Path.Combine (Path.GetDirectoryName (configFilePath), "web_scripts"); }
56                 }
57
58                 public string ScriptsVirtualDirectory {
59                         get { return scriptsVDir; }
60                         set { scriptsVDir = value; }
61                 }
62
63                 static public WebControlsConfig Instance {
64                         get {
65                                 //TODO: use HttpContext to get the configuration
66                                 if (instance != null)
67                                         return instance;
68
69                                 lock (typeof (WebControlsConfig)) {
70                                         if (instance != null)
71                                                 return instance;
72
73                                         instance = (WebControlsConfig) ConfigurationSettings.GetConfig ("system.web/webControls");
74                                 }
75
76                                 return instance;
77                         }
78                 }
79         }
80         
81         class WebControlsSectionHandler : IConfigurationSectionHandler
82         {
83                 public object Create (object parent, object context, XmlNode section)
84                 {
85                         WebControlsConfig config = new WebControlsConfig (parent as WebControlsConfig, context);
86
87                         if (section.Attributes == null && section.Attributes.Count == 0)
88                                 ThrowException ("Lack of clientScriptsLocation attribute", section);
89
90                         string clientLocation = AttValue ("clientScriptsLocation", section, false);
91                         if (section.Attributes != null && section.Attributes.Count != 0)
92                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
93
94                         string error;
95                         config.SetClientScriptsLocation (clientLocation, out error);
96                         if (error != null)
97                                 HandlersUtil.ThrowException (error, section);
98
99                         return config;
100                 }
101
102                 // To save some typing...
103                 static string AttValue (string name, XmlNode node, bool optional)
104                 {
105                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
106                 }
107
108                 static string AttValue (string name, XmlNode node)
109                 {
110                         return HandlersUtil.ExtractAttributeValue (name, node, true);
111                 }
112
113                 static void ThrowException (string message, XmlNode node)
114                 {
115                         HandlersUtil.ThrowException (message, node);
116                 }
117                 //
118         }
119 }
120