2
0

CompilationConfigurationHandler.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. config.Compilers [compiler.Language] = compiler;
  89. }
  90. }
  91. static void ReadAssemblies (XmlNodeList nodes, CompilationConfiguration config)
  92. {
  93. ArrayList assemblies = config.Assemblies;
  94. foreach (XmlNode child in nodes) {
  95. XmlNodeType ntype = child.NodeType;
  96. if (ntype != XmlNodeType.Element)
  97. continue;
  98. if (child.Name == "clear") {
  99. assemblies.Clear ();
  100. config.AssembliesInBin = false;
  101. continue;
  102. }
  103. string aname = AttValue ("assembly", child);
  104. if (child.Name == "add") {
  105. if (aname == "*") {
  106. config.AssembliesInBin = true;
  107. continue;
  108. }
  109. if (!assemblies.Contains (aname))
  110. assemblies.Add (aname);
  111. continue;
  112. }
  113. if (child.Name == "remove") {
  114. if (aname == "*") {
  115. config.AssembliesInBin = false;
  116. continue;
  117. }
  118. assemblies.Remove (aname);
  119. continue;
  120. }
  121. ThrowException ("Unexpected element " + child.Name, child);
  122. }
  123. }
  124. static string AttValue (string name, XmlNode node, bool optional)
  125. {
  126. return AttValue (name, node, optional, false);
  127. }
  128. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  129. {
  130. return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
  131. }
  132. static bool AttBoolValue (string name, XmlNode node, bool _default)
  133. {
  134. string v = AttValue (name, node, true);
  135. if (v == null)
  136. return _default;
  137. bool result = (v == "true");
  138. if (!result && v != "false")
  139. ThrowException ("Invalid boolean value in " + name, node);
  140. return result;
  141. }
  142. static int AttUIntValue (string name, XmlNode node, int _default)
  143. {
  144. string v = AttValue (name, node, true);
  145. if (v == null)
  146. return _default;
  147. int result = 0;
  148. try {
  149. result = (int) UInt32.Parse (v);
  150. } catch {
  151. ThrowException ("Invalid number in " + name, node);
  152. }
  153. return result;
  154. }
  155. static string AttValue (string name, XmlNode node)
  156. {
  157. return HandlersUtil.ExtractAttributeValue (name, node, true);
  158. }
  159. static void ThrowException (string message, XmlNode node)
  160. {
  161. HandlersUtil.ThrowException (message, node);
  162. }
  163. }
  164. }