Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
SVG by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlasov committed Dec 21, 2023
1 parent 0e9daf0 commit 179f2a0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
13 changes: 13 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,18 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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("<img src=\"data:image/png;base64, ");
ret
.append(Base64.getEncoder().encodeToString(baos.toByteArray()))
.append("\"");

if (org.nasdanika.common.Util.isBlank(diagramCMap)) {
ret.append("/>");
return ret.toString();
}

String openingTag = "<map id=\"plantuml_map\" name=\"plantuml_map\">";
if (diagramCMap.startsWith(openingTag)) {
String mapId = "plantuml_map_" + UUID.randomUUID().toString();
ret
.append(" usemap=\"#")
.append(mapId)
.append("\"/>")
.append(System.lineSeparator())
.append("<map id=\"")
.append(mapId)
.append("\" name=\"")
.append(mapId)
.append("\">")
.append(diagramCMap.substring(openingTag.length()));
if (png) {
String diagramCMap = reader.getCMapData(0, fileFormatOption);

StringBuilder ret = new StringBuilder("<img src=\"data:image/png;base64, ");
ret
.append(Base64.getEncoder().encodeToString(baos.toByteArray()))
.append("\"");

} else {
ret
.append(" usemap=\"#plant_uml_map\"/>")
if (org.nasdanika.common.Util.isBlank(diagramCMap)) {
ret.append("/>");
return ret.toString();
}

String openingTag = "<map id=\"plantuml_map\" name=\"plantuml_map\">";
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("<map id=\"")
.append(mapId)
.append("\" name=\"")
.append(mapId)
.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 "<div class=\"nsd-error\">Error during diagram rendering: " + e + "</div>";
}
Expand Down

0 comments on commit 179f2a0

Please sign in to comment.