ServiceDocument.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ServiceDocument.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.Collections.ObjectModel;
  31. using System.IO;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.Xml;
  35. namespace System.ServiceModel.Syndication
  36. {
  37. public class ServiceDocument
  38. {
  39. public static TServiceDocument Load<TServiceDocument> (XmlReader reader)
  40. where TServiceDocument : ServiceDocument, new()
  41. {
  42. var doc = new TServiceDocument ();
  43. new AtomPub10ServiceDocumentFormatter<TServiceDocument> (doc).ReadFrom (reader);
  44. return doc;
  45. }
  46. public static ServiceDocument Load (XmlReader reader)
  47. {
  48. return Load<ServiceDocument> (reader);
  49. }
  50. public ServiceDocument ()
  51. {
  52. Workspaces = new Collection<Workspace> ();
  53. }
  54. public ServiceDocument (IEnumerable<Workspace> workspaces)
  55. : this ()
  56. {
  57. if (workspaces != null)
  58. foreach (var w in workspaces)
  59. Workspaces.Add (w);
  60. }
  61. ServiceDocumentFormatter formatter;
  62. SyndicationExtensions extensions = new SyndicationExtensions ();
  63. internal ServiceDocumentFormatter InternalFormatter {
  64. set { formatter = value; }
  65. }
  66. public Dictionary<XmlQualifiedName, string> AttributeExtensions {
  67. get { return extensions.Attributes; }
  68. }
  69. public Uri BaseUri { get; set; }
  70. public SyndicationElementExtensionCollection ElementExtensions {
  71. get { return extensions.Elements; }
  72. }
  73. public string Language { get; set; }
  74. public Collection<Workspace> Workspaces { get; private set; }
  75. protected internal virtual Workspace CreateWorkspace ()
  76. {
  77. return new Workspace ();
  78. }
  79. public ServiceDocumentFormatter GetFormatter ()
  80. {
  81. if (formatter == null)
  82. formatter = new AtomPub10ServiceDocumentFormatter (this);
  83. return formatter;
  84. }
  85. public void Save (XmlWriter writer)
  86. {
  87. GetFormatter ().WriteTo (writer);
  88. }
  89. protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
  90. {
  91. if (version != "http://www.w3.org/2007/app")
  92. return false;
  93. switch (ns) {
  94. case "http://www.w3.org/XML/1998/namespace":
  95. switch (name) {
  96. case "base":
  97. BaseUri = new Uri (value, UriKind.RelativeOrAbsolute);
  98. return true;
  99. case "lang":
  100. Language = value;
  101. return true;
  102. }
  103. return false;
  104. }
  105. return false;
  106. }
  107. protected internal virtual bool TryParseElement (XmlReader reader, string version)
  108. {
  109. if (reader == null)
  110. throw new ArgumentNullException ("reader");
  111. reader.MoveToContent ();
  112. if (reader.LocalName != "service" || reader.NamespaceURI != version)
  113. return false;
  114. for (int i = 0; i < reader.AttributeCount; i++) {
  115. reader.MoveToAttribute (i);
  116. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, version))
  117. AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  118. }
  119. reader.MoveToElement ();
  120. if (reader.IsEmptyElement)
  121. throw new XmlException ("AtomPP service element requires at least one workspace element");
  122. reader.ReadStartElement ();
  123. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  124. if (reader.LocalName == "workspace" && reader.NamespaceURI == version) {
  125. var ws = CreateWorkspace ();
  126. if (ws.TryParseElement (reader, version)) {
  127. Workspaces.Add (ws);
  128. continue;
  129. }
  130. }
  131. ElementExtensions.Add (new SyndicationElementExtension (reader));
  132. }
  133. reader.ReadEndElement ();
  134. return true;
  135. }
  136. protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
  137. {
  138. extensions.WriteAttributeExtensions (writer, version);
  139. }
  140. protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
  141. {
  142. extensions.WriteElementExtensions (writer, version);
  143. }
  144. }
  145. }