1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-09-27 14:39:02 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/dot/AbstractGraphviz.java

190 lines
5.4 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, 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
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2016-03-06 16:47:34 +00:00
* Revision $Revision: 19109 $
2010-11-15 20:35:36 +00:00
*
*/
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;
import net.sourceforge.plantuml.Log;
import net.sourceforge.plantuml.OptionFlags;
import net.sourceforge.plantuml.StringUtils;
abstract class AbstractGraphviz implements Graphviz {
private final File dotExe;
private final String dotString;
private final String[] type;
static boolean isWindows() {
return File.separatorChar == '\\';
}
AbstractGraphviz(String dotString, String... type) {
if (type == null) {
throw new IllegalArgumentException();
}
this.dotExe = searchDotExe();
this.dotString = dotString;
this.type = type;
}
private File searchDotExe() {
if (OptionFlags.getInstance().getDotExecutable() == null) {
final String getenv = GraphvizUtils.getenvGraphvizDot();
if (getenv == null) {
return specificDotExe();
}
return new File(getenv);
}
return new File(OptionFlags.getInstance().getDotExecutable());
}
abstract protected File specificDotExe();
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();
}
if (illegalDotExe()) {
2015-04-07 18:18:37 +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
}
2015-04-07 18:18:37 +00:00
public boolean illegalDotExe() {
2010-11-15 20:35:36 +00:00
return dotExe == null || dotExe.isFile() == false || dotExe.canRead() == false;
}
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
}