1 //
2 // ResourceCollectionInfo.cs
3 //
4 // Author:
5 //	Atsushi Enomoto <atsushi@ximian.com>
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 
36 namespace System.ServiceModel.Syndication
37 {
38 	public class ResourceCollectionInfo
39 	{
ResourceCollectionInfo()40 		public ResourceCollectionInfo ()
41 		{
42 			Accepts = new Collection<string> ();
43 			Categories = new Collection<CategoriesDocument> ();
44 		}
45 
ResourceCollectionInfo(TextSyndicationContent title, Uri link)46 		public ResourceCollectionInfo (TextSyndicationContent title, Uri link)
47 			: this ()
48 		{
49 			Title = title;
50 			Link = link;
51 		}
52 
ResourceCollectionInfo(string title, Uri link)53 		public ResourceCollectionInfo (string title, Uri link)
54 			: this (new TextSyndicationContent (title), link)
55 		{
56 		}
57 
ResourceCollectionInfo(TextSyndicationContent title, Uri link, IEnumerable<CategoriesDocument> categories, bool allowsNewEntries)58 		public ResourceCollectionInfo (TextSyndicationContent title, Uri link, IEnumerable<CategoriesDocument> categories, bool allowsNewEntries)
59 			: this (title, link)
60 		{
61 			if (categories == null)
62 				throw new ArgumentNullException ("categories");
63 
64 			foreach (var c in categories)
65 				Categories.Add (c);
66 			allow_new_entries = allowsNewEntries;
67 		}
68 
ResourceCollectionInfo(TextSyndicationContent title, Uri link, IEnumerable<CategoriesDocument> categories, IEnumerable<string> accepts)69 		public ResourceCollectionInfo (TextSyndicationContent title, Uri link, IEnumerable<CategoriesDocument> categories, IEnumerable<string> accepts)
70 			: this (title, link, categories, true)
71 		{
72 			if (accepts == null)
73 				throw new ArgumentNullException ("accepts");
74 			foreach (var a in accepts)
75 				Accepts.Add (a);
76 		}
77 
78 		bool allow_new_entries;
79 		SyndicationExtensions extensions = new SyndicationExtensions ();
80 
81 		public Collection<string> Accepts { get; private set; }
82 
83 		public Dictionary<XmlQualifiedName, string> AttributeExtensions {
84 			get { return extensions.Attributes; }
85 		}
86 
87 		public Uri BaseUri { get; set; }
88 
89 		public Collection<CategoriesDocument> Categories { get; private set; }
90 
91 		public SyndicationElementExtensionCollection ElementExtensions {
92 			get { return extensions.Elements; }
93 		}
94 
95 		public Uri Link { get; set; }
96 
97 		public TextSyndicationContent Title { get; set; }
98 
99 
CreateInlineCategoriesDocument()100 		protected internal virtual InlineCategoriesDocument CreateInlineCategoriesDocument ()
101 		{
102 			return new InlineCategoriesDocument ();
103 		}
104 
CreateReferencedCategoriesDocument()105 		protected internal virtual ReferencedCategoriesDocument CreateReferencedCategoriesDocument ()
106 		{
107 			return new ReferencedCategoriesDocument ();
108 		}
109 
TryParseAttribute(string name, string ns, string value, string version)110 		protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
111 		{
112 			if (name == "base" && ns == Namespaces.Xml)
113 				BaseUri = new Uri (value, UriKind.RelativeOrAbsolute);
114 			else if (name == "href" && ns == String.Empty)
115 				Link = new Uri (value, UriKind.RelativeOrAbsolute);
116 			else
117 				return false;
118 			return true;
119 		}
120 
TryParseElement(XmlReader reader, string version)121 		protected internal virtual bool TryParseElement (XmlReader reader, string version)
122 		{
123 			if (reader == null)
124 				throw new ArgumentNullException ("reader");
125 			reader.MoveToContent ();
126 
127 			if (reader.LocalName != "collection" || reader.NamespaceURI != version)
128 				return false;
129 
130 			for (int i = 0; i < reader.AttributeCount; i++) {
131 				reader.MoveToAttribute (i);
132 				if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, version))
133 					AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
134 			}
135 			reader.MoveToElement ();
136 
137 			if (!reader.IsEmptyElement) {
138 				reader.Read ();
139 				for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
140 					if (reader.LocalName == "title" && reader.NamespaceURI == Namespaces.Atom10)
141 						Title = Atom10FeedFormatter.ReadTextSyndicationContent (reader);
142 					else
143 						ElementExtensions.Add (new SyndicationElementExtension (reader));
144 				}
145 			}
146 			reader.Read ();
147 			return true;
148 		}
149 
WriteAttributeExtensions(XmlWriter writer, string version)150 		protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
151 		{
152 			extensions.WriteAttributeExtensions (writer, version);
153 		}
154 
WriteElementExtensions(XmlWriter writer, string version)155 		protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
156 		{
157 			extensions.WriteElementExtensions (writer, version);
158 		}
159 	}
160 }
161