2
0

CompilationConfigurationHandler.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.Xml;
  33. namespace System.Web.Configuration
  34. {
  35. class CompilationConfigurationHandler : IConfigurationSectionHandler
  36. {
  37. public object Create (object parent, object context, XmlNode section)
  38. {
  39. CompilationConfiguration config = new CompilationConfiguration (parent);
  40. string tmp = AttValue ("tempDirectory", section, true);
  41. if (tmp != null && tmp != "")
  42. config.TempDirectory = tmp;
  43. config.DefaultLanguage = AttValue ("defaultLanguage", section);
  44. if (config.DefaultLanguage == null)
  45. config.DefaultLanguage = "c#";
  46. config.Debug = AttBoolValue ("debug", section, false);
  47. config.Batch = AttBoolValue ("batch", section, false);
  48. config.Explicit = AttBoolValue ("explicit", section, true);
  49. config.Strict = AttBoolValue ("strict", section, false);
  50. config.BatchTimeout = AttUIntValue ("batchTimeout", section, 0);
  51. config.MaxBatchSize = AttUIntValue ("maxBatchSize", section, 0);
  52. config.MaxBatchFileSize = AttUIntValue ("maxBatchFileSize", section, 0);
  53. config.NumRecompilesBeforeAppRestart =
  54. AttUIntValue ("numRecompilesBeforeAppRestart", section, 15);
  55. if (section.Attributes != null && section.Attributes.Count != 0)
  56. ThrowException ("Unrecognized attribute.", section);
  57. XmlNodeList authNodes = section.ChildNodes;
  58. foreach (XmlNode child in authNodes) {
  59. XmlNodeType ntype = child.NodeType;
  60. if (ntype != XmlNodeType.Element)
  61. continue;
  62. if (child.Name == "compilers") {
  63. ReadCompilers (child.ChildNodes, config);
  64. continue;
  65. }
  66. if (child.Name == "assemblies") {
  67. ReadAssemblies (child.ChildNodes, config);
  68. continue;
  69. }
  70. ThrowException ("Unexpected element", child);
  71. }
  72. return config;
  73. }
  74. static void ReadCompilers (XmlNodeList nodes, CompilationConfiguration config)
  75. {
  76. foreach (XmlNode child in nodes) {
  77. XmlNodeType ntype = child.NodeType;
  78. if (ntype != XmlNodeType.Element)
  79. continue;
  80. if (child.Name != "compiler")
  81. ThrowException ("Unexpected element", child);
  82. Compiler compiler = new Compiler ();
  83. compiler.Language = AttValue ("language", child);
  84. compiler.Extension = AttValue ("extension", child);
  85. compiler.Type = AttValue ("type", child);
  86. compiler.CompilerOptions = AttValue ("compilerOptions", child, true, true);
  87. compiler.WarningLevel = AttUIntValue ("warningLevel", child, 0);
  88. #if CONFIGURATION_2_0
  89. config.Compilers.Add (compiler);
  90. #else
  91. config.Compilers [compiler.Language] = compiler;
  92. #endif
  93. }
  94. }
  95. static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
  96. {
  97. ArrayList assemblies = config.Assemblies;
  98. foreach (XmlNode child in nodes) {
  99. XmlNodeType ntype = child.NodeType;
  100. if (ntype != XmlNodeType.Element)
  101. continue;
  102. if (child.Name == "clear") {
  103. assemblies.Clear ();
  104. config.AssembliesInBin = false;
  105. continue;
  106. }
  107. string aname = AttValue ("assembly", child);
  108. if (child.Name == "add") {
  109. if (aname == "*") {
  110. config.AssembliesInBin = true;
  111. continue;
  112. }
  113. if (!assemblies.Contains (aname))
  114. assemblies.Add (aname);
  115. continue;
  116. }
  117. if (child.Name == "remove") {
  118. if (aname == "*") {
  119. config.AssembliesInBin = false;
  120. continue;
  121. }
  122. assemblies.Remove (aname);
  123. continue;
  124. }
  125. ThrowException ("Unexpected element " + child.Name, child);
  126. }
  127. }
  128. static string AttValue (string name, XmlNode node, bool optional)
  129. {
  130. return AttValue (name, node, optional, false);
  131. }
  132. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  133. {
  134. return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
  135. }
  136. static bool AttBoolValue (string name, XmlNode node, bool _default)
  137. {
  138. string v = AttValue (name, node, true);
  139. if (v == null)
  140. return _default;
  141. bool result = (v == "true");
  142. if (!result && v != "false")
  143. ThrowException ("Invalid boolean value in " + name, node);
  144. return result;
  145. }
  146. static int AttUIntValue (string name, XmlNode node, int _default)
  147. {
  148. string v = AttValue (name, node, true);
  149. if (v == null)
  150. return _default;
  151. int result = 0;
  152. try {
  153. result = (int) UInt32.Parse (v);
  154. } catch {
  155. ThrowException ("Invalid number in " + name, node);
  156. }
  157. return result;
  158. }
  159. static string AttValue (string name, XmlNode node)
  160. {
  161. return HandlersUtil.ExtractAttributeValue (name, node, true);
  162. }
  163. static void ThrowException (string message, XmlNode node)
  164. {
  165. HandlersUtil.ThrowException (message, node);
  166. }
  167. }
  168. }