* SessionStateModule.cs: If using cookieless sessions add an
[mono.git] / mcs / class / System.Web / System.Web.Configuration / CompilationConfigurationHandler.cs
1 //
2 // System.Web.Configuration.CompilationConfigurationHandler
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.Collections;
12 using System.Configuration;
13 using System.Xml;
14
15 namespace System.Web.Configuration
16 {
17         class CompilationConfigurationHandler : IConfigurationSectionHandler
18         {
19                 public object Create (object parent, object context, XmlNode section)
20                 {
21                         CompilationConfiguration config = new CompilationConfiguration (parent);
22
23                         config.TempDirectory = AttValue ("tempDirectory", section, true);
24                         config.DefaultLanguage = AttValue ("defaultLanguage", section);
25                         if (config.DefaultLanguage == null)
26                                 config.DefaultLanguage = "c#";
27
28                         config.Debug = AttBoolValue ("debug", section, false);
29                         config.Batch = AttBoolValue ("batch", section, false);
30                         config.Explicit = AttBoolValue ("explicit", section, true);
31                         config.Strict = AttBoolValue ("strict", section, false);
32                         config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
33                         config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
34                         config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
35                         config.NumRecompilesBeforeAppRestart =
36                                         AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
37
38                         if (section.Attributes != null && section.Attributes.Count != 0)
39                                 ThrowException ("Unrecognized attribute.", section);
40
41                         XmlNodeList authNodes = section.ChildNodes;
42                         foreach (XmlNode child in authNodes) {
43                                 XmlNodeType ntype = child.NodeType;
44                                 if (ntype != XmlNodeType.Element)
45                                         continue;
46                                 
47                                 if (child.Name == "compilers") {
48                                         ReadCompilers (child.ChildNodes, config);
49                                         continue;
50                                 }
51
52                                 if (child.Name == "assemblies") {
53                                         ReadAssemblies (child.ChildNodes, config);
54                                         continue;
55                                 }
56
57                                 ThrowException ("Unexpected element", child);
58                         }
59
60                         return config;
61                 }
62
63                 static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
64                 {
65                         foreach (XmlNode child in nodes) {
66                                 XmlNodeType ntype = child.NodeType;
67                                 if (ntype != XmlNodeType.Element)
68                                         continue;
69
70                                 if (child.Name != "compiler")
71                                         ThrowException ("Unexpected element", child);
72
73                                 WebCompiler compiler = new WebCompiler ();
74                                 compiler.Languages = AttValue ("language", child);
75                                 compiler.Extension = AttValue ("extension", child);
76                                 compiler.Type = AttValue ("type", child);
77                                 compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
78                                 compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
79                                 config.Compilers [compiler.Languages] = compiler;
80                         }
81                 }
82
83                 static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
84                 {
85                         ArrayList assemblies = config.Assemblies;
86
87                         foreach (XmlNode child in nodes) {
88                                 XmlNodeType ntype = child.NodeType;
89                                 if (ntype != XmlNodeType.Element)
90                                         continue;
91
92                                 if (child.Name == "clear") {
93                                         assemblies.Clear ();
94                                         config.AssembliesInBin = false;
95                                         continue;
96                                 }
97
98                                 string aname = AttValue ("assembly", child);
99                                 if (child.Name == "add") {
100                                         if (aname == "*") {
101                                                 config.AssembliesInBin = true;
102                                                 continue;
103                                         }
104
105                                         aname = ShortAsmName (aname);
106                                         if (!assemblies.Contains (aname))
107                                                 assemblies.Add (aname);
108
109                                         continue;
110                                 }
111
112                                 if (child.Name == "remove") {
113                                         if (aname == "*") {
114                                                 config.AssembliesInBin = false;
115                                                 continue;
116                                         }
117                                         aname = ShortAsmName (aname);
118                                         assemblies.Remove (aname);
119                                         continue;
120                                 }
121
122                                 ThrowException ("Unexpected element " + child.Name, child);
123                         }
124                 }
125
126                 static string AttValue (string name, XmlNode node, bool optional)
127                 {
128                         return AttValue (name, node, optional, false);
129                 }
130                 
131                 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
132                 {
133                         return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
134                 }
135
136                 static bool AttBoolValue (string name, XmlNode node, bool _default)
137                 {
138                         string v = AttValue (name, node, true);
139                         if (v == null)
140                                 return _default;
141
142                         bool result = (v == "true");
143                         if (!result && v != "false")
144                                 ThrowException ("Invalid boolean value in " + name, node);
145
146                         return result;
147                 }
148
149                 static int AttUIntValue (string name, XmlNode node, int _default)
150                 {
151                         string v = AttValue (name, node, true);
152                         if (v == null)
153                                 return _default;
154
155                         int result = 0;
156                         try {
157                                 result = (int) UInt32.Parse (v);
158                         } catch {
159                                 ThrowException ("Invalid number in " + name, node);
160                         }
161
162                         return result;
163                 }
164
165                 static string AttValue (string name, XmlNode node)
166                 {
167                         return HandlersUtil.ExtractAttributeValue (name, node, true);
168                 }
169
170                 private static string ShortAsmName (string long_name)
171                 {
172                         int i = long_name.IndexOf (',');
173                         if (i < 0)
174                                 return long_name + ".dll";
175                         return long_name.Substring (0, i) + ".dll";
176                 }
177                 
178                 static void ThrowException (string message, XmlNode node)
179                 {
180                         HandlersUtil.ThrowException (message, node);
181                 }
182         }
183 }
184