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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
8
9namespace Waher.Content.Rss
10{
19 {
26 public const string ContentType = "application/rss+xml";
27
31 public const string FileExtension = "rss";
32
33 private static readonly string[] contentTypes = new string[] { ContentType };
34 private static readonly string[] fileExtensions = new string[] { FileExtension };
35
39 public string[] ContentTypes => contentTypes;
40
44 public string[] FileExtensions => fileExtensions;
45
52 public bool TryGetContentType(string FileExtension, out string ContentType)
53 {
54 if (string.Compare(FileExtension, RssCodec.FileExtension, true) == 0)
55 {
57 return true;
58 }
59 else
60 {
61 ContentType = null;
62 return false;
63 }
64 }
65
72 public bool TryGetFileExtension(string ContentType, out string FileExtension)
73 {
74 if (string.Compare(ContentType, RssCodec.ContentType, true) == 0)
75 {
77 return true;
78 }
79 else
80 {
81 FileExtension = null;
82 return false;
83 }
84 }
85
92 public bool Decodes(string ContentType, out Grade Grade)
93 {
94 if (string.Compare(ContentType, RssCodec.ContentType, true) == 0)
95 {
96 Grade = Grade.Excellent;
97 return true;
98 }
99 else
100 {
101 Grade = Grade.NotAtAll;
102 return false;
103 }
104 }
105
113 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
114 {
115 if (Object is RssDocument)
116 {
117 if (InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
118 {
119 Grade = Grade.Excellent;
120 return true;
121 }
122 }
123
124 Grade = Grade.NotAtAll;
125 return false;
126 }
127
138 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
139 {
140 string Xml = CommonTypes.GetString(Data, Encoding);
141 XmlDocument Doc = new XmlDocument()
142 {
143 PreserveWhitespace = true
144 };
145
146 Doc.LoadXml(Xml);
147
148 return Task.FromResult<object>(new RssDocument(Doc, BaseUri));
149 }
150
159 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
160 {
161 if (!(Object is RssDocument Doc) ||
162 !InternetContent.IsAccepted(contentTypes, out string ContentType, AcceptedContentTypes))
163 {
164 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
165 }
166
167 return XmlCodec.EncodeXmlAsync(Doc.Xml, Encoding, ContentType);
168 }
169 }
170}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
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:19
string[] ContentTypes
Supported content types.
Definition: RssCodec.cs:39
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: RssCodec.cs:113
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: RssCodec.cs:52
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: RssCodec.cs:72
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: RssCodec.cs:159
const string ContentType
application/rss+xml
Definition: RssCodec.cs:26
const string FileExtension
rss
Definition: RssCodec.cs:31
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: RssCodec.cs:92
string[] FileExtensions
Supported file extensions.
Definition: RssCodec.cs:44
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: RssCodec.cs:138
Contains information from an RSS feed.
Definition: RssDocument.cs:13
XML encoder/decoder.
Definition: XmlCodec.cs:16
static Task< KeyValuePair< byte[], string > > EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
Definition: XmlCodec.cs:247
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