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