AtomPub10ServiceDocumentFormatter.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // AtomPub10ServiceDocumentFormatter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 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.IO;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Channels;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. namespace System.ServiceModel.Syndication
  37. {
  38. class Namespaces
  39. {
  40. public const string Xml = "http://www.w3.org/XML/1998/namespace";
  41. public const string Xmlns = "http://www.w3.org/2000/xmlns/";
  42. public const string AtomPP = "http://www.w3.org/2007/app";
  43. public const string Atom10 = "http://www.w3.org/2005/Atom";
  44. }
  45. [XmlRoot ("service", Namespace = Namespaces.AtomPP)]
  46. public class AtomPub10ServiceDocumentFormatter : ServiceDocumentFormatter, IXmlSerializable
  47. {
  48. public AtomPub10ServiceDocumentFormatter ()
  49. {
  50. }
  51. public AtomPub10ServiceDocumentFormatter (ServiceDocument documentToWrite)
  52. : base (documentToWrite)
  53. {
  54. }
  55. public AtomPub10ServiceDocumentFormatter (Type documentTypeToCreate)
  56. {
  57. doc_type = documentTypeToCreate;
  58. }
  59. Type doc_type;
  60. public override string Version {
  61. get { return Namespaces.AtomPP; }
  62. }
  63. public override bool CanRead (XmlReader reader)
  64. {
  65. if (reader == null)
  66. throw new ArgumentNullException ("reader");
  67. reader.MoveToContent ();
  68. return reader.LocalName == "service" && reader.NamespaceURI == Version;
  69. }
  70. protected override ServiceDocument CreateDocumentInstance ()
  71. {
  72. var doc = doc_type != null ? (ServiceDocument) Activator.CreateInstance (doc_type, new object [0]) : base.CreateDocumentInstance ();
  73. doc.InternalFormatter = this;
  74. return doc;
  75. }
  76. public override void ReadFrom (XmlReader reader)
  77. {
  78. if (!Document.TryParseElement (reader, Version))
  79. throw new XmlException (String.Format ("Unexpected element '{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
  80. }
  81. public override void WriteTo (XmlWriter writer)
  82. {
  83. if (writer == null)
  84. throw new ArgumentNullException ("writer");
  85. writer.WriteStartElement ("app", "service", Version);
  86. writer.WriteAttributeString ("xmlns", "a10", Namespaces.Xmlns, Namespaces.Atom10);
  87. writer.WriteAttributeString ("xmlns", "app", Namespaces.Xmlns, Version);
  88. // xml:lang, xml:base, workspace*
  89. if (Document.Language != null)
  90. writer.WriteAttributeString ("xml", "lang", Namespaces.Xml, Document.Language);
  91. if (Document.BaseUri != null)
  92. writer.WriteAttributeString ("xml", "base", Namespaces.Xml, Document.BaseUri.ToString ());
  93. Document.WriteAttributeExtensions (writer, Version);
  94. Document.WriteElementExtensions (writer, Version);
  95. foreach (var ws in Document.Workspaces) {
  96. writer.WriteStartElement ("app", "workspace", Version);
  97. // xml:base, title, collection*
  98. if (ws.BaseUri != null)
  99. writer.WriteAttributeString ("xml", "base", Namespaces.Xml, ws.BaseUri.ToString ());
  100. ws.WriteAttributeExtensions (writer, Version);
  101. ws.WriteElementExtensions (writer, Version);
  102. if (ws.Title != null)
  103. ws.Title.WriteTo (writer, "title", Namespaces.Atom10);
  104. foreach (var rc in ws.Collections) {
  105. writer.WriteStartElement ("app", "collection", Version);
  106. // accept*, xml:base, category, @href, title
  107. if (rc.BaseUri != null)
  108. writer.WriteAttributeString ("xml", "base", Namespaces.Xml, rc.BaseUri.ToString ());
  109. if (rc.Link != null)
  110. writer.WriteAttributeString ("href", rc.Link.ToString ());
  111. rc.WriteAttributeExtensions (writer, Version);
  112. if (rc.Title != null)
  113. rc.Title.WriteTo (writer, "title", Namespaces.Atom10);
  114. foreach (var s in rc.Accepts) {
  115. writer.WriteStartElement ("app", "accept", Version);
  116. writer.WriteString (s);
  117. writer.WriteEndElement ();
  118. }
  119. foreach (var cat in rc.Categories)
  120. cat.Save (writer);
  121. writer.WriteEndElement ();
  122. }
  123. writer.WriteEndElement ();
  124. }
  125. writer.WriteEndElement ();
  126. }
  127. XmlSchema IXmlSerializable.GetSchema ()
  128. {
  129. return null;
  130. }
  131. void IXmlSerializable.ReadXml (XmlReader reader)
  132. {
  133. ReadFrom (reader);
  134. }
  135. void IXmlSerializable.WriteXml (XmlWriter writer)
  136. {
  137. WriteTo (writer);
  138. }
  139. }
  140. }