1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-06 02:10:53 +00:00
plantuml/src/net/sourceforge/plantuml/dot/AbstractGraphviz.java

204 lines
5.7 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2010-11-15 20:35:36 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
2022-08-17 17:34:24 +00:00
*
2010-11-15 20:35:36 +00:00
*
*/
2023-02-22 18:43:48 +00:00
package net.sourceforge.plantuml.dot;
2010-11-15 20:35:36 +00:00
import java.io.File;
import java.io.OutputStream;
2011-08-08 17:48:29 +00:00
import java.util.Arrays;
2010-11-15 20:35:36 +00:00
import java.util.List;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.OptionFlags;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.style.ISkinParam;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2010-11-15 20:35:36 +00:00
abstract class AbstractGraphviz implements Graphviz {
2023-02-28 21:22:51 +00:00
// ::remove file when __CORE__
2010-11-15 20:35:36 +00:00
private final File dotExe;
private final String dotString;
private final String[] type;
static boolean isWindows() {
return File.separatorChar == '\\';
}
2018-12-22 11:11:40 +00:00
private static String findExecutableOnPath(String name) {
2019-07-14 20:09:26 +00:00
final String path = System.getenv("PATH");
if (path != null)
2020-05-30 15:20:23 +00:00
for (String dirname : path.split(SFile.pathSeparator)) {
final File file = new File(dirname, name);
if (file.isFile() && file.canExecute())
2019-07-14 20:09:26 +00:00
return file.getAbsolutePath();
2018-12-22 11:11:40 +00:00
}
2018-12-22 11:11:40 +00:00
return null;
}
2016-04-04 19:05:10 +00:00
AbstractGraphviz(ISkinParam skinParam, String dotString, String... type) {
2010-11-15 20:35:36 +00:00
this.dotExe = searchDotExe();
this.dotString = dotString;
2021-05-09 21:14:40 +00:00
this.type = Objects.requireNonNull(type);
2010-11-15 20:35:36 +00:00
}
2021-01-10 20:52:19 +00:00
protected boolean findExecutableOnPath() {
return true;
}
2010-11-15 20:35:36 +00:00
2021-01-10 20:52:19 +00:00
final protected File searchDotExe() {
2018-12-22 11:11:40 +00:00
String getenv = GraphvizUtils.getenvGraphvizDot();
if (findExecutableOnPath() && getenv == null)
2018-12-22 11:11:40 +00:00
getenv = findExecutableOnPath(getExeName());
if (getenv == null)
2018-12-22 11:11:40 +00:00
return specificDotExe();
2018-12-22 11:11:40 +00:00
return new File(getenv);
2010-11-15 20:35:36 +00:00
}
abstract protected File specificDotExe();
2018-12-22 11:11:40 +00:00
abstract protected String getExeName();
2013-12-10 19:36:50 +00:00
final public ProcessState createFile3(OutputStream os) {
2021-05-09 21:14:40 +00:00
Objects.requireNonNull(dotString);
2010-11-15 20:35:36 +00:00
if (getExeState() != ExeState.OK)
2013-12-10 19:36:50 +00:00
throw new IllegalStateException();
2013-12-10 19:36:50 +00:00
final String cmd[] = getCommandLine();
2011-02-14 11:56:34 +00:00
ProcessRunner p = null;
2013-12-10 19:36:50 +00:00
ProcessState state = null;
2010-11-15 20:35:36 +00:00
try {
2013-12-10 19:36:50 +00:00
Log.info("Starting Graphviz process " + Arrays.asList(cmd));
2010-11-15 20:35:36 +00:00
Log.info("DotString size: " + dotString.length());
2011-02-14 11:56:34 +00:00
p = new ProcessRunner(cmd);
2015-04-07 18:18:37 +00:00
state = p.run(dotString.getBytes(), os);
2010-11-15 20:35:36 +00:00
Log.info("Ending process ok");
} catch (Throwable e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2010-11-15 20:35:36 +00:00
Log.error("Error: " + e);
Log.error("The command was " + cmd);
Log.error("");
Log.error("Try java -jar plantuml.jar -testdot to figure out the issue");
Log.error("");
} finally {
Log.info("Ending Graphviz process");
}
2011-02-14 11:56:34 +00:00
if (OptionFlags.getInstance().isCheckDotError() && p != null && p.getError().length() > 0) {
Log.error("GraphViz error stream : " + p.getError());
if (OptionFlags.getInstance().isCheckDotError())
2011-02-14 11:56:34 +00:00
throw new IllegalStateException("Dot error " + p.getError());
2011-02-14 11:56:34 +00:00
}
if (OptionFlags.getInstance().isCheckDotError() && p != null && p.getOut().length() > 0) {
Log.error("GraphViz out stream : " + p.getOut());
if (OptionFlags.getInstance().isCheckDotError())
2011-02-14 11:56:34 +00:00
throw new IllegalStateException("Dot out " + p.getOut());
2011-02-14 11:56:34 +00:00
}
2013-12-10 19:36:50 +00:00
return state;
2010-11-15 20:35:36 +00:00
}
2016-07-25 19:25:28 +00:00
final public ExeState getExeState() {
return ExeState.checkFile(dotExe);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
final public String dotVersion() {
final String cmd[] = getCommandLineVersion();
2011-08-08 17:48:29 +00:00
return executeCmd(cmd);
}
2013-12-10 19:36:50 +00:00
private String executeCmd(final String cmd[]) {
2010-11-15 20:35:36 +00:00
final ProcessRunner p = new ProcessRunner(cmd);
2015-04-07 18:18:37 +00:00
final ProcessState state = p.run(null, null);
if (state.differs(ProcessState.TERMINATED_OK()))
2013-12-10 19:36:50 +00:00
return "?";
2010-11-15 20:35:36 +00:00
final StringBuilder sb = new StringBuilder();
if (StringUtils.isNotEmpty(p.getOut()))
2010-11-15 20:35:36 +00:00
sb.append(p.getOut());
2010-11-15 20:35:36 +00:00
if (StringUtils.isNotEmpty(p.getError())) {
if (sb.length() > 0)
2010-11-15 20:35:36 +00:00
sb.append(' ');
2010-11-15 20:35:36 +00:00
sb.append(p.getError());
}
2015-05-31 18:56:03 +00:00
return StringUtils.trin(sb.toString().replace('\n', ' '));
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
final String[] getCommandLine() {
2015-04-07 18:18:37 +00:00
if (OptionFlags.ADD_NICE_FOR_DOT) {
final String[] result = new String[type.length + 1 + 3];
result[0] = "/bin/nice";
result[1] = "-n";
result[2] = "10";
result[3] = getDotExe().getAbsolutePath();
for (int i = 0; i < type.length; i++)
2015-04-07 18:18:37 +00:00
result[i + 4] = "-T" + type[i];
2015-04-07 18:18:37 +00:00
return result;
}
2013-12-10 19:36:50 +00:00
final String[] result = new String[type.length + 1];
result[0] = getDotExe().getAbsolutePath();
for (int i = 0; i < type.length; i++)
2013-12-10 19:36:50 +00:00
result[i + 1] = "-T" + type[i];
2013-12-10 19:36:50 +00:00
return result;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
final String[] getCommandLineVersion() {
return new String[] { getDotExe().getAbsolutePath(), "-V" };
}
2010-11-15 20:35:36 +00:00
public final File getDotExe() {
return dotExe;
}
2011-08-08 17:48:29 +00:00
public final String getDotString() {
return dotString;
}
public final List<String> getType() {
return Arrays.asList(type);
}
2010-11-15 20:35:36 +00:00
}