I'm trying to use plantuml inside java to generate a svg file. The code I'm using is basically the one from the example,
byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);
System.out.println(utf8EncodedString);
SourceStringReader reader = new SourceStringReader(utf8EncodedString);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// Write the first image to "os"
reader.generateImage(os, new FileFormatOption(FileFormat.SVG));
os.close();
// The XML is stored into svg
final String svg = new String(os.toByteArray(), Charset.forName("UTF-8"));
return svg;
Basically the utf-8 stuff is because I was thinking the issue was with encoding, but it turns out it's not. The string used as source is as follows:
@startuml
left to right direction
' Horizontal lines: -->, <--, <-->
' Vertical lines: ->, <-, <->
map Transport.Personnel{
ID => 38
}
map Airplane1{
ID => 39
}
map Airplane2{
ID => 40
}
map Lathe1{
ID => 41
}
map Lathe2{
ID => 42
}
map Production.from.Mold1{
ID => 43
}
map Production.from.Mold2{
ID => 44
}
map Assembly.Line1{
ID => 45
}
map Assembly.Line2{
ID => 46
}
map Assembly.Line3{
ID => 47
}
@enduml
This string generates a diagram on the online plantuml web service but when running on java, the svg generated indicates a syntax error in the string when opening it up:
... (skipping 1 line) ...
left to right direction
' Horizontal lines:
-->, <--
, <-->
' Vertical lines: ->, <-, <->
map Transport.Personnel{
Syntax Error?
Do I need to do anything else? I really have no idea what could be going wrong and I don't know what to search here/their forums to try and debug.
CodePudding user response:
I can this code which resulted in no errors for me
public class PlantUMLDemo {
public static void main (String[] args) throws IOException {
String source = "@startuml\r\n"
"left to right direction\r\n"
"' Horizontal lines: -->, <--, <-->\r\n"
"' Vertical lines: ->, <-, <->\r\n"
"map Transport.Personnel{\r\n"
"ID => 38\r\n"
"}\r\n"
"map Airplane1{\r\n"
"ID => 39\r\n"
"}\r\n"
"map Airplane2{\r\n"
"ID => 40\r\n"
"}\r\n"
"map Lathe1{\r\n"
"ID => 41\r\n"
"}\r\n"
"map Lathe2{\r\n"
"ID => 42\r\n"
"}\r\n"
"map Production.from.Mold1{\r\n"
"ID => 43\r\n"
"}\r\n"
"map Production.from.Mold2{\r\n"
"ID => 44\r\n"
"}\r\n"
"map Assembly.Line1{\r\n"
"ID => 45\r\n"
"}\r\n"
"map Assembly.Line2{\r\n"
"ID => 46\r\n"
"}\r\n"
"map Assembly.Line3{\r\n"
"ID => 47\r\n"
"}\r\n"
"@enduml";
byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);
System.out.println(utf8EncodedString);
SourceStringReader reader = new SourceStringReader(utf8EncodedString);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// Write the first image to "os"
reader.generateImage(os, new FileFormatOption(FileFormat.SVG));
os.close();
// The XML is stored into svg
final String svg = new String(os.toByteArray(), Charset.forName("UTF-8"));
System.out.println(svg);
// return svg;
}
}
In case it is relevant, I used the latest PlantUML jar (version 1.2021.9). My assumption is that you probably made a syntax error in your code. Copy paste my source string and try again.
The output of the program is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="application/ecmascript" contentStyleType="text/css" height="290px" preserveAspectRatio="none" style="width:539px;height:290px;background:#FFFFFF;" version="1.1" viewBox="0 0 539 290" width="539px" zoomAndPan="magnify"><defs><filter height="300%" id="fxvmrbp5q73dl" width="300%" x="-1" y="-1"><feGaussianBlur result="blurOut" stdDeviation="2.0"/><feColorMatrix in="blurOut" result="blurOut2" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .4 0"/><feOffset dx="4.0" dy="4.0" in="blurOut2" result="blurOut3"/><feBlend in="SourceGraphic" in2="blurOut3" mode="normal"/></filter></defs><g><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="127" x="7" y="7"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="113" x="14" y="21.457">Transport.Personnel</text><line style="stroke:#A80036;stroke-width:1.0;" x1="7" x2="134" y1="26.0938" y2="26.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="12" y="38.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="32" y="38.4746">38</text><line style="stroke:#A80036;stroke-width:1.0;" x1="27" x2="27" y1="26.0938" y2="42.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="66" x="37.5" y="85"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="52" x="44.5" y="99.457">Airplane1</text><line style="stroke:#A80036;stroke-width:1.0;" x1="37.5" x2="103.5" y1="104.0938" y2="104.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="42.5" y="116.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="62.5" y="116.4746">39</text><line style="stroke:#A80036;stroke-width:1.0;" x1="57.5" x2="57.5" y1="104.0938" y2="120.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="66" x="37.5" y="163"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="52" x="44.5" y="177.457">Airplane2</text><line style="stroke:#A80036;stroke-width:1.0;" x1="37.5" x2="103.5" y1="182.0938" y2="182.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="42.5" y="194.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="62.5" y="194.4746">40</text><line style="stroke:#A80036;stroke-width:1.0;" x1="57.5" x2="57.5" y1="182.0938" y2="198.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="52" x="44.5" y="241"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="38" x="51.5" y="255.457">Lathe1</text><line style="stroke:#A80036;stroke-width:1.0;" x1="44.5" x2="96.5" y1="260.0938" y2="260.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="49.5" y="272.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="69.5" y="272.4746">41</text><line style="stroke:#A80036;stroke-width:1.0;" x1="64.5" x2="64.5" y1="260.0938" y2="276.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="52" x="252.5" y="7"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="38" x="259.5" y="21.457">Lathe2</text><line style="stroke:#A80036;stroke-width:1.0;" x1="252.5" x2="304.5" y1="26.0938" y2="26.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="257.5" y="38.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="277.5" y="38.4746">42</text><line style="stroke:#A80036;stroke-width:1.0;" x1="272.5" x2="272.5" y1="26.0938" y2="42.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="137" x="210" y="85"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="123" x="217" y="99.457">Production.from.Mold1</text><line style="stroke:#A80036;stroke-width:1.0;" x1="210" x2="347" y1="104.0938" y2="104.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="215" y="116.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="235" y="116.4746">43</text><line style="stroke:#A80036;stroke-width:1.0;" x1="230" x2="230" y1="104.0938" y2="120.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="137" x="210" y="163"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="123" x="217" y="177.457">Production.from.Mold2</text><line style="stroke:#A80036;stroke-width:1.0;" x1="210" x2="347" y1="182.0938" y2="182.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="215" y="194.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="235" y="194.4746">44</text><line style="stroke:#A80036;stroke-width:1.0;" x1="230" x2="230" y1="182.0938" y2="198.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="102" x="227.5" y="241"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="88" x="234.5" y="255.457">Assembly.Line1</text><line style="stroke:#A80036;stroke-width:1.0;" x1="227.5" x2="329.5" y1="260.0938" y2="260.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="232.5" y="272.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="252.5" y="272.4746">45</text><line style="stroke:#A80036;stroke-width:1.0;" x1="247.5" x2="247.5" y1="260.0938" y2="276.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="102" x="423" y="7"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="88" x="430" y="21.457">Assembly.Line2</text><line style="stroke:#A80036;stroke-width:1.0;" x1="423" x2="525" y1="26.0938" y2="26.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="428" y="38.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="448" y="38.4746">46</text><line style="stroke:#A80036;stroke-width:1.0;" x1="443" x2="443" y1="26.0938" y2="42.6719"/><rect fill="#FEFECE" filter="url(#fxvmrbp5q73dl)" height="35.6719" style="stroke:#A80036;stroke-width:1.5;" width="102" x="423" y="85"/><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="88" x="430" y="99.457">Assembly.Line3</text><line style="stroke:#A80036;stroke-width:1.0;" x1="423" x2="525" y1="104.0938" y2="104.0938"/><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="10" x="428" y="116.4746">ID</text><text fill="#000000" font-family="sans-serif" font-size="10" lengthAdjust="spacing" textLength="12" x="448" y="116.4746">47</text><line style="stroke:#A80036;stroke-width:1.0;" x1="443" x2="443" y1="104.0938" y2="120.6719"/><!--MD5=[ff82272a54b0608060e06faff60c0c6f]
link Transport.Personnel to Airplane1--><!--MD5=[c202e95eecfc418b5501e2a8947e8feb]
link Airplane1 to Airplane2--><!--MD5=[636ba822349036c737d84d06f0729b92]
link Airplane2 to Lathe1--><!--MD5=[b9280dfb35d92cd40bcce7a3234b7f39]
link Transport.Personnel to Lathe2--><!--MD5=[771b27f758c2865dd959173aec72d1ef]
link Lathe2 to Production.from.Mold1--><!--MD5=[77de6294656f35161f6e03883c63e876]
link Production.from.Mold1 to Production.from.Mold2--><!--MD5=[73d5fa06c8330ee70fe1a2a0b8f7f067]
link Production.from.Mold2 to Assembly.Line1--><!--MD5=[8053d5220acde5e9c8d637bbcdec0086]
link Lathe2 to Assembly.Line2--><!--MD5=[b61908b6fd80267801bee0bc115ff909]
link Assembly.Line2 to Assembly.Line3--><!--MD5=[83bc2a7ae5b567adf58816b4879445c0]
@startuml
left to right direction
' Horizontal lines: - ->, <- -, <- ->
' Vertical lines: ->, <-, <->
map Transport.Personnel{
ID => 38
}
map Airplane1{
ID => 39
}
map Airplane2{
ID => 40
}
map Lathe1{
ID => 41
}
map Lathe2{
ID => 42
}
map Production.from.Mold1{
ID => 43
}
map Production.from.Mold2{
ID => 44
}
map Assembly.Line1{
ID => 45
}
map Assembly.Line2{
ID => 46
}
map Assembly.Line3{
ID => 47
}
@enduml
@startuml
left to right direction
map Transport.Personnel{
ID => 38
}
map Airplane1{
ID => 39
}
map Airplane2{
ID => 40
}
map Lathe1{
ID => 41
}
map Lathe2{
ID => 42
}
map Production.from.Mold1{
ID => 43
}
map Production.from.Mold2{
ID => 44
}
map Assembly.Line1{
ID => 45
}
map Assembly.Line2{
ID => 46
}
map Assembly.Line3{
ID => 47
}
@enduml
PlantUML version 1.2021.9(Sun Jul 25 05:13:56 CDT 2021)
(GPL source distribution)
Java Runtime: OpenJDK Runtime Environment
JVM: OpenJDK 64-Bit Server VM
Default Encoding: Cp1252
Language: en
Country: US
--></g></svg>
Screenshot of generated SVG online
When I generate this on the PLantUML website and I "view source", the results looks exactly like this output. I have to conclude that the code works with the provided source
string value.