2 // System.Web.Configuration.CompilationConfigurationHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Configuration;
35 namespace System.Web.Configuration
37 class CompilationConfigurationHandler : IConfigurationSectionHandler
39 public object Create (object parent, object context, XmlNode section)
41 CompilationConfiguration config = new CompilationConfiguration (parent);
43 string tmp = AttValue ("tempDirectory", section, true);
44 if (tmp != null && tmp != "")
45 config.TempDirectory = tmp;
46 config.DefaultLanguage = AttValue ("defaultLanguage", section);
47 if (config.DefaultLanguage == null)
48 config.DefaultLanguage = "c#";
50 config.Debug = AttBoolValue ("debug", section, false);
51 config.Batch = AttBoolValue ("batch", section, false);
52 config.Explicit = AttBoolValue ("explicit", section, true);
53 config.Strict = AttBoolValue ("strict", section, false);
54 config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
55 config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
56 config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
57 config.NumRecompilesBeforeAppRestart =
58 AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
60 if (section.Attributes != null && section.Attributes.Count != 0)
61 ThrowException ("Unrecognized attribute.", section);
63 XmlNodeList authNodes = section.ChildNodes;
64 foreach (XmlNode child in authNodes) {
65 XmlNodeType ntype = child.NodeType;
66 if (ntype != XmlNodeType.Element)
69 if (child.Name == "compilers") {
70 ReadCompilers (child.ChildNodes, config);
74 if (child.Name == "assemblies") {
75 ReadAssemblies (child.ChildNodes, config);
79 ThrowException ("Unexpected element", child);
85 static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
87 foreach (XmlNode child in nodes) {
88 XmlNodeType ntype = child.NodeType;
89 if (ntype != XmlNodeType.Element)
92 if (child.Name != "compiler")
93 ThrowException ("Unexpected element", child);
95 Compiler compiler = new Compiler ();
96 compiler.Language = AttValue ("language", child);
97 compiler.Extension = AttValue ("extension", child);
98 compiler.Type = AttValue ("type", child);
99 compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
100 compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
101 config.Compilers [compiler.Language] = compiler;
105 static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
107 ArrayList assemblies = config.Assemblies;
109 foreach (XmlNode child in nodes) {
110 XmlNodeType ntype = child.NodeType;
111 if (ntype != XmlNodeType.Element)
114 if (child.Name == "clear") {
116 config.AssembliesInBin = false;
120 string aname = AttValue ("assembly", child);
121 if (child.Name == "add") {
123 config.AssembliesInBin = true;
127 if (!assemblies.Contains (aname))
128 assemblies.Add (aname);
133 if (child.Name == "remove") {
135 config.AssembliesInBin = false;
138 assemblies.Remove (aname);
142 ThrowException ("Unexpected element " + child.Name, child);
146 static string AttValue (string name, XmlNode node, bool optional)
148 return AttValue (name, node, optional, false);
151 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
153 return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
156 static bool AttBoolValue (string name, XmlNode node, bool _default)
158 string v = AttValue (name, node, true);
162 bool result = (v == "true");
163 if (!result && v != "false")
164 ThrowException ("Invalid boolean value in " + name, node);
169 static int AttUIntValue (string name, XmlNode node, int _default)
171 string v = AttValue (name, node, true);
177 result = (int) UInt32.Parse (v);
179 ThrowException ("Invalid number in " + name, node);
185 static string AttValue (string name, XmlNode node)
187 return HandlersUtil.ExtractAttributeValue (name, node, true);
190 static void ThrowException (string message, XmlNode node)
192 HandlersUtil.ThrowException (message, node);