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.
32 using System.Collections;
33 using System.Configuration;
36 namespace System.Web.Configuration
38 class CompilationConfigurationHandler : IConfigurationSectionHandler
40 public object Create (object parent, object context, XmlNode section)
42 CompilationConfiguration config = new CompilationConfiguration (parent);
44 config.TempDirectory = AttValue ("tempDirectory", section, true);
45 config.DefaultLanguage = AttValue ("defaultLanguage", section);
46 if (config.DefaultLanguage == null)
47 config.DefaultLanguage = "c#";
49 config.Debug = AttBoolValue ("debug", section, false);
50 config.Batch = AttBoolValue ("batch", section, false);
51 config.Explicit = AttBoolValue ("explicit", section, true);
52 config.Strict = AttBoolValue ("strict", section, false);
53 config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
54 config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
55 config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
56 config.NumRecompilesBeforeAppRestart =
57 AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
59 if (section.Attributes != null && section.Attributes.Count != 0)
60 ThrowException ("Unrecognized attribute.", section);
62 XmlNodeList authNodes = section.ChildNodes;
63 foreach (XmlNode child in authNodes) {
64 XmlNodeType ntype = child.NodeType;
65 if (ntype != XmlNodeType.Element)
68 if (child.Name == "compilers") {
69 ReadCompilers (child.ChildNodes, config);
73 if (child.Name == "assemblies") {
74 ReadAssemblies (child.ChildNodes, config);
78 ThrowException ("Unexpected element", child);
84 static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
86 foreach (XmlNode child in nodes) {
87 XmlNodeType ntype = child.NodeType;
88 if (ntype != XmlNodeType.Element)
91 if (child.Name != "compiler")
92 ThrowException ("Unexpected element", child);
94 WebCompiler compiler = new WebCompiler ();
95 compiler.Languages = AttValue ("language", child);
96 compiler.Extension = AttValue ("extension", child);
97 compiler.Type = AttValue ("type", child);
98 compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
99 compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
100 config.Compilers [compiler.Languages] = compiler;
104 static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
106 ArrayList assemblies = config.Assemblies;
108 foreach (XmlNode child in nodes) {
109 XmlNodeType ntype = child.NodeType;
110 if (ntype != XmlNodeType.Element)
113 if (child.Name == "clear") {
115 config.AssembliesInBin = false;
119 string aname = AttValue ("assembly", child);
120 if (child.Name == "add") {
122 config.AssembliesInBin = true;
126 aname = ShortAsmName (aname);
127 if (!assemblies.Contains (aname))
128 assemblies.Add (aname);
133 if (child.Name == "remove") {
135 config.AssembliesInBin = false;
138 aname = ShortAsmName (aname);
139 assemblies.Remove (aname);
143 ThrowException ("Unexpected element " + child.Name, child);
147 static string AttValue (string name, XmlNode node, bool optional)
149 return AttValue (name, node, optional, false);
152 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
154 return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
157 static bool AttBoolValue (string name, XmlNode node, bool _default)
159 string v = AttValue (name, node, true);
163 bool result = (v == "true");
164 if (!result && v != "false")
165 ThrowException ("Invalid boolean value in " + name, node);
170 static int AttUIntValue (string name, XmlNode node, int _default)
172 string v = AttValue (name, node, true);
178 result = (int) UInt32.Parse (v);
180 ThrowException ("Invalid number in " + name, node);
186 static string AttValue (string name, XmlNode node)
188 return HandlersUtil.ExtractAttributeValue (name, node, true);
191 private static string ShortAsmName (string long_name)
193 int i = long_name.IndexOf (',');
195 return long_name + ".dll";
196 return long_name.Substring (0, i) + ".dll";
199 static void ThrowException (string message, XmlNode node)
201 HandlersUtil.ThrowException (message, node);