plantuml/src/net/sourceforge/plantuml/svek/GraphvizCrash.java

246 lines
8.5 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +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
*
2013-12-10 19:36:50 +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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* 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.svek;
2018-09-23 12:15:14 +00:00
import java.awt.Color;
2015-04-07 18:18:37 +00:00
import java.awt.image.BufferedImage;
2013-12-10 19:36:50 +00:00
import java.util.ArrayList;
import java.util.List;
2017-07-03 17:59:53 +00:00
import net.sourceforge.plantuml.BackSlash;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.OptionPrint;
2016-12-21 22:10:29 +00:00
import net.sourceforge.plantuml.StringUtils;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.awt.geom.Dimension2D;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizUtils;
import net.sourceforge.plantuml.flashcode.FlashCodeFactory;
import net.sourceforge.plantuml.flashcode.FlashCodeUtils;
import net.sourceforge.plantuml.fun.IconLoader;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.GraphicPosition;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
2020-09-19 15:43:24 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.QuoteUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2020-09-19 15:43:24 +00:00
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2020-06-07 10:03:18 +00:00
import net.sourceforge.plantuml.ugraphic.AffineTransformType;
import net.sourceforge.plantuml.ugraphic.PixelImage;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UImage;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
2020-09-19 15:43:24 +00:00
import net.sourceforge.plantuml.version.PSystemVersion;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.version.Version;
2015-06-07 10:23:10 +00:00
public class GraphvizCrash extends AbstractTextBlock implements IEntityImage {
2013-12-10 19:36:50 +00:00
2020-09-19 15:43:24 +00:00
private final TextBlock text1;
2015-04-07 18:18:37 +00:00
private final BufferedImage flashCode;
2017-07-03 17:59:53 +00:00
private final String text;
2020-09-19 15:43:24 +00:00
private final boolean graphviz244onWindows;
2015-04-07 18:18:37 +00:00
2020-12-14 18:31:06 +00:00
public GraphvizCrash(String text, boolean graphviz244onWindows, Throwable rootCause) {
2017-07-03 17:59:53 +00:00
this.text = text;
2020-09-19 15:43:24 +00:00
this.graphviz244onWindows = graphviz244onWindows;
2015-04-07 18:18:37 +00:00
final FlashCodeUtils utils = FlashCodeFactory.getFlashCodeUtils();
2018-09-23 12:15:14 +00:00
this.flashCode = utils.exportFlashcode(text, Color.BLACK, Color.WHITE);
2020-12-14 18:31:06 +00:00
this.text1 = GraphicStrings.createBlackOnWhite(init(rootCause), IconLoader.getRandom(),
2016-11-18 21:12:09 +00:00
GraphicPosition.BACKGROUND_CORNER_TOP_RIGHT);
2013-12-10 19:36:50 +00:00
}
2017-07-03 17:59:53 +00:00
public static List<String> anErrorHasOccured(Throwable exception, String text) {
2021-05-14 08:42:57 +00:00
final List<String> strings = new ArrayList<>();
2016-05-11 21:31:47 +00:00
if (exception == null) {
strings.add("An error has occured!");
} else {
strings.add("An error has occured : " + exception);
}
2016-12-21 22:10:29 +00:00
final String quote = StringUtils.rot(QuoteUtils.getSomeQuote());
2013-12-10 19:36:50 +00:00
strings.add("<i>" + quote);
strings.add(" ");
2017-07-03 17:59:53 +00:00
strings.add("Diagram size: " + lines(text) + " lines / " + text.length() + " characters.");
strings.add(" ");
2016-05-11 21:31:47 +00:00
return strings;
}
2017-07-03 17:59:53 +00:00
private static int lines(String text) {
int result = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == BackSlash.CHAR_NEWLINE) {
result++;
}
}
return result;
}
2020-09-19 15:43:24 +00:00
public static void checkOldVersionWarning(List<String> strings) {
2016-05-11 21:31:47 +00:00
final long days = (System.currentTimeMillis() - Version.compileTime()) / 1000L / 3600 / 24;
if (days >= 90) {
2021-01-10 20:52:19 +00:00
strings.add(" ");
strings.add("<b>This version of PlantUML is " + days + " days old, so you should");
strings.add("<b>consider upgrading from https://plantuml.com/download");
2016-05-11 21:31:47 +00:00
}
}
2020-09-19 15:43:24 +00:00
public static void pleaseGoTo(List<String> strings) {
2016-05-11 21:31:47 +00:00
strings.add(" ");
2020-06-28 12:40:33 +00:00
strings.add("Please go to https://plantuml.com/graphviz-dot to check your GraphViz version.");
2016-05-11 21:31:47 +00:00
strings.add(" ");
}
2020-09-19 15:43:24 +00:00
public static void youShouldSendThisDiagram(List<String> strings) {
2016-05-11 21:31:47 +00:00
strings.add("You should send this diagram and this image to <b>plantuml@gmail.com</b> or");
2020-06-28 12:40:33 +00:00
strings.add("post to <b>https://plantuml.com/qa</b> to solve this issue.");
2016-05-11 21:31:47 +00:00
strings.add("You can try to turn arround this issue by simplifing your diagram.");
}
2016-11-18 21:12:09 +00:00
2016-05-11 21:31:47 +00:00
public static void thisMayBeCaused(final List<String> strings) {
strings.add("This may be caused by :");
strings.add(" - a bug in PlantUML");
strings.add(" - a problem in GraphViz");
}
2020-12-14 18:31:06 +00:00
private List<String> init(Throwable rootCause) {
2017-07-03 17:59:53 +00:00
final List<String> strings = anErrorHasOccured(null, text);
2019-03-29 22:14:07 +00:00
strings.add("For some reason, dot/GraphViz has crashed.");
2020-12-14 18:31:06 +00:00
strings.add("");
strings.add("RootCause " + rootCause);
if (rootCause != null) {
strings.addAll(CommandExecutionResult.getStackTrace(rootCause));
}
strings.add("");
2013-12-10 19:36:50 +00:00
strings.add("This has been generated with PlantUML (" + Version.versionString() + ").");
2016-05-11 21:31:47 +00:00
checkOldVersionWarning(strings);
2013-12-10 19:36:50 +00:00
strings.add(" ");
2015-04-07 18:18:37 +00:00
addProperties(strings);
strings.add(" ");
try {
final String dotVersion = GraphvizUtils.dotVersion();
strings.add("Default dot version: " + dotVersion);
} catch (Throwable e) {
strings.add("Cannot determine dot version: " + e.toString());
}
2016-05-11 21:31:47 +00:00
pleaseGoTo(strings);
youShouldSendThisDiagram(strings);
2015-04-07 18:18:37 +00:00
if (flashCode != null) {
addDecodeHint(strings);
}
2013-12-10 19:36:50 +00:00
return strings;
}
2020-09-19 15:43:24 +00:00
private List<String> getText2() {
2021-05-14 08:42:57 +00:00
final List<String> strings = new ArrayList<>();
2020-11-21 17:33:24 +00:00
strings.add(" ");
2020-09-19 15:43:24 +00:00
strings.add("<b>It looks like you are running GraphViz 2.44 under Windows.");
strings.add("If you have just installed GraphViz, you <i>may</i> have to execute");
strings.add("the post-install command <b>dot -c</b> like in the following example:");
return strings;
}
2020-11-21 17:33:24 +00:00
private List<String> getText3() {
2021-05-14 08:42:57 +00:00
final List<String> strings = new ArrayList<>();
2020-11-21 17:33:24 +00:00
strings.add(" ");
strings.add("You may have to have <i>Administrator rights</i> to avoid the following error message:");
return strings;
}
2015-04-07 18:18:37 +00:00
public static void addDecodeHint(final List<String> strings) {
strings.add(" ");
2018-11-26 18:46:22 +00:00
strings.add(" Diagram source: (Use http://zxing.org/w/decode.jspx to decode the qrcode)");
2015-04-07 18:18:37 +00:00
}
public static void addProperties(final List<String> strings) {
2016-11-18 21:12:09 +00:00
strings.addAll(OptionPrint.interestingProperties());
strings.addAll(OptionPrint.interestingValues());
2015-04-07 18:18:37 +00:00
}
public boolean isHidden() {
return false;
}
2020-03-18 10:50:02 +00:00
public HColor getBackcolor() {
return HColorUtils.WHITE;
2015-04-07 18:18:37 +00:00
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
2020-09-19 15:43:24 +00:00
return getMain().calculateDimension(stringBounder);
2015-04-07 18:18:37 +00:00
}
public void drawU(UGraphic ug) {
2020-09-19 15:43:24 +00:00
getMain().drawU(ug);
}
private TextBlock getMain() {
TextBlock result = text1;
2015-04-07 18:18:37 +00:00
if (flashCode != null) {
2020-09-19 15:43:24 +00:00
final UImage flash = new UImage(new PixelImage(flashCode, AffineTransformType.TYPE_NEAREST_NEIGHBOR))
.scale(3);
result = TextBlockUtils.mergeTB(result, flash, HorizontalAlignment.LEFT);
2015-04-07 18:18:37 +00:00
}
2020-09-19 15:43:24 +00:00
if (graphviz244onWindows) {
final TextBlock text2 = GraphicStrings.createBlackOnWhite(getText2());
result = TextBlockUtils.mergeTB(result, text2, HorizontalAlignment.LEFT);
final UImage dotc = new UImage(new PixelImage(PSystemVersion.getDotc(), AffineTransformType.TYPE_BILINEAR));
result = TextBlockUtils.mergeTB(result, dotc, HorizontalAlignment.LEFT);
2020-11-21 17:33:24 +00:00
final TextBlock text3 = GraphicStrings.createBlackOnWhite(getText3());
result = TextBlockUtils.mergeTB(result, text3, HorizontalAlignment.LEFT);
final UImage dotd = new UImage(new PixelImage(PSystemVersion.getDotd(), AffineTransformType.TYPE_BILINEAR));
result = TextBlockUtils.mergeTB(result, dotd, HorizontalAlignment.LEFT);
2020-12-14 18:31:06 +00:00
}
2020-09-19 15:43:24 +00:00
return result;
2015-04-07 18:18:37 +00:00
}
public ShapeType getShapeType() {
return ShapeType.RECTANGLE;
}
2017-04-05 17:37:42 +00:00
public Margins getShield(StringBounder stringBounder) {
return Margins.NONE;
2015-04-07 18:18:37 +00:00
}
2020-06-07 10:03:18 +00:00
2018-12-22 11:11:40 +00:00
public double getOverscanX(StringBounder stringBounder) {
return 0;
}
2013-12-10 19:36:50 +00:00
}