CompilationConfigurationHandler.cs 4.8 KB

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