Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RssCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
10
11namespace Waher.Content.Rss
12{
21 {
28 public const string ContentType = "application/rss+xml";
29
33 public const string FileExtension = "rss";
34
35 private static readonly string[] contentTypes = new string[] { ContentType };
36 private static readonly string[] fileExtensions = new string[] { FileExtension };
37
41 public string[] ContentTypes => contentTypes;
42
46 public string[] FileExtensions => fileExtensions;
47
54 public bool TryGetContentType(string FileExtension, out string ContentType)
55 {
56 if (string.Compare(FileExtension, RssCodec.FileExtension, true) == 0)
57 {
59 return true;
60 }
61 else
62 {
63 ContentType = null;
64 return false;
65 }
66 }
67
74 public bool TryGetFileExtension(string ContentType, out string FileExtension)
75 {
76 if (string.Compare(ContentType, RssCodec.ContentType, true) == 0)
77 {
79 return true;
80 }
81 else
82 {
83 FileExtension = null;
84 return false;
85 }
86 }
87
94 public bool Decodes(string ContentType, out Grade Grade)
95 {
96 if (string.Compare(ContentType, RssCodec.ContentType, true) == 0)
97 {
98 Grade = Grade.Excellent;
99 return true;
100 }
101 else
102 {
103 Grade = Grade.NotAtAll;
104 return false;
105 }
106 }
107
115 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
116 {
117 if (Object is RssDocument)
118 {
119 if (InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
120 {
121 Grade = Grade.Excellent;
122 return true;
123 }
124 }
125
126 Grade = Grade.NotAtAll;
127 return false;
128 }
129
140 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
141 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
142 {
143 string Xml = Strings.GetString(Data, Encoding);
144 XmlDocument Doc = XML.ParseXml(Xml, true);
145
146 return Task.FromResult(new ContentResponse(ContentType, new RssDocument(Doc, BaseUri), Data));
147 }
148
157 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
158 {
159 if (!(Object is RssDocument Doc) ||
160 !InternetContent.IsAccepted(contentTypes, out string ContentType, AcceptedContentTypes))
161 {
162 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
163 }
164
165 return XmlCodec.EncodeXmlAsync(Doc.Xml, Encoding, ContentType);
166 }
167 }
168}
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Encodes and Decodes RSS documents.
Definition: RssCodec.cs:21
string[] ContentTypes
Supported content types.
Definition: RssCodec.cs:41
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: RssCodec.cs:115
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: RssCodec.cs:54
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: RssCodec.cs:74
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: RssCodec.cs:140
const string ContentType
application/rss+xml
Definition: RssCodec.cs:28
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: RssCodec.cs:157
const string FileExtension
rss
Definition: RssCodec.cs:33
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: RssCodec.cs:94
string[] FileExtensions
Supported file extensions.
Definition: RssCodec.cs:46
Contains information from an RSS feed.
Definition: RssDocument.cs:13
XML encoder/decoder.
Definition: XmlCodec.cs:19
static Task< ContentResponse > EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
Definition: XmlCodec.cs:278
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Static class managing binary representations of strings.
Definition: Strings.cs:10
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7