2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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                 static WebControlsConfig instance;
42                 string scriptsVDir;
43                 string configFilePath;
44                 
45                 public WebControlsConfig (WebControlsConfig parent, object context)
46                 {
47                         configFilePath = context as string;
48                         if (parent == null)
49                                 return;
50                         
51                         scriptsVDir = parent.scriptsVDir;
52                         if (scriptsVDir != null)
53                                 configFilePath = parent.configFilePath;
54                 }
55                 
56                 public void SetClientScriptsLocation (string location, out string error)
57                 {
58                         error = null;
59                         if (location == null || location.Length == 0) {
60                                 error = "empty or null value for clientScriptsLocation";
61                                 return;
62                         }
63
64                         if (location [0] != '/')
65                                 location = "/" + location;
66
67                         string [] splitted = location.Split ('/');
68                         int end = splitted.Length;
69                         for (int i = 0; i < end; i++)
70                                 splitted [i] = HttpUtility.UrlEncode (splitted [i]);
71
72                         scriptsVDir = String.Join ("/", splitted);
73                 }
74
75                 public string ScriptsPhysicalDirectory {
76                         get { return Path.Combine (Path.GetDirectoryName (configFilePath), "web_scripts"); }
77                 }
78
79                 public string ScriptsVirtualDirectory {
80                         get { return scriptsVDir; }
81                         set { scriptsVDir = value; }
82                 }
83
84                 static public WebControlsConfig Instance {
85                         get {
86                                 //TODO: use HttpContext to get the configuration
87                                 if (instance != null)
88                                         return instance;
89
90                                 lock (typeof (WebControlsConfig)) {
91                                         if (instance != null)
92                                                 return instance;
93
94                                         instance = (WebControlsConfig) ConfigurationSettings.GetConfig ("system.web/webControls");
95                                 }
96
97                                 return instance;
98                         }
99                 }
100         }
101         
102         class WebControlsSectionHandler : IConfigurationSectionHandler
103         {
104                 public object Create (object parent, object context, XmlNode section)
105                 {
106                         WebControlsConfig config = new WebControlsConfig (parent as WebControlsConfig, context);
107
108                         if (section.Attributes == null && section.Attributes.Count == 0)
109                                 ThrowException ("Lack of clientScriptsLocation attribute", section);
110
111                         string clientLocation = AttValue ("clientScriptsLocation", section, false);
112                         if (section.Attributes != null && section.Attributes.Count != 0)
113                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
114
115                         string error;
116                         config.SetClientScriptsLocation (clientLocation, out error);
117                         if (error != null)
118                                 HandlersUtil.ThrowException (error, section);
119
120                         return config;
121                 }
122
123                 // To save some typing...
124                 static string AttValue (string name, XmlNode node, bool optional)
125                 {
126                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
127                 }
128
129                 static string AttValue (string name, XmlNode node)
130                 {
131                         return HandlersUtil.ExtractAttributeValue (name, node, true);
132                 }
133
134                 static void ThrowException (string message, XmlNode node)
135                 {
136                         HandlersUtil.ThrowException (message, node);
137                 }
138                 //
139         }
140 }
141