ExtensibleSyndicationObject.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Syndication
  5. {
  6. using System.Collections.ObjectModel;
  7. using System.Collections.Generic;
  8. using System.Runtime.Serialization;
  9. using System.Xml.Serialization;
  10. using System.Xml;
  11. // NOTE: This class implements Clone so if you add any members, please update the copy ctor
  12. struct ExtensibleSyndicationObject : IExtensibleSyndicationObject
  13. {
  14. Dictionary<XmlQualifiedName, string> attributeExtensions;
  15. SyndicationElementExtensionCollection elementExtensions;
  16. ExtensibleSyndicationObject(ExtensibleSyndicationObject source)
  17. {
  18. if (source.attributeExtensions != null)
  19. {
  20. this.attributeExtensions = new Dictionary<XmlQualifiedName, string>();
  21. foreach (XmlQualifiedName key in source.attributeExtensions.Keys)
  22. {
  23. this.attributeExtensions.Add(key, source.attributeExtensions[key]);
  24. }
  25. }
  26. else
  27. {
  28. this.attributeExtensions = null;
  29. }
  30. if (source.elementExtensions != null)
  31. {
  32. this.elementExtensions = new SyndicationElementExtensionCollection(source.elementExtensions);
  33. }
  34. else
  35. {
  36. this.elementExtensions = null;
  37. }
  38. }
  39. public Dictionary<XmlQualifiedName, string> AttributeExtensions
  40. {
  41. get
  42. {
  43. if (this.attributeExtensions == null)
  44. {
  45. this.attributeExtensions = new Dictionary<XmlQualifiedName, string>();
  46. }
  47. return this.attributeExtensions;
  48. }
  49. }
  50. public SyndicationElementExtensionCollection ElementExtensions
  51. {
  52. get
  53. {
  54. if (this.elementExtensions == null)
  55. {
  56. this.elementExtensions = new SyndicationElementExtensionCollection();
  57. }
  58. return this.elementExtensions;
  59. }
  60. }
  61. static XmlBuffer CreateXmlBuffer(XmlDictionaryReader unparsedExtensionsReader, int maxExtensionSize)
  62. {
  63. XmlBuffer buffer = new XmlBuffer(maxExtensionSize);
  64. using (XmlDictionaryWriter writer = buffer.OpenSection(unparsedExtensionsReader.Quotas))
  65. {
  66. writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
  67. while (unparsedExtensionsReader.IsStartElement())
  68. {
  69. writer.WriteNode(unparsedExtensionsReader, false);
  70. }
  71. writer.WriteEndElement();
  72. }
  73. buffer.CloseSection();
  74. buffer.Close();
  75. return buffer;
  76. }
  77. internal void LoadElementExtensions(XmlReader readerOverUnparsedExtensions, int maxExtensionSize)
  78. {
  79. if (readerOverUnparsedExtensions == null)
  80. {
  81. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("readerOverUnparsedExtensions");
  82. }
  83. if (maxExtensionSize < 0)
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxExtensionSize"));
  86. }
  87. XmlDictionaryReader r = XmlDictionaryReader.CreateDictionaryReader(readerOverUnparsedExtensions);
  88. this.elementExtensions = new SyndicationElementExtensionCollection(CreateXmlBuffer(r, maxExtensionSize));
  89. }
  90. internal void LoadElementExtensions(XmlBuffer buffer)
  91. {
  92. this.elementExtensions = new SyndicationElementExtensionCollection(buffer);
  93. }
  94. internal void WriteAttributeExtensions(XmlWriter writer)
  95. {
  96. if (writer == null)
  97. {
  98. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  99. }
  100. if (this.attributeExtensions != null)
  101. {
  102. foreach (XmlQualifiedName qname in this.attributeExtensions.Keys)
  103. {
  104. string value = this.attributeExtensions[qname];
  105. writer.WriteAttributeString(qname.Name, qname.Namespace, value);
  106. }
  107. }
  108. }
  109. internal void WriteElementExtensions(XmlWriter writer)
  110. {
  111. if (writer == null)
  112. {
  113. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  114. }
  115. if (this.elementExtensions != null)
  116. {
  117. this.elementExtensions.WriteTo(writer);
  118. }
  119. }
  120. public ExtensibleSyndicationObject Clone()
  121. {
  122. return new ExtensibleSyndicationObject(this);
  123. }
  124. }
  125. }