Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PlainTextCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
8namespace Waher.Content.Text
9{
14 {
19 {
20 }
21
25 public const string DefaultContentType = "text/plain";
26
30 public static readonly string[] PlainTextContentTypes = new string[]
31 {
33 "text/sgml",
34 "text/richtext"
35 };
36
40 public static readonly string[] PlainTextFileExtensions = new string[]
41 {
42 "txt",
43 "text",
44 "sgml",
45 "rtx",
46 "log",
47 "ini",
48 "asc",
49 "me",
50 "readme",
51 "1st",
52 "license",
53 "faq",
54 "note",
55 "utf8",
56 "plain"
57 };
58
63
68
75 public bool Decodes(string ContentType, out Grade Grade)
76 {
77 if (ContentType == DefaultContentType)
78 {
79 Grade = Grade.Excellent;
80 return true;
81 }
82 else if (ContentType.StartsWith("text/"))
83 {
84 Grade = Grade.Barely;
85 return true;
86 }
87 else
88 {
89 Grade = Grade.NotAtAll;
90 return false;
91 }
92 }
93
104 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
105 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
106 {
107 return Task.FromResult(new ContentResponse(ContentType, Strings.GetString(Data, Encoding), Data));
108 }
109
116 public bool TryGetContentType(string FileExtension, out string ContentType)
117 {
118 switch (FileExtension.ToLower())
119 {
120 case "txt":
121 case "text":
122 case "log":
123 case "ini":
124 case "asc":
125 case "me":
126 case "readme":
127 case "1st":
128 case "license":
129 case "faq":
130 case "note":
131 case "utf8":
132 case "plain":
134 return true;
135
136 case "sgml":
137 ContentType = "text/sgml";
138 return true;
139
140 case "rtx":
141 ContentType = "text/richtext";
142 return true;
143
144 default:
145 ContentType = string.Empty;
146 return false;
147 }
148 }
149
156 public bool TryGetFileExtension(string ContentType, out string FileExtension)
157 {
158 switch (ContentType.ToLower())
159 {
161 FileExtension = "txt";
162 return true;
163
164 case "text/sgml":
165 FileExtension = "sgml";
166 return true;
167
168 case "text/richtext":
169 FileExtension = "rtx";
170 return true;
171
172 default:
173 FileExtension = string.Empty;
174 return false;
175 }
176 }
177
185 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
186 {
187 if (Object is string)
188 {
190 {
191 Grade = Grade.Ok;
192 return true;
193 }
194 else
195 {
196 foreach (string s in AcceptedContentTypes)
197 {
198 if (s.StartsWith("text/"))
199 {
200 Grade = Grade.Barely;
201 return true;
202 }
203 }
204 }
205 }
206
207 Grade = Grade.NotAtAll;
208 return false;
209 }
210
219 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
220 ICodecProgress Progress, params string[] AcceptedContentTypes)
221 {
222 string ContentType;
223
224 if (Encoding is null)
225 {
226 ContentType = DefaultContentType + "; charset=utf-8";
227 Encoding = Encoding.UTF8;
228 }
229 else
230 ContentType = DefaultContentType + "; charset=" + Encoding.WebName;
231
232 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(Object.ToString())));
233 }
234 }
235}
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.
Plain text encoder/decoder.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
const string DefaultContentType
text/plain
static readonly string[] PlainTextContentTypes
Plain text content types.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
string[] ContentTypes
Supported content types.
static readonly string[] PlainTextFileExtensions
Plain text file extensions.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
string[] FileExtensions
Supported file extensions.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
PlainTextCodec()
Plain text encoder/decoder.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
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