SyndicationElementExtension.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 virtual string Name {
  109. get { return null; }
  110. }
  111. public virtual string Namespace {
  112. get { return null; }
  113. }
  114. public virtual XmlReader GetReader ()
  115. {
  116. StringWriter sw = new StringWriter ();
  117. using (XmlWriter w = XmlWriter.Create (sw))
  118. WriteTo (w);
  119. return XmlReader.Create (new StringReader (sw.ToString ()));
  120. }
  121. public abstract void WriteTo (XmlWriter writer);
  122. }
  123. class DataContractReadWriteHandler : ReadWriteHandler
  124. {
  125. string name, ns;
  126. object extension;
  127. XmlObjectSerializer serializer;
  128. public DataContractReadWriteHandler (string name, string ns, object extension, XmlObjectSerializer serializer)
  129. {
  130. this.name = name;
  131. this.ns = ns;
  132. this.extension = extension;
  133. this.serializer = serializer;
  134. if (this.serializer == null)
  135. this.serializer = new DataContractSerializer (extension.GetType ());
  136. }
  137. public override string Name {
  138. get { return name; }
  139. }
  140. public override string Namespace {
  141. get { return ns; }
  142. }
  143. public override void WriteTo (XmlWriter writer)
  144. {
  145. if (name != null) {
  146. writer.WriteStartElement (name, ns);
  147. serializer.WriteObjectContent (writer, extension);
  148. writer.WriteFullEndElement ();
  149. }
  150. else
  151. serializer.WriteObject (writer, extension);
  152. }
  153. }
  154. class XmlSerializationReadWriteHandler : ReadWriteHandler
  155. {
  156. object extension;
  157. XmlSerializer serializer;
  158. public XmlSerializationReadWriteHandler (object extension, XmlSerializer serializer)
  159. {
  160. this.extension = extension;
  161. this.serializer = serializer;
  162. if (serializer == null)
  163. serializer = new XmlSerializer (extension.GetType ());
  164. }
  165. public override void WriteTo (XmlWriter writer)
  166. {
  167. serializer.Serialize (writer, extension);
  168. }
  169. }
  170. class XmlReaderReadWriteHandler : ReadWriteHandler
  171. {
  172. XmlDocument doc = new XmlDocument ();
  173. public XmlReaderReadWriteHandler (XmlReader reader)
  174. {
  175. doc.AppendChild (doc.ReadNode (reader));
  176. }
  177. public override XmlReader GetReader ()
  178. {
  179. return new XmlNodeReader (doc.FirstChild);
  180. }
  181. public override void WriteTo (XmlWriter writer)
  182. {
  183. doc.FirstChild.WriteTo (writer);
  184. }
  185. }
  186. }
  187. }