XmlSerializerSection.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if CONFIGURATION_DEP
  2. namespace System.Xml.Serialization.Configuration
  3. {
  4. using System;
  5. using System.IO;
  6. using System.Web;
  7. using System.Configuration;
  8. using System.ComponentModel;
  9. using System.Globalization;
  10. using System.Reflection;
  11. using System.Resources;
  12. public sealed class XmlSerializerSection : ConfigurationSection
  13. {
  14. public XmlSerializerSection()
  15. {
  16. this.properties.Add(this.checkDeserializeAdvances);
  17. this.properties.Add(this.tempFilesLocation);
  18. this.properties.Add(this.useLegacySerializerGeneration);
  19. }
  20. protected override ConfigurationPropertyCollection Properties
  21. {
  22. get
  23. {
  24. return this.properties;
  25. }
  26. }
  27. [ConfigurationProperty(ConfigurationStrings.CheckDeserializeAdvances, DefaultValue = false)]
  28. public bool CheckDeserializeAdvances
  29. {
  30. get { return (bool)this[this.checkDeserializeAdvances]; }
  31. set { this[this.checkDeserializeAdvances] = value; }
  32. }
  33. [ConfigurationProperty(ConfigurationStrings.TempFilesLocation, DefaultValue = null)]
  34. public string TempFilesLocation
  35. {
  36. get { return (string)this[this.tempFilesLocation]; }
  37. set { this[this.tempFilesLocation] = value; }
  38. }
  39. [ConfigurationProperty(ConfigurationStrings.UseLegacySerializerGeneration, DefaultValue = false)]
  40. public bool UseLegacySerializerGeneration
  41. {
  42. get { return (bool)this[this.useLegacySerializerGeneration]; }
  43. set { this[this.useLegacySerializerGeneration] = value; }
  44. }
  45. ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
  46. // Supply a type converter, even though it's a plain type converter, to get around ConfigurationProperty's internal
  47. // Enum conversion routine. The internal one is case-sensitive, we want this to be case-insensitive.
  48. readonly ConfigurationProperty checkDeserializeAdvances =
  49. new ConfigurationProperty(ConfigurationStrings.CheckDeserializeAdvances, typeof(bool), false,
  50. ConfigurationPropertyOptions.None);
  51. readonly ConfigurationProperty tempFilesLocation =
  52. new ConfigurationProperty(ConfigurationStrings.TempFilesLocation, typeof(string), null, null,
  53. new RootedPathValidator(),
  54. ConfigurationPropertyOptions.None);
  55. readonly ConfigurationProperty useLegacySerializerGeneration =
  56. new ConfigurationProperty(ConfigurationStrings.UseLegacySerializerGeneration, typeof(bool), false,
  57. ConfigurationPropertyOptions.None);
  58. }
  59. public class RootedPathValidator : ConfigurationValidatorBase
  60. {
  61. public override bool CanValidate(Type type)
  62. {
  63. return (type == typeof(string));
  64. }
  65. public override void Validate(object value)
  66. {
  67. string tempDirectory = value as string;
  68. if (string.IsNullOrEmpty(tempDirectory))
  69. return;
  70. tempDirectory = tempDirectory.Trim();
  71. if (string.IsNullOrEmpty(tempDirectory))
  72. return;
  73. if (!Path.IsPathRooted(tempDirectory))
  74. {
  75. // Make sure the path is not relative (VSWhidbey 260075)
  76. throw new ConfigurationErrorsException();
  77. }
  78. char firstChar = tempDirectory[0];
  79. if (firstChar == Path.DirectorySeparatorChar || firstChar == Path.AltDirectorySeparatorChar)
  80. {
  81. // Make sure the path is explicitly rooted
  82. throw new ConfigurationErrorsException();
  83. }
  84. }
  85. }
  86. }
  87. #endif