Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PlantUmlCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
9{
14 {
19 {
20 }
21
25 public const string DefaultContentType = "text/vnd.plantuml";
26
30 public static readonly string[] PlantUmlContentTypes = new string[]
31 {
33 };
34
38 public static readonly string[] PlantUmlFileExtensions = new string[]
39 {
40 "uml"
41 };
42
47
52
59 public bool Decodes(string ContentType, out Grade Grade)
60 {
61 if (Array.IndexOf(PlantUmlContentTypes, ContentType) >= 0)
62 {
63 Grade = Grade.Barely;
64 return true;
65 }
66 else
67 {
68 Grade = Grade.NotAtAll;
69 return false;
70 }
71 }
72
83 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
84 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
85 {
86 string PlantUml = Strings.GetString(Data, Encoding);
87 return Task.FromResult(new ContentResponse(ContentType, new PlantUmlDocument(PlantUml), Data));
88 }
89
96 public bool TryGetContentType(string FileExtension, out string ContentType)
97 {
98 switch (FileExtension.ToLower())
99 {
100 case "uml":
101 ContentType = PlantUmlContentTypes[0];
102 return true;
103
104 default:
105 ContentType = string.Empty;
106 return false;
107 }
108 }
109
116 public bool TryGetFileExtension(string ContentType, out string FileExtension)
117 {
118 switch (ContentType.ToLower())
119 {
121 FileExtension = PlantUmlFileExtensions[0];
122 return true;
123
124 default:
125 FileExtension = string.Empty;
126 return false;
127 }
128 }
129
137 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
138 {
139 if (Object is PlantUmlDocument)
140 {
141 if (InternetContent.IsAccepted(PlantUmlContentTypes, AcceptedContentTypes))
142 {
143 Grade = Grade.Barely;
144 return true;
145 }
146 }
147
148 Grade = Grade.NotAtAll;
149 return false;
150 }
151
160 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
161 {
162 if (!InternetContent.IsAccepted(PlantUmlContentTypes, out string ContentType, AcceptedContentTypes))
163 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
164
165 string s;
166
167 if (Object is PlantUmlDocument PlantUmlDoc)
168 s = PlantUmlDoc.GraphDescription;
169 else
170 s = Object.ToString();
171
172 if (Encoding is null)
173 {
174 ContentType += "; charset=utf-8";
175 Encoding = Encoding.UTF8;
176 }
177 else
178 ContentType += "; charset=" + Encoding.WebName;
179
180 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(s)));
181 }
182 }
183}
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.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
static readonly string[] PlantUmlFileExtensions
Plain text file extensions.
PlantUmlCodec()
PlantUML encoder/decoder.
string[] ContentTypes
Supported content types.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
static readonly string[] PlantUmlContentTypes
PlantUml content types.
const string DefaultContentType
text/vnd.graphviz
string[] FileExtensions
Supported file extensions.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Class managing PlantUML integration into Markdown documents.
Definition: PlantUml.cs:56
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