Compare commits

...

3 Commits

Author SHA1 Message Date
HeinrichAD 608b58b3e0 add the same support to maps
sendMap should work similar to sendDiagram w.r.t. diagram source selection
2023-07-19 23:08:13 +02:00
HeinrichAD 5859c3b427 support diagrams without startuml, enduml
Add support for diagram without @startuml and @enduml.
2023-07-19 23:08:13 +02:00
Arnaud Roques af8f7d0c33 v1.2023.10 2023-07-19 18:29:41 +02:00
3 changed files with 62 additions and 31 deletions

View File

@ -61,7 +61,7 @@
<jetty.contextpath>/${wtp.contextName}</jetty.contextpath> <jetty.contextpath>/${wtp.contextName}</jetty.contextpath>
<!-- main versions --> <!-- main versions -->
<plantuml.version>1.2023.9</plantuml.version> <plantuml.version>1.2023.10</plantuml.version>
<!-- Please keep the jetty version identical with the docker image --> <!-- Please keep the jetty version identical with the docker image -->
<jetty.version>11.0.15</jetty.version> <jetty.version>11.0.15</jetty.version>
<!-- <!--

View File

@ -157,10 +157,14 @@ public class DiagramResponse {
response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(getContentType()); response.setContentType(getContentType());
final Defines defines = getPreProcDefines(); if (idx < 0) {
SourceStringReader reader = new SourceStringReader(defines, uml, CONFIG); response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid diagram index: {0}", idx));
if (CONFIG.size() > 0 && reader.getBlocks().get(0).getDiagram().getWarningOrError() != null) { return;
reader = new SourceStringReader(uml); }
final SourceStringReader reader = getSourceStringReader(uml);
if (reader == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No UML diagram found");
return;
} }
if (format == FileFormat.BASE64) { if (format == FileFormat.BASE64) {
@ -218,6 +222,27 @@ public class DiagramResponse {
return null; return null;
} }
private SourceStringReader getSourceStringReader(String uml) {
SourceStringReader reader = getSourceStringReaderWithConfig(uml);
if (reader.getBlocks().isEmpty()) {
uml = "@startuml\n" + uml + "\n@enduml";
reader = getSourceStringReaderWithConfig(uml);
if (reader.getBlocks().isEmpty()) {
return null;
}
}
return reader;
}
private SourceStringReader getSourceStringReaderWithConfig(String uml) {
final Defines defines = getPreProcDefines();
SourceStringReader reader = new SourceStringReader(defines, uml, CONFIG);
if (!CONFIG.isEmpty() && reader.getBlocks().get(0).getDiagram().getWarningOrError() != null) {
reader = new SourceStringReader(defines, uml);
}
return reader;
}
/** /**
* Get PlantUML preprocessor defines. * Get PlantUML preprocessor defines.
* *
@ -264,19 +289,36 @@ public class DiagramResponse {
* @throws IOException if an input or output exception occurred * @throws IOException if an input or output exception occurred
*/ */
public void sendMap(String uml, int idx) throws IOException { public void sendMap(String uml, int idx) throws IOException {
if (idx < 0) {
idx = 0;
}
response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(getContentType()); response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
final BlockUml blockUml = reader.getBlocks().get(0); if (idx < 0) {
if (StringUtils.isDiagramCacheable(uml)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid diagram index: {0}", idx));
addHeaderForCache(blockUml); return;
} }
final Diagram diagram = blockUml.getDiagram(); final SourceStringReader reader = getSourceStringReader(uml);
ImageData map = diagram.exportDiagram(new NullOutputStream(), idx, if (reader == null) {
new FileFormatOption(FileFormat.PNG, false)); response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No UML diagram found");
return;
}
final BlockSelection blockSelection = getOutputBlockSelection(reader, idx);
if (blockSelection == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (StringUtils.isDiagramCacheable(uml)) {
addHeaderForCache(blockSelection.block);
}
final Diagram diagram = blockSelection.block.getDiagram();
if (diagram instanceof PSystemError) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
ImageData map = diagram.exportDiagram(
new NullOutputStream(),
blockSelection.systemIdx,
new FileFormatOption(FileFormat.PNG, false)
);
if (map.containsCMapData()) { if (map.containsCMapData()) {
PrintWriter httpOut = response.getWriter(); PrintWriter httpOut = response.getWriter();
final String cmap = map.getCMapData("plantuml"); final String cmap = map.getCMapData("plantuml");

View File

@ -29,7 +29,6 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.imageio.IIOException; import javax.imageio.IIOException;
@ -39,11 +38,7 @@ import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import net.sourceforge.plantuml.BlockUml;
import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.UmlSource;
/** /**
* Proxy servlet of the webapp. * Proxy servlet of the webapp.
@ -108,29 +103,23 @@ public class ProxyServlet extends HttpServlet {
final String source = request.getParameter("src"); final String source = request.getParameter("src");
final String index = request.getParameter("idx"); final String index = request.getParameter("idx");
final int idx = index == null ? 0 : Integer.parseInt(index);
final URL srcUrl = validateURL(source, response); final URL srcUrl = validateURL(source, response);
if (srcUrl == null) { if (srcUrl == null) {
return; // error is already set/handled inside `validateURL` return; // error is already set/handled inside `validateURL`
} }
// generate the response // fetch diagram from URL
String diagmarkup = getSource(srcUrl); final String uml = getSource(srcUrl);
SourceStringReader reader = new SourceStringReader(diagmarkup);
int n = index == null ? 0 : Integer.parseInt(index);
List<BlockUml> blocks = reader.getBlocks();
BlockUml block = blocks.get(n);
Diagram diagram = block.getDiagram();
UmlSource umlSrc = diagram.getSource();
String uml = umlSrc.getPlainString("\n");
// generate the response // generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request); DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
try { try {
// special handling for the MAP since it's not using "#sendDiagram()" like the other types // special handling for the MAP since it's not using "#sendDiagram()" like the other types
if ("map".equals(fmt)) { if ("map".equals(fmt)) {
dr.sendMap(uml, 0); dr.sendMap(uml, idx);
} else { } else {
dr.sendDiagram(uml, 0); dr.sendDiagram(uml, idx);
} }
} catch (IIOException e) { } catch (IIOException e) {
// Browser has closed the connection, so the HTTP OutputStream is closed // Browser has closed the connection, so the HTTP OutputStream is closed