1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-16 23:22:24 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/dot/AbstractGraphviz.java

212 lines
5.9 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
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
*
*
*/
package net.sourceforge.plantuml.cucadiagram.dot;
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;
2016-04-04 19:05:10 +00:00
import net.sourceforge.plantuml.ISkinParam;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.Log;
import net.sourceforge.plantuml.OptionFlags;
import net.sourceforge.plantuml.StringUtils;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2010-11-15 20:35:36 +00:00
abstract class AbstractGraphviz implements Graphviz {
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);
2019-07-14 20:09:26 +00:00
if (file.isFile() && file.canExecute()) {
return file.getAbsolutePath();
}
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
if (type == null) {
throw new IllegalArgumentException();
}
this.dotExe = searchDotExe();
this.dotString = dotString;
this.type = type;
}
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();
2021-01-10 20:52:19 +00:00
if (findExecutableOnPath() && getenv == null) {
2018-12-22 11:11:40 +00:00
getenv = findExecutableOnPath(getExeName());
}
if (getenv == null) {
return specificDotExe();
2010-11-15 20:35:36 +00:00
}
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) {
2010-11-15 20:35:36 +00:00
if (dotString == null) {
throw new IllegalArgumentException();
}
2016-07-25 19:25:28 +00:00
if (getExeState() != ExeState.OK) {
2020-05-30 15:20:23 +00:00
// createPngNoGraphviz(os, new
// FileFormatOption(FileFormat.valueOf(type[0].goUpperCase())));
2013-12-10 19:36:50 +00:00
throw new IllegalStateException();
2010-11-15 20:35:36 +00:00
}
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);
// if (state == ProcessState.TERMINATED_OK) {
// result = true;
// }
2010-11-15 20:35:36 +00:00
Log.info("Ending process ok");
} catch (Throwable e) {
e.printStackTrace();
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()) {
throw new IllegalStateException("Dot error " + p.getError());
}
}
if (OptionFlags.getInstance().isCheckDotError() && p != null && p.getOut().length() > 0) {
Log.error("GraphViz out stream : " + p.getOut());
if (OptionFlags.getInstance().isCheckDotError()) {
throw new IllegalStateException("Dot out " + p.getOut());
}
}
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())) {
sb.append(p.getOut());
}
if (StringUtils.isNotEmpty(p.getError())) {
if (sb.length() > 0) {
sb.append(' ');
}
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++) {
result[i + 4] = "-T" + type[i];
}
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++) {
result[i + 1] = "-T" + type[i];
2010-11-15 20:35:36 +00:00
}
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
}