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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
7namespace Waher.Content.Text
8{
13 {
18 {
19 }
20
24 public const string DefaultContentType = "text/plain";
25
29 public static readonly string[] PlainTextContentTypes = new string[]
30 {
32 "text/sgml",
33 "text/richtext"
34 };
35
39 public static readonly string[] PlainTextFileExtensions = new string[]
40 {
41 "txt",
42 "text",
43 "sgml",
44 "rtx",
45 "log",
46 "ini",
47 "asc",
48 "me",
49 "readme",
50 "1st",
51 "license",
52 "faq",
53 "note",
54 "utf8",
55 "plain"
56 };
57
62
67
74 public bool Decodes(string ContentType, out Grade Grade)
75 {
76 if (ContentType == DefaultContentType)
77 {
78 Grade = Grade.Excellent;
79 return true;
80 }
81 else if (ContentType.StartsWith("text/"))
82 {
83 Grade = Grade.Barely;
84 return true;
85 }
86 else
87 {
88 Grade = Grade.NotAtAll;
89 return false;
90 }
91 }
92
103 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
104 {
105 return Task.FromResult<object>(CommonTypes.GetString(Data, Encoding));
106 }
107
114 public bool TryGetContentType(string FileExtension, out string ContentType)
115 {
116 switch (FileExtension.ToLower())
117 {
118 case "txt":
119 case "text":
120 case "log":
121 case "ini":
122 case "asc":
123 case "me":
124 case "readme":
125 case "1st":
126 case "license":
127 case "faq":
128 case "note":
129 case "utf8":
130 case "plain":
132 return true;
133
134 case "sgml":
135 ContentType = "text/sgml";
136 return true;
137
138 case "rtx":
139 ContentType = "text/richtext";
140 return true;
141
142 default:
143 ContentType = string.Empty;
144 return false;
145 }
146 }
147
154 public bool TryGetFileExtension(string ContentType, out string FileExtension)
155 {
156 switch (ContentType.ToLower())
157 {
159 FileExtension = "txt";
160 return true;
161
162 case "text/sgml":
163 FileExtension = "sgml";
164 return true;
165
166 case "text/richtext":
167 FileExtension = "rtx";
168 return true;
169
170 default:
171 FileExtension = string.Empty;
172 return false;
173 }
174 }
175
183 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
184 {
185 if (Object is string)
186 {
188 {
189 Grade = Grade.Ok;
190 return true;
191 }
192 else
193 {
194 foreach (string s in AcceptedContentTypes)
195 {
196 if (s.StartsWith("text/"))
197 {
198 Grade = Grade.Barely;
199 return true;
200 }
201 }
202 }
203 }
204
205 Grade = Grade.NotAtAll;
206 return false;
207 }
208
217 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
218 {
219 string ContentType;
220
221 if (Encoding is null)
222 {
223 ContentType = PlainTextCodec.DefaultContentType + "; charset=utf-8";
224 Encoding = Encoding.UTF8;
225 }
226 else
227 ContentType = PlainTextCodec.DefaultContentType + "; charset=" + Encoding.WebName;
228
229 return Task.FromResult(new KeyValuePair<byte[], string>(Encoding.GetBytes(Object.ToString()), ContentType));
230 }
231 }
232}
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.
Plain text encoder/decoder.
const string DefaultContentType
text/plain
static readonly string[] PlainTextContentTypes
Plain text content types.
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
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.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
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.
PlainTextCodec()
Plain text encoder/decoder.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
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