From 534fc279422b59361e17db802b1b06d16d71c59a Mon Sep 17 00:00:00 2001 From: Arnaud Roques Date: Tue, 14 Mar 2023 18:46:55 +0100 Subject: [PATCH] fix: Improve line endings in the output files https://github.com/plantuml/plantuml/pull/1183 --- .../sourceforge/plantuml/AbstractPSystem.java | 20 ++++++++-------- src/net/sourceforge/plantuml/BlockUml.java | 2 +- src/net/sourceforge/plantuml/UmlDiagram.java | 2 +- .../sourceforge/plantuml/core/UmlSource.java | 14 +++++------ .../plantuml/ditaa/PSystemDitaa.java | 2 +- .../plantuml/project/GanttDiagram.java | 4 +++- .../plantuml/svek/GeneralImageBuilder.java | 11 +++++---- .../sourceforge/plantuml/text/BackSlash.java | 24 +++++++++++-------- test/nonreg/simple/A0000_TestResult.java | 2 +- test/nonreg/simple/A0001_TestResult.java | 2 +- test/nonreg/simple/A0002_TestResult.java | 2 +- test/nonreg/simple/A0003_TestResult.java | 2 +- test/nonreg/simple/A0004_TestResult.java | 2 +- test/nonreg/simple/A0005_TestResult.java | 2 +- test/nonreg/simple/A0006_TestResult.java | 2 +- 15 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/net/sourceforge/plantuml/AbstractPSystem.java b/src/net/sourceforge/plantuml/AbstractPSystem.java index 95fb0aa1f..bce725466 100644 --- a/src/net/sourceforge/plantuml/AbstractPSystem.java +++ b/src/net/sourceforge/plantuml/AbstractPSystem.java @@ -105,14 +105,14 @@ public abstract class AbstractPSystem implements Diagram { } final public String getMetadata() { - if (source == null) { + if (source == null) return getVersion(); - } - final String rawString = source.getRawString(); - final String plainString = source.getPlainString(); - if (rawString != null && rawString.equals(plainString)) { + + final String rawString = source.getRawString("\n"); + final String plainString = source.getPlainString("\n"); + if (rawString != null && rawString.equals(plainString)) return rawString + BackSlash.NEWLINE + getVersion(); - } + return rawString + BackSlash.NEWLINE + plainString + BackSlash.NEWLINE + getVersion(); } @@ -121,9 +121,9 @@ public abstract class AbstractPSystem implements Diagram { } final public long seed() { - if (source == null) { + if (source == null) return 42; - } + return getSource().seed(); } @@ -150,9 +150,9 @@ public abstract class AbstractPSystem implements Diagram { } public DisplayPositionned getTitle() { - if (source == null) { + if (source == null) return DisplayPositioned.single(Display.empty(), HorizontalAlignment.CENTER, VerticalAlignment.TOP); - } + return DisplayPositioned.single(source.getTitle(), HorizontalAlignment.CENTER, VerticalAlignment.TOP); } diff --git a/src/net/sourceforge/plantuml/BlockUml.java b/src/net/sourceforge/plantuml/BlockUml.java index 204538b64..9a4229bad 100644 --- a/src/net/sourceforge/plantuml/BlockUml.java +++ b/src/net/sourceforge/plantuml/BlockUml.java @@ -89,7 +89,7 @@ public class BlockUml { // ::comment when __CORE__ public String getEncodedUrl() throws IOException { final Transcoder transcoder = TranscoderUtil.getDefaultTranscoder(); - final String source = getDiagram().getSource().getPlainString(); + final String source = getDiagram().getSource().getPlainString("\n"); final String encoded = transcoder.encode(source); return encoded; } diff --git a/src/net/sourceforge/plantuml/UmlDiagram.java b/src/net/sourceforge/plantuml/UmlDiagram.java index c0c0d0e62..67860d2cb 100644 --- a/src/net/sourceforge/plantuml/UmlDiagram.java +++ b/src/net/sourceforge/plantuml/UmlDiagram.java @@ -223,7 +223,7 @@ public abstract class UmlDiagram extends TitledDiagram implements Diagram, Annot if (source == null) return ""; - return source.getPlainString(); + return source.getPlainString("\n"); } static private List getFailureText1(Throwable exception, String graphvizVersion, String textDiagram) { diff --git a/src/net/sourceforge/plantuml/core/UmlSource.java b/src/net/sourceforge/plantuml/core/UmlSource.java index 51b16c656..b39eb40e8 100755 --- a/src/net/sourceforge/plantuml/core/UmlSource.java +++ b/src/net/sourceforge/plantuml/core/UmlSource.java @@ -160,32 +160,30 @@ final public class UmlSource { // } /** - * Return the source as a single String with \n as line separator. + * Return the source as a single String. * * @return the whole diagram source */ - public String getPlainString() { + public String getPlainString(String separator) { final StringBuilder sb = new StringBuilder(); for (StringLocated s : source) { sb.append(s.getString()); - sb.append('\r'); - sb.append(BackSlash.CHAR_NEWLINE); + sb.append(separator); } return sb.toString(); } - public String getRawString() { + public String getRawString(String separator) { final StringBuilder sb = new StringBuilder(); for (StringLocated s : rawSource) { sb.append(s.getString()); - sb.append('\r'); - sb.append(BackSlash.CHAR_NEWLINE); + sb.append(separator); } return sb.toString(); } public long seed() { - return StringUtils.seed(getPlainString()); + return StringUtils.seed(getPlainString("\n")); } public String getLine(LineLocation n) { diff --git a/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java b/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java index 44d014d63..b7dd3f87a 100644 --- a/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java +++ b/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java @@ -109,7 +109,7 @@ public class PSystemDitaa extends AbstractPSystem { final protected ImageData exportDiagramNow(OutputStream os, int num, FileFormatOption fileFormat) throws IOException { if (fileFormat.getFileFormat() == FileFormat.ATXT) { - os.write(getSource().getPlainString().getBytes()); + os.write(getSource().getPlainString(BackSlash.lineSeparator()).getBytes()); return ImageDataSimple.ok(); } diff --git a/src/net/sourceforge/plantuml/project/GanttDiagram.java b/src/net/sourceforge/plantuml/project/GanttDiagram.java index 9cf1c2ab0..611f9dc57 100644 --- a/src/net/sourceforge/plantuml/project/GanttDiagram.java +++ b/src/net/sourceforge/plantuml/project/GanttDiagram.java @@ -113,6 +113,7 @@ import net.sourceforge.plantuml.style.SName; import net.sourceforge.plantuml.style.Style; import net.sourceforge.plantuml.style.StyleSignatureBasic; import net.sourceforge.plantuml.svek.GraphvizCrash; +import net.sourceforge.plantuml.text.BackSlash; public class GanttDiagram extends TitledDiagram implements ToTaskDraw, WithSprite { @@ -256,7 +257,8 @@ public class GanttDiagram extends TitledDiagram implements ToTaskDraw, WithSprit } catch (Throwable t) { Logme.error(t); - final UDrawable crash = new GraphvizCrash(getSource().getPlainString(), false, t); + final UDrawable crash = new GraphvizCrash(getSource().getPlainString(BackSlash.lineSeparator()), + false, t); crash.drawU(ug); } diff --git a/src/net/sourceforge/plantuml/svek/GeneralImageBuilder.java b/src/net/sourceforge/plantuml/svek/GeneralImageBuilder.java index 45e99a101..9f7120298 100644 --- a/src/net/sourceforge/plantuml/svek/GeneralImageBuilder.java +++ b/src/net/sourceforge/plantuml/svek/GeneralImageBuilder.java @@ -122,6 +122,7 @@ import net.sourceforge.plantuml.svek.image.EntityImageStateEmptyDescription; import net.sourceforge.plantuml.svek.image.EntityImageSynchroBar; import net.sourceforge.plantuml.svek.image.EntityImageTips; import net.sourceforge.plantuml.svek.image.EntityImageUseCase; +import net.sourceforge.plantuml.text.BackSlash; import net.sourceforge.plantuml.text.Guillemet; import net.sourceforge.plantuml.utils.Log; @@ -455,11 +456,12 @@ public final class GeneralImageBuilder { try { svg = dotStringFactory.getSvg(basefile, dotStrings); } catch (IOException e) { - return new GraphvizCrash(source.getPlainString(), GraphvizUtils.graphviz244onWindows(), e); + return new GraphvizCrash(source.getPlainString(BackSlash.lineSeparator()), + GraphvizUtils.graphviz244onWindows(), e); } if (svg.length() == 0) - return new GraphvizCrash(source.getPlainString(), GraphvizUtils.graphviz244onWindows(), - new EmptySvgException()); + return new GraphvizCrash(source.getPlainString(BackSlash.lineSeparator()), + GraphvizUtils.graphviz244onWindows(), new EmptySvgException()); final String graphvizVersion = extractGraphvizVersion(svg); try { @@ -469,7 +471,8 @@ public final class GeneralImageBuilder { return result; } catch (Exception e) { Log.error("Exception " + e); - throw new UnparsableGraphvizException(e, graphvizVersion, svg, source.getPlainString()); + throw new UnparsableGraphvizException(e, graphvizVersion, svg, + source.getPlainString(BackSlash.lineSeparator())); } // ::done // ::uncomment when __CORE__ diff --git a/src/net/sourceforge/plantuml/text/BackSlash.java b/src/net/sourceforge/plantuml/text/BackSlash.java index 8970a6c8e..cd2f328cd 100644 --- a/src/net/sourceforge/plantuml/text/BackSlash.java +++ b/src/net/sourceforge/plantuml/text/BackSlash.java @@ -47,6 +47,10 @@ public class BackSlash { public static final String BS_BS_N = "\\n"; public static final String NEWLINE = "\n"; public static final char CHAR_NEWLINE = '\n'; + + public static final String lineSeparator() { + return System.lineSeparator(); + } public static char hiddenNewLine() { return StringUtils.PRIVATE_BLOCK + BackSlash.CHAR_NEWLINE; @@ -67,9 +71,9 @@ public class BackSlash { } public static List getWithNewlines(CharSequence s) { - if (s == null) { + if (s == null) return null; - } + final List result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); for (int i = 0; i < s.length(); i++) { @@ -94,9 +98,9 @@ public class BackSlash { } public static String translateBackSlashes(CharSequence s) { - if (s == null) { + if (s == null) return null; - } + // final String tmps = s.toString(); // if (tmps.indexOf('\\') == -1) { // return tmps; @@ -121,24 +125,24 @@ public class BackSlash { } public static String untranslateBackSlashes(CharSequence s) { - if (s == null) { + if (s == null) return null; - } + final StringBuilder result = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); - if (c > StringUtils.PRIVATE_BLOCK && c < '\uE07F') { + if (c > StringUtils.PRIVATE_BLOCK && c < '\uE07F') c = (char) (c - StringUtils.PRIVATE_BLOCK); - } + result.append(c); } return result.toString(); } private static char translateChar(char c) { - if (c > 128) { + if (c > 128) throw new IllegalArgumentException(); - } + return (char) (StringUtils.PRIVATE_BLOCK + c); } diff --git a/test/nonreg/simple/A0000_TestResult.java b/test/nonreg/simple/A0000_TestResult.java index e952e1302..4815fc445 100644 --- a/test/nonreg/simple/A0000_TestResult.java +++ b/test/nonreg/simple/A0000_TestResult.java @@ -7,7 +7,7 @@ public class A0000_TestResult { DPI: 96 dimension: [ 185.5185 ; 113.0000 ] scaleFactor: 1.0000 -seed: 5605069588648637213 +seed: 7067927655347766828 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0001_TestResult.java b/test/nonreg/simple/A0001_TestResult.java index 13c8bfec6..6626bc701 100644 --- a/test/nonreg/simple/A0001_TestResult.java +++ b/test/nonreg/simple/A0001_TestResult.java @@ -7,7 +7,7 @@ public class A0001_TestResult { DPI: 96 dimension: [ 296.7331 ; 285.0000 ] scaleFactor: 1.0000 -seed: -1212656935193060805 +seed: 1230863714434809965 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0002_TestResult.java b/test/nonreg/simple/A0002_TestResult.java index 5dcc01844..5e56a3f21 100644 --- a/test/nonreg/simple/A0002_TestResult.java +++ b/test/nonreg/simple/A0002_TestResult.java @@ -7,7 +7,7 @@ public class A0002_TestResult { DPI: 96 dimension: [ 660.1096 ; 994.5000 ] scaleFactor: 1.0000 -seed: -9181376250803721714 +seed: -2543044357581316352 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0003_TestResult.java b/test/nonreg/simple/A0003_TestResult.java index da86bb4cf..8bf3f0424 100644 --- a/test/nonreg/simple/A0003_TestResult.java +++ b/test/nonreg/simple/A0003_TestResult.java @@ -7,7 +7,7 @@ public class A0003_TestResult { DPI: 96 dimension: [ 367.7447 ; 76.0000 ] scaleFactor: 2.0000 -seed: -6040919743496430850 +seed: 2061842952546013393 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0004_TestResult.java b/test/nonreg/simple/A0004_TestResult.java index 6ea1b656e..18aca499a 100644 --- a/test/nonreg/simple/A0004_TestResult.java +++ b/test/nonreg/simple/A0004_TestResult.java @@ -7,7 +7,7 @@ public class A0004_TestResult { DPI: 96 dimension: [ 550.6156 ; 680.0000 ] scaleFactor: 1.0000 -seed: 6985134683589840646 +seed: -1093380870564056548 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0005_TestResult.java b/test/nonreg/simple/A0005_TestResult.java index 1cf7d34ba..5951b8586 100644 --- a/test/nonreg/simple/A0005_TestResult.java +++ b/test/nonreg/simple/A0005_TestResult.java @@ -7,7 +7,7 @@ public class A0005_TestResult { DPI: 96 dimension: [ 134.0935 ; 286.0000 ] scaleFactor: 1.0000 -seed: 1145907385572836867 +seed: -2035695693520640443 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none diff --git a/test/nonreg/simple/A0006_TestResult.java b/test/nonreg/simple/A0006_TestResult.java index c82dc67d1..6743ab493 100644 --- a/test/nonreg/simple/A0006_TestResult.java +++ b/test/nonreg/simple/A0006_TestResult.java @@ -7,7 +7,7 @@ public class A0006_TestResult { DPI: 96 dimension: [ 149.3989 ; 231.0000 ] scaleFactor: 1.0000 -seed: -2375783792654745998 +seed: -967197275408610478 svgLinkTarget: _top hoverPathColorRGB: null preserveAspectRatio: none