From 179f2a0f33055141f6e581cf35ca5668e89c00b3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 20 Dec 2023 19:57:35 -0500 Subject: [PATCH] SVG by default --- .classpath | 13 +++ .../plantuml/PlantUMLDiagramGenerator.java | 84 +++++++++++-------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/.classpath b/.classpath index 48edbe0..0d20b22 100644 --- a/.classpath +++ b/.classpath @@ -23,5 +23,18 @@ + + + + + + + + + + + + + diff --git a/src/main/java/org/nasdanika/diagramgenerator/plantuml/PlantUMLDiagramGenerator.java b/src/main/java/org/nasdanika/diagramgenerator/plantuml/PlantUMLDiagramGenerator.java index 38c60a3..d5cc46f 100644 --- a/src/main/java/org/nasdanika/diagramgenerator/plantuml/PlantUMLDiagramGenerator.java +++ b/src/main/java/org/nasdanika/diagramgenerator/plantuml/PlantUMLDiagramGenerator.java @@ -12,6 +12,20 @@ public class PlantUMLDiagramGenerator implements DiagramGenerator { + private boolean png; + + public PlantUMLDiagramGenerator() { + this(false); + } + + /** + * + * @param png Renders PNG if true, SVG otherwise. + */ + public PlantUMLDiagramGenerator(boolean png) { + this.png = png; + } + @Override public boolean isSupported(String dialect) { return DiagramGenerator.UML_DIALECT.equals(dialect) @@ -36,45 +50,49 @@ public String generateDiagram(String spec, String dialect) { SourceStringReader reader = new SourceStringReader(sb.toString()); - FileFormatOption fileFormatOption = new FileFormatOption(FileFormat.PNG); + FileFormatOption fileFormatOption = new FileFormatOption(png ? FileFormat.PNG : FileFormat.SVG); reader.outputImage(baos, 0, fileFormatOption); - String diagramCMap = reader.getCMapData(0, fileFormatOption); baos.close(); - - StringBuilder ret = new StringBuilder(""); - return ret.toString(); - } - - String openingTag = ""; - if (diagramCMap.startsWith(openingTag)) { - String mapId = "plantuml_map_" + UUID.randomUUID().toString(); - ret - .append(" usemap=\"#") - .append(mapId) - .append("\"/>") - .append(System.lineSeparator()) - .append("") - .append(diagramCMap.substring(openingTag.length())); + if (png) { + String diagramCMap = reader.getCMapData(0, fileFormatOption); + + StringBuilder ret = new StringBuilder("") + if (org.nasdanika.common.Util.isBlank(diagramCMap)) { + ret.append("/>"); + return ret.toString(); + } + + String openingTag = ""; + if (diagramCMap.startsWith(openingTag)) { + String mapId = "plantuml_map_" + UUID.randomUUID().toString(); + ret + .append(" usemap=\"#") + .append(mapId) + .append("\"/>") .append(System.lineSeparator()) - .append(diagramCMap); - return ret.toString(); + .append("") + .append(diagramCMap.substring(openingTag.length())); + + } else { + ret + .append(" usemap=\"#plant_uml_map\"/>") + .append(System.lineSeparator()) + .append(diagramCMap); + return ret.toString(); + } + return ret.toString(); } - - return ret.toString(); + + return new String(baos.toByteArray()); } catch (Exception e) { return "
Error during diagram rendering: " + e + "
"; }