support diagrams without startuml, enduml

Add support for diagram without @startuml and @enduml.
This commit is contained in:
HeinrichAD 2023-07-19 22:22:35 +02:00 committed by PlantUML
parent af8f7d0c33
commit 5859c3b427
2 changed files with 30 additions and 20 deletions

View File

@ -157,10 +157,10 @@ 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(); final SourceStringReader reader = getSourceStringReader(uml);
SourceStringReader reader = new SourceStringReader(defines, uml, CONFIG); if (reader == null) {
if (CONFIG.size() > 0 && reader.getBlocks().get(0).getDiagram().getWarningOrError() != null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No UML diagram found");
reader = new SourceStringReader(uml); return;
} }
if (format == FileFormat.BASE64) { if (format == FileFormat.BASE64) {
@ -218,6 +218,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.
* *

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