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 config.TempDirectory = AttValue ("tempDirectory", section, true);
44 config.DefaultLanguage = AttValue ("defaultLanguage", section);
45 if (config.DefaultLanguage == null)
46 config.DefaultLanguage = "c#";
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);
58 if (section.Attributes != null && section.Attributes.Count != 0)
59 ThrowException ("Unrecognized attribute.", section);
61 XmlNodeList authNodes = section.ChildNodes;
62 foreach (XmlNode child in authNodes) {
63 XmlNodeType ntype = child.NodeType;
64 if (ntype != XmlNodeType.Element)
67 if (child.Name == "compilers") {
68 ReadCompilers (child.ChildNodes, config);
72 if (child.Name == "assemblies") {
73 ReadAssemblies (child.ChildNodes, config);
77 ThrowException ("Unexpected element", child);
83 static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
85 foreach (XmlNode child in nodes) {
86 XmlNodeType ntype = child.NodeType;
87 if (ntype != XmlNodeType.Element)
90 if (child.Name != "compiler")
91 ThrowException ("Unexpected element", child);
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;
103 static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
105 ArrayList assemblies = config.Assemblies;
107 foreach (XmlNode child in nodes) {
108 XmlNodeType ntype = child.NodeType;
109 if (ntype != XmlNodeType.Element)
112 if (child.Name == "clear") {
114 config.AssembliesInBin = false;
118 string aname = AttValue ("assembly", child);
119 if (child.Name == "add") {
121 config.AssembliesInBin = true;
125 aname = ShortAsmName (aname);
126 if (!assemblies.Contains (aname))
127 assemblies.Add (aname);
132 if (child.Name == "remove") {
134 config.AssembliesInBin = false;
137 aname = ShortAsmName (aname);
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 private static string ShortAsmName (string long_name)
192 int i = long_name.IndexOf (',');
194 return long_name + ".dll";
195 return long_name.Substring (0, i) + ".dll";
198 static void ThrowException (string message, XmlNode node)
200 HandlersUtil.ThrowException (message, node);