Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphVizCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
9{
14 {
19 {
20 }
21
25 public const string DefaultContentType = "text/vnd.graphviz";
26
30 public static readonly string[] GraphVizContentTypes = new string[]
31 {
33 };
34
38 public static readonly string[] GraphVizFileExtensions = new string[]
39 {
40 "gv", "dot", "neato", "fdp", "sfdp", "twopi", "circo"
41 };
42
47
52
59 public bool Decodes(string ContentType, out Grade Grade)
60 {
61 if (Array.IndexOf(GraphVizContentTypes, 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 GraphViz = Strings.GetString(Data, Encoding);
87 return Task.FromResult(new ContentResponse(ContentType, new GraphVizDocument(GraphViz), Data));
88 }
89
96 public bool TryGetContentType(string FileExtension, out string ContentType)
97 {
98 switch (FileExtension.ToLower())
99 {
100 case "gv":
101 case "dot":
102 case "neato":
103 case "fdp":
104 case "sfdp":
105 case "twopi":
106 case "circo":
107 ContentType = GraphVizContentTypes[0];
108 return true;
109
110 default:
111 ContentType = string.Empty;
112 return false;
113 }
114 }
115
122 public bool TryGetFileExtension(string ContentType, out string FileExtension)
123 {
124 switch (ContentType.ToLower())
125 {
127 FileExtension = GraphVizFileExtensions[0];
128 return true;
129
130 default:
131 FileExtension = string.Empty;
132 return false;
133 }
134 }
135
143 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
144 {
145 if (Object is GraphVizDocument)
146 {
147 if (InternetContent.IsAccepted(GraphVizContentTypes, AcceptedContentTypes))
148 {
149 Grade = Grade.Barely;
150 return true;
151 }
152 }
153
154 Grade = Grade.NotAtAll;
155 return false;
156 }
157
166 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
167 {
168 if (!InternetContent.IsAccepted(GraphVizContentTypes, out string ContentType, AcceptedContentTypes))
169 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
170
171 string s;
172
173 if (Object is GraphVizDocument GraphVizDoc)
174 s = GraphVizDoc.GraphDescription;
175 else
176 s = Object.ToString();
177
178 if (Encoding is null)
179 {
180 ContentType += "; charset=utf-8";
181 Encoding = Encoding.UTF8;
182 }
183 else
184 ContentType += "; charset=" + Encoding.WebName;
185
186 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(s)));
187 }
188 }
189}
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.
string[] ContentTypes
Supported content types.
const string DefaultContentType
text/vnd.graphviz
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
static readonly string[] GraphVizContentTypes
GraphViz content types.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
GraphVizCodec()
GraphViz encoder/decoder.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
string[] FileExtensions
Supported file extensions.
static readonly string[] GraphVizFileExtensions
Plain text file extensions.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Class managing GraphViz integration into Markdown documents.
Definition: GraphViz.cs:61
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