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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
18 {
19 }
20
24 public const string DefaultContentType = "text/vnd.plantuml";
25
29 public static readonly string[] PlantUmlContentTypes = new string[]
30 {
32 };
33
37 public static readonly string[] PlantUmlFileExtensions = new string[]
38 {
39 "uml"
40 };
41
46
51
58 public bool Decodes(string ContentType, out Grade Grade)
59 {
60 if (Array.IndexOf(PlantUmlContentTypes, ContentType) >= 0)
61 {
62 Grade = Grade.Barely;
63 return true;
64 }
65 else
66 {
67 Grade = Grade.NotAtAll;
68 return false;
69 }
70 }
71
82 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
83 {
84 string PlantUml = CommonTypes.GetString(Data, Encoding);
85 return Task.FromResult<object>(new PlantUmlDocument(PlantUml));
86 }
87
94 public bool TryGetContentType(string FileExtension, out string ContentType)
95 {
96 switch (FileExtension.ToLower())
97 {
98 case "uml":
99 ContentType = PlantUmlContentTypes[0];
100 return true;
101
102 default:
103 ContentType = string.Empty;
104 return false;
105 }
106 }
107
114 public bool TryGetFileExtension(string ContentType, out string FileExtension)
115 {
116 switch (ContentType.ToLower())
117 {
119 FileExtension = PlantUmlFileExtensions[0];
120 return true;
121
122 default:
123 FileExtension = string.Empty;
124 return false;
125 }
126 }
127
135 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
136 {
137 if (Object is PlantUmlDocument)
138 {
139 if (InternetContent.IsAccepted(PlantUmlContentTypes, AcceptedContentTypes))
140 {
141 Grade = Grade.Barely;
142 return true;
143 }
144 }
145
146 Grade = Grade.NotAtAll;
147 return false;
148 }
149
158 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
159 {
160 if (!InternetContent.IsAccepted(PlantUmlContentTypes, out string ContentType, AcceptedContentTypes))
161 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
162
163 string s;
164
165 if (Object is PlantUmlDocument PlantUmlDoc)
166 s = PlantUmlDoc.GraphDescription;
167 else
168 s = Object.ToString();
169
170 if (Encoding is null)
171 {
172 ContentType += "; charset=utf-8";
173 Encoding = Encoding.UTF8;
174 }
175 else
176 ContentType += "; charset=" + Encoding.WebName;
177
178 return Task.FromResult(new KeyValuePair<byte[], string>(Encoding.GetBytes(s), ContentType));
179 }
180 }
181}
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.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, 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< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
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:51
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