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