XWiki - PlantUMLMacroGClass

Page of - last modified by Helge Dahl on 2018/03/07 23:26

PlantUMLMacroGClass

Helge Dahl

2018/03/07 23:26

Table of Contents

/* Groovy Class #* */

import net.sourceforge.plantuml.*

class PlantUMLMacroGClass {
def umldoc;
def ctx;
def umltext;
def imageFormat;
def enableImageMap;

void setObjects( doc, context, text, format, imageMap) {
umldoc = doc
ctx = context
umltext = text
imageFormat = format
enableImageMap = imageMap
}

String run( request, response) {
Using umlref, each diagram of the wiki page has its own unique url
def umlref="1"
if (null != ctx.umlref) {
umlref=Integer.toString(Integer.valueOf(ctx.umlref)+1)
}
ctx.put("umlref", umlref)

if("true".equals(request.preview)){
umltext = request.getSession().getAttribute("preview_uml_" + umlref)
request.getSession().removeAttribute("preview_uml_" + umlref)
}

def reader = new SourceStringReader("@startuml\n"+umltext+"\n@enduml")
if ("plain".equals(request.xpage)) {
if( !umlref.equals(request.uml) ){
return "";
}
response.setContentType( "svg".equals(imageFormat) ? "image/svg+xml" : "image/png" )
def img = response.getOutputStream()
def desc = reader.outputImage(img, new FileFormatOption("svg".equals(imageFormat) ? FileFormat.SVG : FileFormat.PNG))
img.close();
return ""
} else if("xpart".equals(request.xpage)) {
return ""
} else {
def preview = false
if("wysiwyginput".equals(request.xpage) || request.getRequestURL().indexOf("/bin/preview/") != -1){
imageFormat = "png"
enableImageMap = true
preview = true
request.getSession().setAttribute("preview_uml_" + umlref, umltext)
}
if("svg".equals(imageFormat)){
return "" + "

" + ""
} else {
if( !enableImageMap ){
return ""
} else {
def imgMapHtml = "<map name=\"umlmap"+umlref+"\">";
String[] mapLines = new String[0];
String map = reader.generateImage(new NullOutputStream(), new FileFormatOption(FileFormat.PNG, false));
String map = reader.getCMapData(0, new FileFormatOption(FileFormat.PNG, false));
if( map != null ){
mapLines = map.split("[
r
n]");
}
for (int i = 1; (i + 1) < mapLines.length; i++) {
imgMapHtml += mapLines[i] + "\n";
}
imgMapHtml += "</map>"
int random = (int)(Math.random() * 100)
return "" + "

" + imgMapHtml + "

" + ""
}
}
}
}
}

/* *# */