AtomPub10ServiceDocumentFormatter.cs 5.1 KB

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