2005-10-29 Atsushi Enomoto <atsushi@ximian.com>
[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 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Configuration;
33 using System.IO;
34 using System.Web;
35 using System.Xml;
36
37 namespace System.Web.Configuration
38 {
39         class WebControlsConfig
40         {
41                 volatile static WebControlsConfig instance;
42                 string scriptsVDir;
43                 string configFilePath;
44                 static readonly object lockobj = new object ();
45                 
46                 public WebControlsConfig (WebControlsConfig parent, object context)
47                 {
48                         configFilePath = context as string;
49                         if (parent == null)
50                                 return;
51                         
52                         scriptsVDir = parent.scriptsVDir;
53                         if (scriptsVDir != null)
54                                 configFilePath = parent.configFilePath;
55                 }
56                 
57                 public void SetClientScriptsLocation (string location, out string error)
58                 {
59                         error = null;
60                         if (location == null || location.Length == 0) {
61                                 error = "empty or null value for clientScriptsLocation";
62                                 return;
63                         }
64
65                         if (location [0] != '/')
66                                 location = "/" + location;
67
68                         string [] splitted = location.Split ('/');
69                         int end = splitted.Length;
70                         for (int i = 0; i < end; i++)
71                                 splitted [i] = HttpUtility.UrlEncode (splitted [i]);
72
73                         scriptsVDir = String.Join ("/", splitted);
74                 }
75
76                 public string ScriptsPhysicalDirectory {
77                         get { return Path.Combine (Path.GetDirectoryName (configFilePath), "web_scripts"); }
78                 }
79
80                 public string ScriptsVirtualDirectory {
81                         get { return scriptsVDir; }
82                         set { scriptsVDir = value; }
83                 }
84
85                 static public WebControlsConfig Instance {
86                         get {
87                                 //TODO: use HttpContext to get the configuration
88                                 if (instance != null)
89                                         return instance;
90
91                                 lock (lockobj) {
92                                         if (instance != null)
93                                                 return instance;
94
95                                         instance = (WebControlsConfig) ConfigurationSettings.GetConfig ("system.web/webControls");
96                                 }
97
98                                 return instance;
99                         }
100                 }
101         }
102         
103         class WebControlsSectionHandler : IConfigurationSectionHandler
104         {
105                 public object Create (object parent, object context, XmlNode section)
106                 {
107                         WebControlsConfig config = new WebControlsConfig (parent as WebControlsConfig, context);
108
109                         if (section.Attributes == null && section.Attributes.Count == 0)
110                                 ThrowException ("Lack of clientScriptsLocation attribute", section);
111
112                         string clientLocation = AttValue ("clientScriptsLocation", section, false);
113                         if (section.Attributes != null && section.Attributes.Count != 0)
114                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
115
116                         string error;
117                         config.SetClientScriptsLocation (clientLocation, out error);
118                         if (error != null)
119                                 HandlersUtil.ThrowException (error, section);
120
121                         return config;
122                 }
123
124                 // To save some typing...
125                 static string AttValue (string name, XmlNode node, bool optional)
126                 {
127                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
128                 }
129
130                 static void ThrowException (string message, XmlNode node)
131                 {
132                         HandlersUtil.ThrowException (message, node);
133                 }
134                 //
135         }
136 }
137