SettingsMapping.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Web.Util.SettingsMapping
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007 Novell, Inc
  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. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.Xml;
  34. using System.Xml.XPath;
  35. namespace System.Web.Util
  36. {
  37. internal enum SettingsMappingPlatform
  38. {
  39. Windows,
  40. Unix
  41. };
  42. internal class SettingsMapping
  43. {
  44. string _sectionTypeName;
  45. Type _sectionType;
  46. string _mapperTypeName;
  47. Type _mapperType;
  48. SettingsMappingPlatform _platform;
  49. List <SettingsMappingWhat> _whats;
  50. public Type SectionType {
  51. get {
  52. if (_sectionType == null)
  53. _sectionType = Type.GetType (_sectionTypeName, false);
  54. return _sectionType;
  55. }
  56. }
  57. public Type MapperType {
  58. get {
  59. if (_mapperType == null) {
  60. _mapperType = Type.GetType (_mapperTypeName, true);
  61. if (!typeof (ISectionSettingsMapper).IsAssignableFrom (_mapperType)) {
  62. _mapperType = null;
  63. throw new InvalidOperationException ("Mapper type does not implement the ISectionSettingsMapper interface");
  64. }
  65. }
  66. return _mapperType;
  67. }
  68. }
  69. public SettingsMappingPlatform Platform {
  70. get { return _platform; }
  71. }
  72. public SettingsMapping (XPathNavigator nav)
  73. {
  74. _sectionTypeName = nav.GetAttribute ("sectionType", String.Empty);
  75. _mapperTypeName = nav.GetAttribute ("mapperType", String.Empty);
  76. EnumConverter cvt = new EnumConverter (typeof (SettingsMappingPlatform));
  77. _platform = (SettingsMappingPlatform) cvt.ConvertFromInvariantString (nav.GetAttribute ("platform", String.Empty));
  78. LoadContents (nav);
  79. }
  80. public object MapSection (object input, Type type)
  81. {
  82. if (type != SectionType)
  83. throw new ArgumentException ("type", "Invalid section type for this mapper");
  84. ISectionSettingsMapper mapper = Activator.CreateInstance (MapperType) as ISectionSettingsMapper;
  85. if (mapper == null)
  86. return input;
  87. return mapper.MapSection (input, _whats);
  88. }
  89. void LoadContents (XPathNavigator nav)
  90. {
  91. XPathNodeIterator iter = nav.Select ("./what[string-length (@value) > 0]");
  92. _whats = new List <SettingsMappingWhat> ();
  93. while (iter.MoveNext ())
  94. _whats.Add (new SettingsMappingWhat (iter.Current));
  95. }
  96. }
  97. }
  98. #endif