Imported Upstream version 1.8.15
[platform/upstream/doxygen.git] / src / plantuml.cpp
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2015 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15
16 #include "plantuml.h"
17 #include "portable.h"
18 #include "config.h"
19 #include "doxygen.h"
20 #include "index.h"
21 #include "message.h"
22
23 #include <qdir.h>
24
25 static const int maxCmdLine = 40960;
26
27 QCString writePlantUMLSource(const QCString &outDir,const QCString &fileName,const QCString &content)
28 {
29   QCString baseName(4096);
30   static int umlindex=1;
31
32   if (fileName.isEmpty()) // generate name
33   {
34     baseName = outDir+"/inline_umlgraph_"+QCString().setNum(umlindex++);
35   }
36   else // user specified name
37   {
38     baseName = fileName;
39     int i=baseName.findRev('.');
40     if (i!=-1) baseName = baseName.left(i);
41     baseName.prepend(outDir+"/");
42   }
43   QFile file(baseName+".pu");
44   if (!file.open(IO_WriteOnly))
45   {
46     err("Could not open file %s for writing\n",baseName.data());
47   }
48   QCString text = "@startuml\n";
49   text+=content;
50   text+="\n@enduml\n";
51   file.writeBlock( text, text.length() );
52   file.close();
53   return baseName;
54 }
55
56 void generatePlantUMLOutput(const char *baseName,const char *outDir,PlantUMLOutputFormat format)
57 {
58   static QCString plantumlJarPath = Config_getString(PLANTUML_JAR_PATH);
59   static QCString plantumlConfigFile = Config_getString(PLANTUML_CFG_FILE);
60   static QCString dotPath = Config_getString(DOT_PATH);
61
62   QCString pumlExe = "java";
63   QCString pumlArgs = "";
64
65   QStrList &pumlIncludePathList = Config_getList(PLANTUML_INCLUDE_PATH);
66   char *s=pumlIncludePathList.first();
67   if (s)
68   {
69     pumlArgs += "-Dplantuml.include.path=\"";
70     pumlArgs += s;
71     s = pumlIncludePathList.next(); 
72   }
73   while (s)
74   {
75     pumlArgs += portable_pathListSeparator();
76     pumlArgs += s;
77     s = pumlIncludePathList.next(); 
78   }
79   if (pumlIncludePathList.first()) pumlArgs += "\" ";
80   pumlArgs += "-Djava.awt.headless=true -jar \""+plantumlJarPath+"plantuml.jar\" ";
81   if (!plantumlConfigFile.isEmpty())
82   {
83     pumlArgs += "-config \"";
84     pumlArgs += plantumlConfigFile;
85     pumlArgs += "\" ";
86   }
87   if (Config_getBool(HAVE_DOT) && !dotPath.isEmpty())
88   {
89     pumlArgs += "-graphvizdot \"";
90     pumlArgs += dotPath;
91     pumlArgs += "dot";
92     pumlArgs += portable_commandExtension();
93     pumlArgs += "\" ";
94   }
95   pumlArgs+="-o \"";
96   pumlArgs+=outDir;
97   pumlArgs+="\" ";
98   QCString imgName = baseName;
99   // The basename contains path, we need to strip the path from the filename in order
100   // to create the image file name which should be included in the index.qhp (Qt help index file).
101   int i;
102   if ((i=imgName.findRev('/'))!=-1) // strip path
103   {
104     imgName=imgName.right(imgName.length()-i-1);
105   }
106   switch (format)
107   {
108     case PUML_BITMAP:
109       pumlArgs+="-tpng";
110       imgName+=".png";
111       break;
112     case PUML_EPS:
113       pumlArgs+="-teps";
114       imgName+=".eps";
115       break;
116     case PUML_SVG:
117       pumlArgs+="-tsvg";
118       imgName+=".svg";
119       break;
120   }
121   pumlArgs+=" \"";
122   pumlArgs+=baseName;
123   pumlArgs+=".pu\" ";
124   pumlArgs+="-charset UTF-8 ";
125   int exitCode;
126   //printf("*** running: %s %s outDir:%s %s\n",pumlExe.data(),pumlArgs.data(),outDir,baseName);
127   msg("Running PlantUML on generated file %s.pu\n",baseName);
128   portable_sysTimerStart();
129   if ((exitCode=portable_system(pumlExe,pumlArgs,TRUE))!=0)
130   {
131     err("Problems running PlantUML. Verify that the command 'java -jar \"%splantuml.jar\" -h' works from the command line. Exit code: %d\n",
132         plantumlJarPath.data(),exitCode);
133   }
134   else if (Config_getBool(DOT_CLEANUP))
135   {
136     QFile(QCString(baseName)+".pu").remove();
137   }
138   portable_sysTimerStop();
139   if ( (format==PUML_EPS) && (Config_getBool(USE_PDFLATEX)) )
140   {
141     QCString epstopdfArgs(maxCmdLine);
142     epstopdfArgs.sprintf("\"%s.eps\" --outfile=\"%s.pdf\"",baseName,baseName);
143     portable_sysTimerStart();
144     if ((exitCode=portable_system("epstopdf",epstopdfArgs))!=0)
145     {
146       err("Problems running epstopdf. Check your TeX installation! Exit code: %d\n",exitCode);
147     }
148     portable_sysTimerStop();
149   }
150   Doxygen::indexList->addImageFile(imgName);
151 }
152