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