CompilationConfigurationHandler.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Web.Configuration.CompilationConfigurationHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if !NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.Xml;
  34. namespace System.Web.Configuration
  35. {
  36. class CompilationConfigurationHandler : IConfigurationSectionHandler
  37. {
  38. public object Create (object parent, object context, XmlNode section)
  39. {
  40. CompilationConfiguration config = new CompilationConfiguration (parent);
  41. string tmp = AttValue ("tempDirectory", section, true);
  42. if (tmp != null && tmp != "")
  43. config.TempDirectory = tmp;
  44. config.DefaultLanguage = AttValue ("defaultLanguage", section);
  45. if (config.DefaultLanguage == null)
  46. config.DefaultLanguage = "c#";
  47. config.Debug = AttBoolValue ("debug", section, false);
  48. config.Batch = AttBoolValue ("batch", section, false);
  49. config.Explicit = AttBoolValue ("explicit", section, true);
  50. config.Strict = AttBoolValue ("strict", section, false);
  51. config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
  52. config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
  53. config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
  54. config.NumRecompilesBeforeAppRestart =
  55. AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
  56. if (section.Attributes != null && section.Attributes.Count != 0)
  57. ThrowException ("Unrecognized attribute.", section);
  58. XmlNodeList authNodes = section.ChildNodes;
  59. foreach (XmlNode child in authNodes) {
  60. XmlNodeType ntype = child.NodeType;
  61. if (ntype != XmlNodeType.Element)
  62. continue;
  63. if (child.Name == "compilers") {
  64. ReadCompilers (child.ChildNodes, config);
  65. continue;
  66. }
  67. if (child.Name == "assemblies") {
  68. ReadAssemblies (child.ChildNodes, config);
  69. continue;
  70. }
  71. ThrowException ("Unexpected element", child);
  72. }
  73. return config;
  74. }
  75. static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
  76. {
  77. foreach (XmlNode child in nodes) {
  78. XmlNodeType ntype = child.NodeType;
  79. if (ntype != XmlNodeType.Element)
  80. continue;
  81. if (child.Name != "compiler")
  82. ThrowException ("Unexpected element", child);
  83. Compiler compiler = new Compiler ();
  84. compiler.Language = AttValue ("language", child);
  85. compiler.Extension = AttValue ("extension", child);
  86. compiler.Type = AttValue ("type", child);
  87. compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
  88. compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
  89. #if NET_2_0
  90. config.Compilers.Add (compiler);
  91. #else
  92. config.Compilers [compiler.Language] = compiler;
  93. #endif
  94. }
  95. }
  96. static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
  97. {
  98. ArrayList assemblies = config.Assemblies;
  99. foreach (XmlNode child in nodes) {
  100. XmlNodeType ntype = child.NodeType;
  101. if (ntype != XmlNodeType.Element)
  102. continue;
  103. if (child.Name == "clear") {
  104. assemblies.Clear ();
  105. config.AssembliesInBin = false;
  106. continue;
  107. }
  108. string aname = AttValue ("assembly", child);
  109. if (child.Name == "add") {
  110. if (aname == "*") {
  111. config.AssembliesInBin = true;
  112. continue;
  113. }
  114. if (!assemblies.Contains (aname))
  115. assemblies.Add (aname);
  116. continue;
  117. }
  118. if (child.Name == "remove") {
  119. if (aname == "*") {
  120. config.AssembliesInBin = false;
  121. continue;
  122. }
  123. assemblies.Remove (aname);
  124. continue;
  125. }
  126. ThrowException ("Unexpected element " + child.Name, child);
  127. }
  128. }
  129. static string AttValue (string name, XmlNode node, bool optional)
  130. {
  131. return AttValue (name, node, optional, false);
  132. }
  133. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  134. {
  135. return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
  136. }
  137. static bool AttBoolValue (string name, XmlNode node, bool _default)
  138. {
  139. string v = AttValue (name, node, true);
  140. if (v == null)
  141. return _default;
  142. bool result = (v == "true");
  143. if (!result && v != "false")
  144. ThrowException ("Invalid boolean value in " + name, node);
  145. return result;
  146. }
  147. static int AttUIntValue (string name, XmlNode node, int _default)
  148. {
  149. string v = AttValue (name, node, true);
  150. if (v == null)
  151. return _default;
  152. int result = 0;
  153. try {
  154. result = (int) UInt32.Parse (v);
  155. } catch {
  156. ThrowException ("Invalid number in " + name, node);
  157. }
  158. return result;
  159. }
  160. static string AttValue (string name, XmlNode node)
  161. {
  162. return HandlersUtil.ExtractAttributeValue (name, node, true);
  163. }
  164. static void ThrowException (string message, XmlNode node)
  165. {
  166. HandlersUtil.ThrowException (message, node);
  167. }
  168. }
  169. }
  170. #endif