SyndicationElementExtension.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // SyndicationElementExtension.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.IO;
  32. using System.Runtime.Serialization;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. namespace System.ServiceModel.Syndication
  37. {
  38. public class SyndicationElementExtension
  39. {
  40. ReadWriteHandler handler;
  41. public SyndicationElementExtension (object dataContractExtension)
  42. : this (dataContractExtension, (XmlObjectSerializer) null)
  43. {
  44. }
  45. public SyndicationElementExtension (object dataContractExtension, XmlObjectSerializer dataContractSerializer)
  46. : this (null, null, dataContractExtension, dataContractSerializer)
  47. {
  48. }
  49. public SyndicationElementExtension (string outerName, string outerNamespace, object dataContractExtension)
  50. : this (outerName, outerNamespace, dataContractExtension, null)
  51. {
  52. }
  53. public SyndicationElementExtension (string outerName, string outerNamespace, object dataContractExtension, XmlObjectSerializer dataContractSerializer)
  54. {
  55. if (dataContractExtension == null)
  56. throw new ArgumentNullException ("dataContractExtension");
  57. handler = new DataContractReadWriteHandler (outerName, outerNamespace, dataContractExtension, dataContractSerializer);
  58. }
  59. public SyndicationElementExtension (object xmlSerializerExtension, XmlSerializer serializer)
  60. {
  61. if (xmlSerializerExtension == null)
  62. throw new ArgumentNullException ("xmlSerializerExtension");
  63. handler = new XmlSerializationReadWriteHandler (xmlSerializerExtension, serializer);
  64. }
  65. public SyndicationElementExtension (XmlReader xmlReader)
  66. {
  67. if (xmlReader == null)
  68. throw new ArgumentNullException ("xmlReader");
  69. xmlReader.MoveToContent ();
  70. if (xmlReader.NodeType != XmlNodeType.Element)
  71. throw new XmlException ("Element node is expected on the argument xmlReader");
  72. handler = new XmlReaderReadWriteHandler (xmlReader);
  73. }
  74. public string OuterName {
  75. get { return handler != null ? handler.Name : null; }
  76. }
  77. public string OuterNamespace {
  78. get { return handler != null ? handler.Namespace : null; }
  79. }
  80. public TExtension GetObject<TExtension> ()
  81. {
  82. return GetObject<TExtension> (new DataContractSerializer (typeof (TExtension)));
  83. }
  84. public TExtension GetObject<TExtension> (XmlObjectSerializer serializer)
  85. {
  86. if (serializer == null)
  87. throw new ArgumentNullException ("serializer");
  88. return (TExtension) serializer.ReadObject (GetReader (), false);
  89. }
  90. public TExtension GetObject<TExtension> (XmlSerializer serializer)
  91. {
  92. if (serializer == null)
  93. throw new ArgumentNullException ("serializer");
  94. return (TExtension) serializer.Deserialize (GetReader ());
  95. }
  96. public XmlReader GetReader ()
  97. {
  98. return handler.GetReader ();
  99. }
  100. public void WriteTo (XmlWriter writer)
  101. {
  102. if (writer == null)
  103. throw new ArgumentNullException ("writer");
  104. handler.WriteTo (writer);
  105. }
  106. abstract class ReadWriteHandler
  107. {
  108. public string Name { get; protected set; }
  109. public string Namespace { get; protected set; }
  110. public virtual XmlReader GetReader ()
  111. {
  112. StringWriter sw = new StringWriter ();
  113. using (XmlWriter w = XmlWriter.Create (sw))
  114. WriteTo (w);
  115. return XmlReader.Create (new StringReader (sw.ToString ()));
  116. }
  117. public abstract void WriteTo (XmlWriter writer);
  118. }
  119. class DataContractReadWriteHandler : ReadWriteHandler
  120. {
  121. object extension;
  122. XmlObjectSerializer serializer;
  123. public DataContractReadWriteHandler (string name, string ns, object extension, XmlObjectSerializer serializer)
  124. {
  125. this.Name = name;
  126. this.Namespace = ns;
  127. this.extension = extension;
  128. this.serializer = serializer;
  129. if (this.serializer == null)
  130. this.serializer = new DataContractSerializer (extension.GetType ());
  131. }
  132. public override void WriteTo (XmlWriter writer)
  133. {
  134. if (Name != null) {
  135. writer.WriteStartElement (Name, Namespace);
  136. serializer.WriteObjectContent (writer, extension);
  137. writer.WriteFullEndElement ();
  138. }
  139. else
  140. serializer.WriteObject (writer, extension);
  141. }
  142. }
  143. class XmlSerializationReadWriteHandler : ReadWriteHandler
  144. {
  145. object extension;
  146. XmlSerializer serializer;
  147. public XmlSerializationReadWriteHandler (object extension, XmlSerializer serializer)
  148. {
  149. this.extension = extension;
  150. this.serializer = serializer;
  151. if (serializer == null)
  152. serializer = new XmlSerializer (extension.GetType ());
  153. }
  154. public override void WriteTo (XmlWriter writer)
  155. {
  156. serializer.Serialize (writer, extension);
  157. }
  158. }
  159. class XmlReaderReadWriteHandler : ReadWriteHandler
  160. {
  161. string xml;
  162. public XmlReaderReadWriteHandler (XmlReader reader)
  163. {
  164. reader.MoveToContent ();
  165. Name = reader.LocalName;
  166. Namespace = reader.NamespaceURI;
  167. xml = reader.ReadOuterXml ();
  168. }
  169. public override XmlReader GetReader ()
  170. {
  171. var r = XmlReader.Create (new StringReader (xml));
  172. r.MoveToContent ();
  173. return r;
  174. }
  175. public override void WriteTo (XmlWriter writer)
  176. {
  177. writer.WriteNode (GetReader (), false);
  178. }
  179. }
  180. }
  181. }