2
0

WebControlsSectionHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // System.Web.Configuration.WebControlsSectionHandler
  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.Configuration;
  11. using System.IO;
  12. using System.Web;
  13. using System.Xml;
  14. namespace System.Web.Configuration
  15. {
  16. class WebControlsConfig
  17. {
  18. static WebControlsConfig instance;
  19. string scriptsVDir;
  20. string configFilePath;
  21. public WebControlsConfig (WebControlsConfig parent, object context)
  22. {
  23. configFilePath = context as string;
  24. if (parent == null)
  25. return;
  26. scriptsVDir = parent.scriptsVDir;
  27. if (scriptsVDir != null)
  28. configFilePath = parent.configFilePath;
  29. }
  30. public void SetClientScriptsLocation (string location, out string error)
  31. {
  32. error = null;
  33. if (location == null || location.Length == 0) {
  34. error = "empty or null value for clientScriptsLocation";
  35. return;
  36. }
  37. if (location [0] != '/')
  38. location = "/" + location;
  39. string [] splitted = location.Split ('/');
  40. int end = splitted.Length;
  41. for (int i = 0; i < end; i++)
  42. splitted [i] = HttpUtility.UrlEncode (splitted [i]);
  43. scriptsVDir = String.Join ("/", splitted);
  44. }
  45. public string ScriptsPhysicalDirectory {
  46. get { return Path.Combine (Path.GetDirectoryName (configFilePath), "web_scripts"); }
  47. }
  48. public string ScriptsVirtualDirectory {
  49. get { return scriptsVDir; }
  50. set { scriptsVDir = value; }
  51. }
  52. static public WebControlsConfig Instance {
  53. get {
  54. //TODO: use HttpContext to get the configuration
  55. if (instance != null)
  56. return instance;
  57. lock (typeof (WebControlsConfig)) {
  58. if (instance != null)
  59. return instance;
  60. instance = (WebControlsConfig) ConfigurationSettings.GetConfig ("system.web/webControls");
  61. }
  62. return instance;
  63. }
  64. }
  65. }
  66. class WebControlsSectionHandler : IConfigurationSectionHandler
  67. {
  68. public object Create (object parent, object context, XmlNode section)
  69. {
  70. WebControlsConfig config = new WebControlsConfig (parent as WebControlsConfig, context);
  71. if (section.Attributes == null && section.Attributes.Count == 0)
  72. ThrowException ("Lack of clientScriptsLocation attribute", section);
  73. string clientLocation = AttValue ("clientScriptsLocation", section, false);
  74. if (section.Attributes != null && section.Attributes.Count != 0)
  75. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  76. string error;
  77. config.SetClientScriptsLocation (clientLocation, out error);
  78. if (error != null)
  79. HandlersUtil.ThrowException (error, section);
  80. return config;
  81. }
  82. // To save some typing...
  83. static string AttValue (string name, XmlNode node, bool optional)
  84. {
  85. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  86. }
  87. static string AttValue (string name, XmlNode node)
  88. {
  89. return HandlersUtil.ExtractAttributeValue (name, node, true);
  90. }
  91. static void ThrowException (string message, XmlNode node)
  92. {
  93. HandlersUtil.ThrowException (message, node);
  94. }
  95. //
  96. }
  97. }