SyndicationLink.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // SyndicationLink.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. namespace System.ServiceModel.Syndication
  36. {
  37. public class SyndicationLink : ISyndicationElement
  38. {
  39. #region Static members
  40. public static SyndicationLink CreateAlternateLink (Uri uri)
  41. {
  42. return CreateAlternateLink (uri, null);
  43. }
  44. public static SyndicationLink CreateAlternateLink (Uri uri, string mediaType)
  45. {
  46. return new SyndicationLink (uri, "alternate", null, mediaType, 0);
  47. }
  48. public static SyndicationLink CreateMediaEnclosureLink (Uri uri, string mediaType, long length)
  49. {
  50. return new SyndicationLink (uri, "enclosure", null, mediaType, length);
  51. }
  52. public static SyndicationLink CreateSelfLink (Uri uri)
  53. {
  54. return CreateSelfLink (uri, null);
  55. }
  56. public static SyndicationLink CreateSelfLink (Uri uri, string mediaType)
  57. {
  58. return new SyndicationLink (uri, "self", null, mediaType, 0);
  59. }
  60. #endregion
  61. #region Instance members
  62. Uri base_uri, href;
  63. long length;
  64. string rel, title, type;
  65. SyndicationExtensions extensions = new SyndicationExtensions ();
  66. public SyndicationLink ()
  67. {
  68. }
  69. public SyndicationLink (Uri uri)
  70. {
  71. Uri = uri;
  72. }
  73. public SyndicationLink (Uri uri, string relationshipType, string title, string mediaType, long length)
  74. {
  75. Uri = uri;
  76. RelationshipType = relationshipType;
  77. Title = title;
  78. MediaType = mediaType;
  79. Length = length;
  80. }
  81. protected SyndicationLink (SyndicationLink source)
  82. {
  83. if (source == null)
  84. throw new ArgumentNullException ("source");
  85. base_uri = source.base_uri;
  86. href = source.href;
  87. length = source.length;
  88. rel = source.rel;
  89. title = source.title;
  90. type = source.type;
  91. extensions = source.extensions.Clone ();
  92. }
  93. public Dictionary<XmlQualifiedName, string> AttributeExtensions {
  94. get { return extensions.Attributes; }
  95. }
  96. public SyndicationElementExtensionCollection ElementExtensions {
  97. get { return extensions.Elements; }
  98. }
  99. public Uri BaseUri {
  100. get { return base_uri; }
  101. set {
  102. if (value != null && !value.IsAbsoluteUri)
  103. throw new ArgumentException ("Base URI must not be relative");
  104. base_uri = value;
  105. }
  106. }
  107. public long Length {
  108. get { return length; }
  109. set {
  110. if (value < 0)
  111. throw new ArgumentOutOfRangeException ("value");
  112. length = value;
  113. }
  114. }
  115. public string MediaType {
  116. get { return type; }
  117. set { type = value; }
  118. }
  119. public string RelationshipType {
  120. get { return rel; }
  121. set { rel = value; }
  122. }
  123. public string Title {
  124. get { return title; }
  125. set { title = value; }
  126. }
  127. public Uri Uri {
  128. get { return href; }
  129. set { href = value; }
  130. }
  131. public virtual SyndicationLink Clone ()
  132. {
  133. return new SyndicationLink (this);
  134. }
  135. protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
  136. {
  137. return false;
  138. }
  139. protected internal virtual bool TryParseElement (XmlReader reader, string version)
  140. {
  141. return false;
  142. }
  143. protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
  144. {
  145. extensions.WriteAttributeExtensions (writer, version);
  146. }
  147. protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
  148. {
  149. extensions.WriteElementExtensions (writer, version);
  150. }
  151. public Uri GetAbsoluteUri ()
  152. {
  153. if (href == null)
  154. return null;
  155. // our BaseUri is always absolute (unlike .NET).
  156. return base_uri != null ? new Uri (base_uri, href.ToString ()) : href.IsAbsoluteUri ? href : null;
  157. }
  158. #endregion
  159. }
  160. }