Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PlantUmlDocumentToImageConverter.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
7using Waher.Script;
8
10{
15 {
20 {
21 }
22
26 public string[] FromContentTypes => new string[] { PlantUmlCodec.DefaultContentType };
27
31 public virtual string[] ToContentTypes
32 {
33 get
34 {
35 return new string[]
36 {
38 ImageCodec.ContentTypeSvg
39 };
40 }
41 }
42
46 public virtual Grade ConversionGrade => Grade.Excellent;
47
53 public async Task<bool> ConvertAsync(ConversionState State)
54 {
55 string GraphDescription;
56
57 using (StreamReader rd = new StreamReader(State.From))
58 {
59 GraphDescription = rd.ReadToEnd();
60 }
61
62 string s = State.ToContentType;
63 int i;
64
65 i = s.IndexOf(';');
66 if (i > 0)
67 s = s.Substring(0, i);
68
69 bool Png = string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
70 bool Svg = string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
71
72 if (!(State.PossibleContentTypes is null))
73 {
74 foreach (string ContentType in State.PossibleContentTypes)
75 {
76 s = ContentType;
77 i = s.IndexOf(';');
78 if (i > 0)
79 s = s.Substring(0, i);
80
81 Png |= string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
82 Svg |= string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
83 }
84 }
85
86 GraphInfo Graph;
87
88 if (Svg)
89 {
90 Graph = await PlantUml.GetGraphInfo("uml", GraphDescription, ResultType.Svg, true);
91 State.ToContentType = ImageCodec.ContentTypeSvg;
92 }
93 else if (Png)
94 {
95 Graph = await PlantUml.GetGraphInfo("uml", GraphDescription, ResultType.Png, true);
96 State.ToContentType = ImageCodec.ContentTypePng;
97 }
98 else
99 throw new Exception("Unable to convert document from " + State.FromContentType + " to " + State.ToContentType);
100
101 byte[] Data = await Resources.ReadAllBytesAsync(Graph.ImageFileName);
102
103 await State.To.WriteAsync(Data, 0, Data.Length);
104
105 return false;
106 }
107
108 }
109}
Contains the state of a content conversion process.
string ToContentType
Content type of the content to convert to. This value might be changed, in case the converter finds a...
string[] PossibleContentTypes
Possible content types the converter is allowed to convert to. Can be null if there are no alternativ...
string FromContentType
Content type of the content to convert from.
Stream To
Stream pointing to where binary representation of content is to be sent.
Stream From
Stream pointing to binary representation of content.
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypePng
image/png
Definition: ImageCodec.cs:30
const string ContentTypeSvg
image/svg+xml
Definition: ImageCodec.cs:45
virtual string[] ToContentTypes
Converts content to these content types.
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Class managing PlantUML integration into Markdown documents.
Definition: PlantUml.cs:51
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7