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;
3using System.Text;
4using System.Threading.Tasks;
8
9namespace Waher.Content.Html
10{
15 {
19 public HtmlCodec()
20 {
21 }
22
26 public const string DefaultContentType = "text/html";
27
31 public static readonly string[] HtmlContentTypes = new string[]
32 {
34 "application/xhtml+xml"
35 };
36
40 public static readonly string[] HtmlFileExtensions = new string[]
41 {
42 "htm",
43 "html",
44 "xhtml",
45 "xhtm"
46 };
47
51 public string[] ContentTypes => HtmlContentTypes;
52
57
64 public bool Decodes(string ContentType, out Grade Grade)
65 {
66 if (Array.IndexOf(HtmlContentTypes, ContentType) >= 0)
67 {
68 Grade = Grade.Ok;
69 return true;
70 }
71 else
72 {
73 Grade = Grade.NotAtAll;
74 return false;
75 }
76 }
77
88 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
89 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
90 {
91 string Html = Strings.GetString(Data, Encoding);
92 return Task.FromResult(new ContentResponse(ContentType, new HtmlDocument(Html), Data));
93 }
94
101 public bool TryGetContentType(string FileExtension, out string ContentType)
102 {
103 switch (FileExtension.ToLower())
104 {
105 case "htm":
106 case "html":
107 ContentType = DefaultContentType;
108 return true;
109
110 case "xhtml":
111 case "xhtm":
112 ContentType = "application/xhtml+xml";
113 return true;
114
115 default:
116 ContentType = string.Empty;
117 return false;
118 }
119 }
120
127 public bool TryGetFileExtension(string ContentType, out string FileExtension)
128 {
129 switch (ContentType.ToLower())
130 {
132 FileExtension = "html";
133 return true;
134
135 case "application/xhtml+xml":
136 FileExtension = "xhtml";
137 return true;
138
139 default:
140 FileExtension = string.Empty;
141 return false;
142 }
143 }
144
152 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
153 {
154 if (Object is HtmlDocument || (Object is string s && s.TrimEnd().EndsWith("</html>", StringComparison.OrdinalIgnoreCase)))
155 {
156 if (InternetContent.IsAccepted(HtmlContentTypes, AcceptedContentTypes))
157 {
158 Grade = Grade.Ok;
159 return true;
160 }
161 }
162
163 Grade = Grade.NotAtAll;
164 return false;
165 }
166
175 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
176 {
177 if (!InternetContent.IsAccepted(HtmlContentTypes, out string ContentType, AcceptedContentTypes))
178 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
179
180 string Html = null;
181 byte[] Bin;
182
183 if (Object is HtmlDocument HtmlDoc)
184 Html = HtmlDoc.HtmlText;
185 else if (Object is string s)
186 Html = s;
187
188 if (Html is null)
189 Bin = null;
190 else
191 {
192 if (Encoding is null)
193 {
194 ContentType += "; charset=utf-8";
195 Bin = Encoding.UTF8.GetBytes(Html);
196 }
197 else
198 {
199 ContentType += "; charset=" + Encoding.WebName;
200 Bin = Encoding.GetBytes(Html);
201 }
202 }
203
204 return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
205 }
206
212 public Grade Supports(Type ObjectType)
213 {
214 return ObjectType == typeof(HtmlDocument) ? Grade.Excellent : Grade.NotAtAll;
215 }
216
223 public void Encode(object Object, int? Indent, StringBuilder Json)
224 {
225 Json.Append('"');
226 Json.Append(JSON.Encode(((HtmlDocument)Object).HtmlText));
227 Json.Append('"');
228 }
229
230 }
231}
Contains information about a response to a content request.
HTML encoder/decoder.
Definition: HtmlCodec.cs:15
Grade Supports(Type ObjectType)
How well the JSON encoder encodes objects of type ObjectType .
Definition: HtmlCodec.cs:212
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: HtmlCodec.cs:88
void Encode(object Object, int? Indent, StringBuilder Json)
Encodes the Object to JSON.
Definition: HtmlCodec.cs:223
static readonly string[] HtmlFileExtensions
HTML file extensions.
Definition: HtmlCodec.cs:40
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: HtmlCodec.cs:152
string[] FileExtensions
Supported file extensions.
Definition: HtmlCodec.cs:56
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: HtmlCodec.cs:64
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: HtmlCodec.cs:101
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: HtmlCodec.cs:175
HtmlCodec()
HTML encoder/decoder.
Definition: HtmlCodec.cs:19
string[] ContentTypes
Supported content types.
Definition: HtmlCodec.cs:51
static readonly string[] HtmlContentTypes
HTML content types.
Definition: HtmlCodec.cs:31
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: HtmlCodec.cs:127
const string DefaultContentType
Default Content-Type for HTML: text/html
Definition: HtmlCodec.cs:26
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.
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
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...
Interface for encoding objects of certain types to JSON.
Definition: IJsonEncoder.cs:11
Grade
Grade enumeration
Definition: Grade.cs:7