Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HtmlCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
7namespace Waher.Content.Html
8{
13 {
17 public HtmlCodec()
18 {
19 }
20
24 public const string DefaultContentType = "text/html";
25
29 public static readonly string[] HtmlContentTypes = new string[]
30 {
32 "application/xhtml+xml"
33 };
34
38 public static readonly string[] HtmlFileExtensions = new string[]
39 {
40 "htm",
41 "html",
42 "xhtml",
43 "xhtm"
44 };
45
49 public string[] ContentTypes => HtmlContentTypes;
50
55
62 public bool Decodes(string ContentType, out Grade Grade)
63 {
64 if (Array.IndexOf(HtmlContentTypes, ContentType) >= 0)
65 {
66 Grade = Grade.Ok;
67 return true;
68 }
69 else
70 {
71 Grade = Grade.NotAtAll;
72 return false;
73 }
74 }
75
86 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
87 {
88 string Html = CommonTypes.GetString(Data, Encoding);
89 return Task.FromResult<object>(new HtmlDocument(Html));
90 }
91
98 public bool TryGetContentType(string FileExtension, out string ContentType)
99 {
100 switch (FileExtension.ToLower())
101 {
102 case "htm":
103 case "html":
104 ContentType = DefaultContentType;
105 return true;
106
107 case "xhtml":
108 case "xhtm":
109 ContentType = "application/xhtml+xml";
110 return true;
111
112 default:
113 ContentType = string.Empty;
114 return false;
115 }
116 }
117
124 public bool TryGetFileExtension(string ContentType, out string FileExtension)
125 {
126 switch (ContentType.ToLower())
127 {
129 FileExtension = "html";
130 return true;
131
132 case "application/xhtml+xml":
133 FileExtension = "xhtml";
134 return true;
135
136 default:
137 FileExtension = string.Empty;
138 return false;
139 }
140 }
141
149 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
150 {
151 if (Object is HtmlDocument || (Object is string s && s.TrimEnd().EndsWith("</html>", StringComparison.OrdinalIgnoreCase)))
152 {
153 if (InternetContent.IsAccepted(HtmlContentTypes, AcceptedContentTypes))
154 {
155 Grade = Grade.Ok;
156 return true;
157 }
158 }
159
160 Grade = Grade.NotAtAll;
161 return false;
162 }
163
172 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
173 {
174 if (!InternetContent.IsAccepted(HtmlContentTypes, out string ContentType, AcceptedContentTypes))
175 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
176
177 string Html = null;
178 byte[] Bin;
179
180 if (Object is HtmlDocument HtmlDoc)
181 Html = HtmlDoc.HtmlText;
182 else if (Object is string s)
183 Html = s;
184
185 if (Html is null)
186 Bin = null;
187 else
188 {
189 if (Encoding is null)
190 {
191 ContentType += "; charset=utf-8";
192 Bin = Encoding.UTF8.GetBytes(Html);
193 }
194 else
195 {
196 ContentType += "; charset=" + Encoding.WebName;
197 Bin = Encoding.GetBytes(Html);
198 }
199 }
200
201 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
202 }
203 }
204}
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.
HTML encoder/decoder.
Definition: HtmlCodec.cs:13
static readonly string[] HtmlFileExtensions
HTML file extensions.
Definition: HtmlCodec.cs:38
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: HtmlCodec.cs:149
string[] FileExtensions
Supported file extensions.
Definition: HtmlCodec.cs:54
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: HtmlCodec.cs:62
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: HtmlCodec.cs:98
HtmlCodec()
HTML encoder/decoder.
Definition: HtmlCodec.cs:17
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: HtmlCodec.cs:86
string[] ContentTypes
Supported content types.
Definition: HtmlCodec.cs:49
static readonly string[] HtmlContentTypes
HTML content types.
Definition: HtmlCodec.cs:29
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: HtmlCodec.cs:172
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: HtmlCodec.cs:124
const string DefaultContentType
Default Content-Type for HTML: text/html
Definition: HtmlCodec.cs:24
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.
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