【发布时间】:2023-02-05 10:59:09
【问题描述】:
我正在尝试在 java 中使用 plantuml 来生成一个 svg 文件。我使用的代码基本上是示例中的代码,
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;
基本上 utf-8 的东西是因为我认为问题出在编码上,但事实证明不是。用作源的字符串如下:
@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
此字符串在在线 plantuml Web 服务上生成图表,但在 java 上运行时,生成的 svg 在打开时指示字符串中的语法错误:
... (skipping 1 line) ...
left to right direction
' Horizontal lines:
-->, <--
, <-->
' Vertical lines: ->, <-, <->
map Transport.Personnel{
Syntax Error?
我还需要做其他事情吗?我真的不知道出了什么问题,也不知道要在这里搜索什么/他们的论坛来尝试和调试。
【问题讨论】:
-
输入
source变量值是多少?你从哪里导入这些课程?下次发布最小的、可重现的代码?如果我不知道您使用的是哪些类,我将无法重现。 -
@hfontanez 来源在这种情况下是无关紧要的,因为我复制粘贴了 utf8EncodedString。导入来自 mvn 存储库中的 net.sourceforge.plantuml。它适用于 Bob-Alice 字符串,就像我从中获取的示例一样
-
输入并非无关紧要,因为它阻止我运行您的代码。我需要知道
source变量的字符串值才能重现您的问题。 -
@hfontanez 然后使用我发布的那个,它是一样的。或者使用
"@startuml\nleft to right direction\n\' Horizontal lines: -->, <--, <-->\n\' Vertical lines: ->, <-, <->\nmap Transport.Personel{\n ID => 15\n}\n@enduml\n"在 svg 上返回相同的错误 -
我很困惑,因为你发布了“上面函数中打印出的字符串如下:”这听起来像是我的输出。我将对其进行处理,看看是否可以重现它。