Doxygen
Loading...
Searching...
No Matches
plantuml.cpp
Go to the documentation of this file.
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 <mutex>
17#include "plantuml.h"
18#include "util.h"
19#include "portable.h"
20#include "config.h"
21#include "doxygen.h"
22#include "message.h"
23#include "debug.h"
24#include "fileinfo.h"
25#include "dir.h"
26#include "indexlist.h"
27#include "stringutil.h"
28
29static std::mutex g_PlantUmlMutex;
30
32 const QCString &content,OutputFormat format, const QCString &engine,
33 const QCString &srcFile,int srcLine,bool inlineCode)
34{
35 StringVector baseNameVector;
36 QCString baseName;
37 QCString puName;
38 QCString imgName;
39 QCString outDir(outDirArg);
40
41 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource fileName: {}\n",fileName);
42 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource outDir: {}\n",outDir);
43
44 // strip any trailing slashes and backslashes
45 size_t l = 0;
46 while ((l=outDir.length())>0 && (outDir.at(l-1)=='/' || outDir.at(l-1)=='\\'))
47 {
48 outDir = outDir.left(l-1);
49 }
50
51 generatePlantUmlFileNames(fileName,format,outDir,baseName,puName,imgName);
52
53 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSourcebaseName: {}\n",baseName);
54 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSourcebaseName puName: {}\n",puName);
55 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSourcebaseName imgName: {}\n",imgName);
56
57 QCString text;
58 if (inlineCode) text = "@start"+engine+" "+imgName+"\n";
59 text.reserve(text.length()+content.length()+100); // add room for image name and end marker
60 const char *p = content.data();
61 if (p)
62 {
63 char c = 0;
64 bool insideComment = false;
65 QCString locEngine;
66 while ((c=*p++))
67 {
68 text+=c;
69 switch (c)
70 {
71 case '\'': insideComment=true; break;
72 case '\n': insideComment=false; break;
73 case '\t': break;
74 case ' ': break;
75 case '@':
76 if (!insideComment && literal_at(p,"start")) // @start...
77 {
78 locEngine.clear();
79 p+=5;
80 text += "start";
81 while ((c=*p++) && isId(c))
82 {
83 locEngine += c;
84 text+=c;
85 }
86 QCString inpName;
87 QCString rest;
88
89 // skip leading whitespace
90 if (*p && (c==' ' || c=='\t'))
91 {
92 while ((c=*p++) && (c==' ' || c=='\t')) {}
93 }
94 // get everything till end or endOfLine, and split into inpName (without extension) and rest
95 enum State { InName, InExt, InRest };
96 State state = InName;
97 while (*p && c!='\n')
98 {
99 switch (state)
100 {
101 case InName: // looking for the name part
102 if (isId(c) || c=='-') inpName+=c;
103 else if (c=='.') state=InExt;
104 else rest+=c, state=InRest;
105 break;
106 case InExt: // skipping over extension part
107 if (!isId(c) && c!='-') rest+=c, state=InRest;
108 break;
109 case InRest: // gather rest until new line
110 rest+=c;
111 break;
112 }
113 c = *p++;
114 }
115 //printf("inpName='%s' rest='%s'\n",qPrint(inpName),qPrint(rest));
116 generatePlantUmlFileNames(inpName,format,outDir,baseName,puName,imgName);
117
118 // insert the image name
119 text+=' ';
120 text+=imgName;
121
122 if (!rest.isEmpty())
123 {
124 text += '\n';
125 text += rest;
126 }
127 if (c) text+=c;
128 }
129 else if (!insideComment && strncmp(p,("end"+locEngine).data(), 3+strlen(engine.data()))==0) // @end...
130 {
131 text += "end"+locEngine+"\n";
132 p+=3+locEngine.length();
133 if (!inlineCode)
134 {
135 QCString qcOutDir(substitute(outDir,"\\","/"));
136 uint32_t pos = qcOutDir.findRev("/");
137 QCString generateType(qcOutDir.right(qcOutDir.length() - (pos + 1)) );
138 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource generateType: {}\n",generateType);
139 PlantumlManager::instance().insert(generateType.str(),puName.str(),outDir,format,text,srcFile,srcLine);
140 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource generateType: {}\n",generateType);
141 baseNameVector.push_back(baseName.str());
142 text.clear();
143 }
144 }
145 break;
146 default:
147 break;
148 }
149 }
150 text+='\n';
151 }
152 if (inlineCode)
153 {
154 text +="@end"+engine+"\n";
155 //printf("content\n====\n%s\n=====\n->\n-----\n%s\n------\n",qPrint(content),qPrint(text));
156 QCString qcOutDir(substitute(outDir,"\\","/"));
157 uint32_t pos = qcOutDir.findRev("/");
158 QCString generateType(qcOutDir.right(qcOutDir.length() - (pos + 1)) );
159 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource generateType: {}\n",generateType);
160 PlantumlManager::instance().insert(generateType.str(),puName.str(),outDir,format,text,srcFile,srcLine);
161 Debug::print(Debug::Plantuml,0,"*** writePlantUMLSource generateType: {}\n",generateType);
162 baseNameVector.push_back(baseName.str());
163 }
164
165 return baseNameVector;
166}
167
169 QCString &baseName,QCString &puName,QCString &imgName)
170{
171 static int umlindex=1;
172
173 if (fileName.isEmpty()) // generate name
174 {
175 std::lock_guard<std::mutex> lock(g_PlantUmlMutex);
176 puName = "inline_umlgraph_"+QCString().setNum(umlindex);
177 baseName = outDir+"/inline_umlgraph_"+QCString().setNum(umlindex++);
178 }
179 else // user specified name
180 {
181 baseName = fileName;
182 int i=baseName.findRev('.');
183 if (i!=-1) baseName = baseName.left(i);
184 puName = baseName;
185 baseName.prepend(outDir+"/");
186 }
187
188 switch (format)
189 {
190 case PUML_BITMAP:
191 imgName =puName+".png";
192 break;
193 case PUML_EPS:
194 imgName =puName+".eps";
195 break;
196 case PUML_SVG:
197 imgName =puName+".svg";
198 break;
199 }
200}
201
202void PlantumlManager::generatePlantUMLOutput(const QCString &baseName,const QCString &/* outDir */,OutputFormat format)
203{
204 QCString imgName = baseName;
205 // The basename contains path, we need to strip the path from the filename in order
206 // to create the image file name which should be included in the index.qhp (Qt help index file).
207 int i = imgName.findRev('/');
208 if (i!=-1) // strip path
209 {
210 imgName=imgName.mid(i+1);
211 }
212 switch (format)
213 {
214 case PUML_BITMAP:
215 imgName+=".png";
216 break;
217 case PUML_EPS:
218 imgName+=".eps";
219 break;
220 case PUML_SVG:
221 imgName+=".svg";
222 break;
223 }
224
225 Doxygen::indexList->addImageFile(imgName);
226}
227
228//--------------------------------------------------------------------
229
230
232{
233 static PlantumlManager theInstance;
234 return theInstance;
235}
236
240
241static void runPlantumlContent(const PlantumlManager::FilesMap &plantumlFiles,
242 const PlantumlManager::ContentMap &plantumlContent,
244{
245 /* example : running: java -Djava.awt.headless=true
246 -jar "/usr/local/bin/plantuml.jar"
247 -o "test_doxygen/DOXYGEN_OUTPUT/html"
248 -tpng
249 "test_doxygen/DOXYGEN_OUTPUT/html/A.pu"
250 -charset UTF-8
251 outDir:test_doxygen/DOXYGEN_OUTPUT/html
252 test_doxygen/DOXYGEN_OUTPUT/html/A
253 */
254 int exitCode = 0;
255 QCString plantumlJarPath = Config_getString(PLANTUML_JAR_PATH);
256 QCString plantumlConfigFile = Config_getString(PLANTUML_CFG_FILE);
257
258 QCString pumlExe = "java";
259 QCString pumlArgs = "";
260 QCString pumlType = "";
261 QCString pumlOutDir = "";
262
263 const StringVector &pumlIncludePathList = Config_getList(PLANTUML_INCLUDE_PATH);
264 {
265 auto it = pumlIncludePathList.begin();
266 if (it!=pumlIncludePathList.end())
267 {
268 pumlArgs += "-Dplantuml.include.path=\"";
269 pumlArgs += it->c_str();
270 ++it;
271 }
272 while (it!=pumlIncludePathList.end())
273 {
274 pumlArgs += Portable::pathListSeparator();
275 pumlArgs += it->c_str();
276 ++it;
277 }
278 }
279 if (!pumlIncludePathList.empty()) pumlArgs += "\" ";
280 pumlArgs += "-Djava.awt.headless=true -jar \""+plantumlJarPath+"\" ";
281 if (!plantumlConfigFile.isEmpty())
282 {
283 pumlArgs += "-config \"";
284 pumlArgs += plantumlConfigFile;
285 pumlArgs += "\" ";
286 }
287 // the -graphvizdot option expects a relative or absolute path to the dot executable, so
288 // we need to use the unverified DOT_PATH option and check if it points to an existing file.
289 QCString dotPath = Config_getString(DOT_PATH);
290 FileInfo dp(dotPath.str());
291 if (Config_getBool(HAVE_DOT) && dp.exists() && dp.isFile())
292 {
293 pumlArgs += "-graphvizdot \"";
294 pumlArgs += dotPath;
295 pumlArgs += "\" ";
296 }
297 switch (format)
298 {
300 pumlType="png";
301 break;
303 pumlType="eps";
304 break;
306 pumlType="svg";
307 break;
308 }
309
310 {
311 for (const auto &[name,nb] : plantumlContent)
312 {
313 if (nb.content.isEmpty()) continue;
314
315 QCString pumlArguments = pumlArgs;
316 msg("Generating PlantUML {} Files in {}\n",pumlType,name);
317 pumlArguments+="-o \"";
318 pumlArguments+=nb.outDir;
319 pumlArguments+="\" ";
320 pumlArguments+="-charset UTF-8 -t";
321 pumlArguments+=pumlType;
322 pumlArguments+=" ";
323
324 QCString puFileName("");
325 puFileName+=nb.outDir;
326 puFileName+="/";
327 pumlOutDir=puFileName;
328 puFileName+="inline_umlgraph_";
329 puFileName+=pumlType;
330 puFileName+=name.c_str();
331 puFileName+=".pu";
332
333 pumlArguments+="\"";
334 pumlArguments+=puFileName;
335 pumlArguments+="\" ";
336
337
338 QCString cachedContent;
339 FileInfo fi(puFileName.str());
340 if (fi.exists())
341 {
342 cachedContent = fileToString(puFileName);
343 }
344
345 std::ofstream file = Portable::openOutputStream(puFileName);
346 if (!file.is_open())
347 {
348 err_full(nb.srcFile,nb.srcLine,"Could not open file {} for writing",puFileName);
349 }
350 file.write( nb.content.data(), nb.content.length() );
351 file.close();
352 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::runPlantumlContent Running Plantuml arguments:{}\n",pumlArguments);
353
354 if (cachedContent == nb.content) continue;
355
356 if ((exitCode=Portable::system(pumlExe.data(),pumlArguments.data(),TRUE))!=0)
357 {
358 err_full(nb.srcFile,nb.srcLine,"Problems running PlantUML. Verify that the command 'java -jar \"{}\" -h' works from the command line. Exit code: {}.",
359 plantumlJarPath,exitCode);
360 }
361
362 if ( (format==PlantumlManager::PUML_EPS) && (Config_getBool(USE_PDFLATEX)) )
363 {
364 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::runPlantumlContent Running epstopdf\n");
365 auto files_kv = plantumlFiles.find(name);
366 if (files_kv!=plantumlFiles.end())
367 {
368 for (const auto &str : files_kv->second)
369 {
370 const int maxCmdLine = 40960;
372 epstopdfArgs.sprintf("\"%s%s.eps\" --outfile=\"%s%s.pdf\"",
373 pumlOutDir.data(),str.c_str(), pumlOutDir.data(),str.c_str());
374 if ((exitCode=Portable::system("epstopdf",epstopdfArgs.data()))!=0)
375 {
376 err_full(nb.srcFile,nb.srcLine,"Problems running epstopdf. Check your TeX installation! Exit code: {}.",exitCode);
377 }
378 else
379 {
380 Dir().remove(pumlOutDir.str()+str+".eps");
381 }
382 }
383 }
384 }
385 }
386 }
387}
388
396
397static void print(const PlantumlManager::FilesMap &plantumlFiles)
398{
400 {
401 for (const auto &[key,list] : plantumlFiles)
402 {
403 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::print Files PlantumlFiles key:{} size:{}\n",key,list.size());
404 for (const auto &s : list)
405 {
406 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::print list:{}\n",s);
407 }
408 }
409 }
410}
411
412static void print(const PlantumlManager::ContentMap &plantumlContent)
413{
415 {
416 for (const auto &[key,content] : plantumlContent)
417 {
418 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::print Content PlantumlContent key: {}\n",key);
419 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::print Content:\n{}\n",content.content);
420 }
421 }
422}
423
425 const std::string &key, const std::string &value)
426{
427 auto kv = plantumlFiles.find(key);
428 if (kv==plantumlFiles.end())
429 {
430 kv = plantumlFiles.emplace(key,StringVector()).first;
431 }
432 kv->second.push_back(value);
433}
434
436 const std::string &key, const QCString &outDir, const QCString &puContent,
437 const QCString &srcFile,int srcLine)
438{
439 auto kv = plantumlContent.find(key);
440 if (kv==plantumlContent.end())
441 {
442 kv = plantumlContent.emplace(key,PlantumlContent("",outDir,srcFile,srcLine)).first;
443 }
444 kv->second.content+=puContent;
445}
446
447void PlantumlManager::insert(const std::string &key, const std::string &value,
448 const QCString &outDir,OutputFormat format,const QCString &puContent,
449 const QCString &srcFile,int srcLine)
450{
451 Debug::print(Debug::Plantuml,0,"*** PlantumlManager::insert key:{} ,value:{}\n",key,value);
452
453 switch (format)
454 {
455 case PUML_BITMAP:
458 addPlantumlContent(m_pngPlantumlContent,key,outDir,puContent,srcFile,srcLine);
460 break;
461 case PUML_EPS:
464 addPlantumlContent(m_epsPlantumlContent,key,outDir,puContent,srcFile,srcLine);
466 break;
467 case PUML_SVG:
470 addPlantumlContent(m_svgPlantumlContent,key,outDir,puContent,srcFile,srcLine);
472 break;
473 }
474}
475
476//--------------------------------------------------------------------
@ Plantuml
Definition debug.h:39
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:76
Class representing a directory in the file system.
Definition dir.h:75
bool remove(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:314
static IndexList * indexList
Definition doxygen.h:134
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
bool isFile() const
Definition fileinfo.cpp:63
ContentMap m_pngPlantumlContent
Definition plantuml.h:92
std::map< std::string, PlantumlContent > ContentMap
Definition plantuml.h:76
OutputFormat
Plant UML output image formats.
Definition plantuml.h:44
ContentMap m_epsPlantumlContent
Definition plantuml.h:94
void generatePlantUmlFileNames(const QCString &fileName, OutputFormat format, const QCString &outDir, QCString &baseName, QCString &puName, QCString &imgName)
Definition plantuml.cpp:168
std::map< std::string, StringVector > FilesMap
Definition plantuml.h:75
void insert(const std::string &key, const std::string &value, const QCString &outDir, OutputFormat format, const QCString &puContent, const QCString &srcFile, int srcLine)
Definition plantuml.cpp:447
StringVector writePlantUMLSource(const QCString &outDirArg, const QCString &fileName, const QCString &content, OutputFormat format, const QCString &engine, const QCString &srcFile, int srcLine, bool inlineCode)
Write a PlantUML compatible file.
Definition plantuml.cpp:31
ContentMap m_svgPlantumlContent
Definition plantuml.h:93
FilesMap m_pngPlantumlFiles
Definition plantuml.h:89
static PlantumlManager & instance()
Definition plantuml.cpp:231
FilesMap m_svgPlantumlFiles
Definition plantuml.h:90
void run()
Run plant UML tool for all images.
Definition plantuml.cpp:389
void generatePlantUMLOutput(const QCString &baseName, const QCString &outDir, OutputFormat format)
Convert a PlantUML file to an image.
Definition plantuml.cpp:202
FilesMap m_epsPlantumlFiles
Definition plantuml.h:91
This is an alternative implementation of QCString.
Definition qcstring.h:101
QCString & prepend(const char *s)
Definition qcstring.h:407
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:226
char & at(size_t i)
Returns a reference to the character at index i.
Definition qcstring.h:578
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
const std::string & str() const
Definition qcstring.h:537
QCString & setNum(short n)
Definition qcstring.h:444
QCString right(size_t len) const
Definition qcstring.h:219
void reserve(size_t size)
Reserve space for size bytes without changing the string contents.
Definition qcstring.h:172
QCString & sprintf(const char *format,...)
Definition qcstring.cpp:29
@ ExplicitSize
Definition qcstring.h:133
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition qcstring.cpp:91
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:159
QCString left(size_t len) const
Definition qcstring.h:214
void clear()
Definition qcstring.h:169
#define Config_getList(name)
Definition config.h:38
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
std::vector< std::string > StringVector
Definition containers.h:33
static const int maxCmdLine
Definition dia.cpp:24
#define msg(fmt,...)
Definition message.h:94
#define err_full(file, line, fmt,...)
Definition message.h:132
std::ofstream openOutputStream(const QCString &name, bool append=false)
Definition portable.cpp:665
QCString pathListSeparator()
Definition portable.cpp:400
int system(const QCString &command, const QCString &args, bool commandHasConsole=true)
Definition portable.cpp:106
static void runPlantumlContent(const PlantumlManager::FilesMap &plantumlFiles, const PlantumlManager::ContentMap &plantumlContent, PlantumlManager::OutputFormat format)
Definition plantuml.cpp:241
static std::mutex g_PlantUmlMutex
Definition plantuml.cpp:29
static void addPlantumlContent(PlantumlManager::ContentMap &plantumlContent, const std::string &key, const QCString &outDir, const QCString &puContent, const QCString &srcFile, int srcLine)
Definition plantuml.cpp:435
static void print(const PlantumlManager::FilesMap &plantumlFiles)
Definition plantuml.cpp:397
static void addPlantumlFiles(PlantumlManager::FilesMap &plantumlFiles, const std::string &key, const std::string &value)
Definition plantuml.cpp:424
Portable versions of functions that are platform dependent.
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition qcstring.cpp:477
#define TRUE
Definition qcstring.h:37
Some helper functions for std::string.
bool literal_at(const char *data, const char(&str)[N])
returns TRUE iff data points to a substring that matches string literal str
Definition stringutil.h:98
QCString fileToString(const QCString &name, bool filter, bool isSourceCode)
Definition util.cpp:1442
A bunch of utility functions.
bool isId(int c)
Definition util.h:208