2 // System.Web.Configuration.CompilationConfigurationHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 using System.Collections;
12 using System.Configuration;
15 namespace System.Web.Configuration
17 class CompilationConfigurationHandler : IConfigurationSectionHandler
19 public object Create (object parent, object context, XmlNode section)
21 CompilationConfiguration config = new CompilationConfiguration (parent);
23 config.TempDirectory = AttValue ("tempDirectory", section, true);
24 config.DefaultLanguage = AttValue ("defaultLanguage", section);
25 if (config.DefaultLanguage == null)
26 config.DefaultLanguage = "c#";
28 config.Debug = AttBoolValue ("debug", section, false);
29 config.Batch = AttBoolValue ("batch", section, false);
30 config.Explicit = AttBoolValue ("explicit", section, true);
31 config.Strict = AttBoolValue ("strict", section, false);
32 config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
33 config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
34 config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
35 config.NumRecompilesBeforeAppRestart =
36 AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
38 if (section.Attributes != null && section.Attributes.Count != 0)
39 ThrowException ("Unrecognized attribute.", section);
41 XmlNodeList authNodes = section.ChildNodes;
42 foreach (XmlNode child in authNodes) {
43 XmlNodeType ntype = child.NodeType;
44 if (ntype != XmlNodeType.Element)
47 if (child.Name == "compilers") {
48 ReadCompilers (child.ChildNodes, config);
52 if (child.Name == "assemblies") {
53 ReadAssemblies (child.ChildNodes, config);
57 ThrowException ("Unexpected element", child);
63 static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
65 foreach (XmlNode child in nodes) {
66 XmlNodeType ntype = child.NodeType;
67 if (ntype != XmlNodeType.Element)
70 if (child.Name != "compiler")
71 ThrowException ("Unexpected element", child);
73 WebCompiler compiler = new WebCompiler ();
74 compiler.Languages = AttValue ("language", child);
75 compiler.Extension = AttValue ("extension", child);
76 compiler.Type = AttValue ("type", child);
77 compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
78 compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
79 config.Compilers [compiler.Languages] = compiler;
83 static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
85 ArrayList assemblies = config.Assemblies;
87 foreach (XmlNode child in nodes) {
88 XmlNodeType ntype = child.NodeType;
89 if (ntype != XmlNodeType.Element)
92 if (child.Name == "clear") {
94 config.AssembliesInBin = false;
98 string aname = AttValue ("assembly", child);
99 if (child.Name == "add") {
101 config.AssembliesInBin = true;
105 aname = ShortAsmName (aname);
106 if (!assemblies.Contains (aname))
107 assemblies.Add (aname);
112 if (child.Name == "remove") {
114 config.AssembliesInBin = false;
117 aname = ShortAsmName (aname);
118 assemblies.Remove (aname);
122 ThrowException ("Unexpected element " + child.Name, child);
126 static string AttValue (string name, XmlNode node, bool optional)
128 return AttValue (name, node, optional, false);
131 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
133 return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
136 static bool AttBoolValue (string name, XmlNode node, bool _default)
138 string v = AttValue (name, node, true);
142 bool result = (v == "true");
143 if (!result && v != "false")
144 ThrowException ("Invalid boolean value in " + name, node);
149 static int AttUIntValue (string name, XmlNode node, int _default)
151 string v = AttValue (name, node, true);
157 result = (int) UInt32.Parse (v);
159 ThrowException ("Invalid number in " + name, node);
165 static string AttValue (string name, XmlNode node)
167 return HandlersUtil.ExtractAttributeValue (name, node, true);
170 private static string ShortAsmName (string long_name)
172 int i = long_name.IndexOf (',');
174 return long_name + ".dll";
175 return long_name.Substring (0, i) + ".dll";
178 static void ThrowException (string message, XmlNode node)
180 HandlersUtil.ThrowException (message, node);