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