# Wiki source code of PlantUMLMacroGClass

Last modified by Helge Dahl on 2018/03/07 23:26

#### Export

#### Office Formats

Export as PDF Export as ODT Export as RTF

#### Other Formats

Export as HTML
Show last authors

1 | /* Groovy Class #* */
2 |
3 | import net.sourceforge.plantuml.*
4 |
5 | class PlantUMLMacroGClass {
6 | def umldoc;
7 | def ctx;
8 | def umltext;
9 | def imageFormat;
10 | def enableImageMap;
11 |
12 | void setObjects( doc, context, text, format, imageMap) {
13 | umldoc = doc
14 | ctx = context
15 | umltext = text
16 | imageFormat = format
17 | enableImageMap = imageMap
18 | }
19 |
20 | String run( request, response) {
21 | // Using umlref, each diagram of the wiki page has its own unique url
22 | def umlref="1"
23 | if (null != ctx.umlref) {
24 | umlref=Integer.toString(Integer.valueOf(ctx.umlref)+1)
25 | }
26 | ctx.put("umlref", umlref)
27 |
28 | if("true".equals(request.preview)){
29 | umltext = request.getSession().getAttribute("preview_uml_" + umlref)
30 | //request.getSession().removeAttribute("preview_uml_" + umlref)
31 | }
32 |
33 | def reader = new SourceStringReader("@startuml\n"+umltext+"\n@enduml")
34 | if ("plain".equals(request.xpage)) {
35 | if( !umlref.equals(request.uml) ){
36 | return "";
37 | }
38 | response.setContentType( "svg".equals(imageFormat) ? "image/svg+xml" : "image/png" )
39 | def img = response.getOutputStream()
40 | def desc = reader.outputImage(img, new FileFormatOption("svg".equals(imageFormat) ? FileFormat.SVG : FileFormat.PNG))
41 | img.close();
42 | return ""
43 | } else if("xpart".equals(request.xpage)) {
44 | return ""
45 | } else {
46 | def preview = false
47 | if("wysiwyginput".equals(request.xpage) || request.getRequestURL().indexOf("/bin/preview/") != -1){
48 | imageFormat = "png"
49 | enableImageMap = true
50 | preview = true
51 | request.getSession().setAttribute("preview_uml_" + umlref, umltext)
52 | }
53 | if("svg".equals(imageFormat)){
54 | return "{{html clean=\"false\"}}" + "<p><embed src=\"" + umldoc.getURL("get") + "?xpage=plain&uml=" + umlref + "\" style=\"max-width:100%\"></p>" + "{{/html}}"
55 | } else {
56 | if( !enableImageMap ){
57 | return "[[image:"+umldoc.getURL("get")+"?xpage=plain&uml="+umlref+"||style=\"max-width:100%\"]]"
58 | } else {
59 | def imgMapHtml = "<map name=\"umlmap"+umlref+"\">";
60 | String[] mapLines = new String[0];
61 | //String map = reader.generateImage(new NullOutputStream(), new FileFormatOption(FileFormat.PNG, false));
62 | String map = reader.getCMapData(0, new FileFormatOption(FileFormat.PNG, false));
63 | if( map != null ){
64 | mapLines = map.split("[\\r\\n]");
65 | }
66 | for (int i = 1; (i + 1) < mapLines.length; i++) {
67 | imgMapHtml += mapLines[i] + "\n";
68 | }
69 | imgMapHtml += "</map>"
70 | int random = (int)(Math.random() * 100)
71 | return "{{html clean=\"false\"}}" +
72 | "<p><img src=\"" + umldoc.getURL("get") + "?xpage=plain&uml=" + umlref + (preview ? "&preview=true" : "") + "\" style=\"max-width:200%\" usemap=\"#umlmap"+umlref + "\"/>" + imgMapHtml + "</p>" +
73 | "{{/html}}"
74 | }
75 | }
76 | }
77 | }
78 | }
79 |
80 | /* *# */

# Applications

# Tips

Jump to any page in the wiki with Ctrl+G or Meta+G.
See more shortcuts.

# Need help?

If you need help with XWiki you can contact:

- Community Support
- Professional Support