HttpHandlersSectionHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Web.Configuration.HttpHandlersSectionHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Configuration;
  11. using System.Xml;
  12. namespace System.Web.Configuration
  13. {
  14. class HttpHandlersSectionHandler : IConfigurationSectionHandler
  15. {
  16. public virtual object Create (object parent, object configContext, XmlNode section)
  17. {
  18. HandlerFactoryConfiguration mapper;
  19. if (parent is HandlerFactoryConfiguration)
  20. mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
  21. else
  22. mapper = new HandlerFactoryConfiguration ();
  23. if (section.Attributes != null && section.Attributes.Count != 0)
  24. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  25. XmlNodeList httpHandlers = section.ChildNodes;
  26. foreach (XmlNode child in httpHandlers) {
  27. XmlNodeType ntype = child.NodeType;
  28. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  29. continue;
  30. if (ntype != XmlNodeType.Element)
  31. HandlersUtil.ThrowException ("Only elements allowed", child);
  32. string name = child.Name;
  33. if (name == "clear") {
  34. if (child.Attributes != null && child.Attributes.Count != 0)
  35. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  36. mapper.Clear ();
  37. continue;
  38. }
  39. string verb = HandlersUtil.ExtractAttributeValue ("verb", child);
  40. string path = HandlersUtil.ExtractAttributeValue ("path", child);
  41. string validateStr = HandlersUtil.ExtractAttributeValue ("validate", child, true);
  42. bool validate;
  43. if (validateStr == null) {
  44. validate = true;
  45. } else {
  46. validate = validateStr == "true";
  47. if (!validate && validateStr != "false")
  48. HandlersUtil.ThrowException (
  49. "Invalid value for validate attribute.", child);
  50. }
  51. if (name == "add") {
  52. string type = HandlersUtil.ExtractAttributeValue ("type", child);
  53. if (child.Attributes != null && child.Attributes.Count != 0)
  54. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  55. HandlerItem item = new HandlerItem (verb, path, type, validate);
  56. mapper.Add (item);
  57. continue;
  58. }
  59. if (name == "remove") {
  60. if (child.Attributes != null && child.Attributes.Count != 0)
  61. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  62. if (validate && mapper.Remove (verb, path) == null)
  63. HandlersUtil.ThrowException ("There's no mapping to remove", child);
  64. continue;
  65. }
  66. HandlersUtil.ThrowException ("Unexpected element", child);
  67. }
  68. return mapper;
  69. }
  70. }
  71. internal class HandlersUtil
  72. {
  73. private HandlersUtil ()
  74. {
  75. }
  76. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  77. {
  78. return ExtractAttributeValue (attKey, node, false);
  79. }
  80. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  81. {
  82. return ExtractAttributeValue (attKey, node, optional, false);
  83. }
  84. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
  85. bool allowEmpty)
  86. {
  87. if (node.Attributes == null) {
  88. if (optional)
  89. return null;
  90. ThrowException ("Required attribute not found: " + attKey, node);
  91. }
  92. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  93. if (att == null) {
  94. if (optional)
  95. return null;
  96. ThrowException ("Required attribute not found: " + attKey, node);
  97. }
  98. string value = att.Value;
  99. if (!allowEmpty && value == String.Empty) {
  100. string opt = optional ? "Optional" : "Required";
  101. ThrowException (opt + " attribute is empty: " + attKey, node);
  102. }
  103. return value;
  104. }
  105. static internal void ThrowException (string msg, XmlNode node)
  106. {
  107. if (node != null && node.Name != String.Empty)
  108. msg = msg + " (node name: " + node.Name + ") ";
  109. throw new ConfigurationException (msg, node);
  110. }
  111. }
  112. }