地图内的语法错误,但适用于在线网络服务

Syntax Error inside map, but works on the online webservice

我正在尝试在 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 网络服务上生成了一个图表,但是当 运行 在 java 上时,生成的 svg 表明打开它时字符串中存在语法错误:

... (skipping 1 line) ...
left to right direction
' Horizontal lines:
-->, <--
, <-->
' Vertical lines: ->, <-, <->
map Transport.Personnel{
Syntax Error?

我还需要做其他事情吗?我真的不知道出了什么问题,也不知道要搜索什么 here/their 论坛来尝试调试。

我可以使用这段代码,它对我来说没有任何错误

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;
    }
}

如果相关,我使用了最新的 PlantUML jar(版本 1.2021.9)。我的假设是您可能在代码中犯了语法错误。复制粘贴我的源字符串并重试。

程序输出如下:

<?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>

在线生成SVG截图

当我在 PLantUML 网站上生成此文件并“查看源代码”时,结果看起来与此输出完全一样。我必须得出结论,该代码适用于提供的 source 字符串值。