1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-28 22:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/UmlDiagram.java

460 lines
15 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;
2018-09-23 12:15:14 +00:00
import java.awt.Color;
2013-12-10 19:36:50 +00:00
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
2011-04-19 16:50:40 +00:00
import java.awt.image.BufferedImage;
2011-08-08 17:48:29 +00:00
import java.io.BufferedOutputStream;
2013-12-10 19:36:50 +00:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
2011-04-19 16:50:40 +00:00
import java.io.File;
import java.io.FileNotFoundException;
2011-08-08 17:48:29 +00:00
import java.io.FileOutputStream;
2011-04-19 16:50:40 +00:00
import java.io.IOException;
2019-04-21 20:40:01 +00:00
import java.io.InputStream;
2011-04-19 16:50:40 +00:00
import java.io.OutputStream;
import java.io.PrintWriter;
2010-11-15 20:35:36 +00:00
import java.util.List;
2013-12-10 19:36:50 +00:00
import javax.imageio.ImageIO;
2015-04-07 18:18:37 +00:00
import javax.script.ScriptException;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.anim.Animation;
import net.sourceforge.plantuml.anim.AnimationDecoder;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.api.ImageDataSimple;
2019-01-16 18:34:41 +00:00
import net.sourceforge.plantuml.command.BlocLines;
import net.sourceforge.plantuml.command.CommandControl;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2019-01-16 18:34:41 +00:00
import net.sourceforge.plantuml.command.CommandSkinParam;
import net.sourceforge.plantuml.command.CommandSkinParamMultilines;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2018-05-06 19:59:19 +00:00
import net.sourceforge.plantuml.cucadiagram.DisplaySection;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.UnparsableGraphvizException;
import net.sourceforge.plantuml.flashcode.FlashCodeFactory;
import net.sourceforge.plantuml.flashcode.FlashCodeUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.fun.IconLoader;
import net.sourceforge.plantuml.graphic.GraphicPosition;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
2017-02-15 21:34:36 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.UDrawable;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.mjpeg.MJPEGGenerator;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.pdf.PdfConverter;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.sprite.Sprite;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.EmptySvgException;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.svek.GraphvizCrash;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UImage;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.version.Version;
2010-11-15 20:35:36 +00:00
2019-04-21 20:40:01 +00:00
public abstract class UmlDiagram extends TitledDiagram implements Diagram, Annotated, WithSprite {
2010-11-15 20:35:36 +00:00
private boolean rotation;
2011-02-14 11:56:34 +00:00
private boolean hideUnlinkedData;
2010-11-15 20:35:36 +00:00
private int minwidth = Integer.MAX_VALUE;
private final Pragma pragma = new Pragma();
2015-04-07 18:18:37 +00:00
private Animation animation;
2010-11-15 20:35:36 +00:00
2019-03-01 22:16:29 +00:00
private final SkinParam skinParam;
public UmlDiagram() {
this.skinParam = SkinParam.create(getUmlDiagramType());
}
public UmlDiagram(ISkinSimple orig) {
this();
if (orig != null) {
this.skinParam.copyAllFrom(orig);
}
}
2010-11-15 20:35:36 +00:00
final public int getMinwidth() {
return minwidth;
}
final public void setMinwidth(int minwidth) {
this.minwidth = minwidth;
}
final public boolean isRotation() {
return rotation;
}
final public void setRotation(boolean rotation) {
this.rotation = rotation;
}
public final ISkinParam getSkinParam() {
return skinParam;
}
public void setParam(String key, String value) {
2015-04-07 18:18:37 +00:00
skinParam.setParam(StringUtils.goLowerCase(key), value);
2010-11-15 20:35:36 +00:00
}
2018-05-06 19:59:19 +00:00
public final DisplaySection getFooterOrHeaderTeoz(FontParam param) {
2015-05-31 18:56:03 +00:00
if (param == FontParam.FOOTER) {
return getFooter();
}
if (param == FontParam.HEADER) {
return getHeader();
}
throw new IllegalArgumentException();
}
2010-11-15 20:35:36 +00:00
abstract public UmlDiagramType getUmlDiagramType();
public Pragma getPragma() {
return pragma;
}
2015-06-20 10:54:49 +00:00
final public void setAnimation(Iterable<CharSequence> animationData) {
2015-04-07 18:18:37 +00:00
try {
final AnimationDecoder animationDecoder = new AnimationDecoder(animationData);
this.animation = Animation.create(animationDecoder.decode());
} catch (ScriptException e) {
e.printStackTrace();
}
}
final public Animation getAnimation() {
return animation;
}
2019-03-29 22:14:07 +00:00
public final double getScaleCoef(FileFormatOption fileFormatOption) {
2011-01-05 18:23:06 +00:00
if (getSkinParam().getDpi() == 96) {
2019-03-29 22:14:07 +00:00
return fileFormatOption.getScaleCoef();
2011-01-05 18:23:06 +00:00
}
2019-03-29 22:14:07 +00:00
return getSkinParam().getDpi() * fileFormatOption.getScaleCoef() / 96.0;
2011-01-05 18:23:06 +00:00
}
2011-02-14 11:56:34 +00:00
public final boolean isHideUnlinkedData() {
return hideUnlinkedData;
}
public final void setHideUnlinkedData(boolean hideUnlinkedData) {
this.hideUnlinkedData = hideUnlinkedData;
}
2016-12-01 20:29:25 +00:00
@Override
2017-04-19 18:30:16 +00:00
final protected ImageData exportDiagramNow(OutputStream os, int index, FileFormatOption fileFormatOption, long seed)
2011-04-19 16:50:40 +00:00
throws IOException {
2013-12-10 19:36:50 +00:00
2017-02-15 21:34:36 +00:00
final HtmlColor hover = getSkinParam().getHoverPathColor();
2018-11-26 18:46:22 +00:00
if (fileFormatOption.getSvgLinkTarget() == null || fileFormatOption.getSvgLinkTarget().equals("_top")) {
2018-09-23 12:15:14 +00:00
fileFormatOption = fileFormatOption.withSvgLinkTarget(getSkinParam().getSvgLinkTarget());
}
2017-10-07 09:46:53 +00:00
fileFormatOption = fileFormatOption.withTikzFontDistortion(getSkinParam().getTikzFontDistortion());
2017-02-15 21:34:36 +00:00
if (hover != null) {
fileFormatOption = fileFormatOption.withHoverColor(StringUtils.getAsHtml(getSkinParam().getColorMapper()
.getMappedColor(hover)));
}
2016-09-29 19:51:18 +00:00
2011-08-08 17:48:29 +00:00
if (fileFormatOption.getFileFormat() == FileFormat.PDF) {
2015-04-07 18:18:37 +00:00
return exportDiagramInternalPdf(os, index);
2013-12-10 19:36:50 +00:00
}
2015-04-07 18:18:37 +00:00
2013-12-10 19:36:50 +00:00
try {
2015-04-07 18:18:37 +00:00
final ImageData imageData = exportDiagramInternal(os, index, fileFormatOption);
2013-12-10 19:36:50 +00:00
this.lastInfo = new Dimension2DDouble(imageData.getWidth(), imageData.getHeight());
return imageData;
} catch (UnparsableGraphvizException e) {
e.printStackTrace();
2017-04-19 18:30:16 +00:00
exportDiagramError(os, e.getCause(), fileFormatOption, seed, e.getGraphvizVersion());
2013-12-10 19:36:50 +00:00
} catch (Exception e) {
e.printStackTrace();
2017-04-19 18:30:16 +00:00
exportDiagramError(os, e, fileFormatOption, seed, null);
2013-12-10 19:36:50 +00:00
}
2018-05-01 17:26:04 +00:00
return ImageDataSimple.error();
2011-04-19 16:50:40 +00:00
}
2017-04-19 18:30:16 +00:00
private void exportDiagramError(OutputStream os, Throwable exception, FileFormatOption fileFormat, long seed,
2015-08-05 20:17:01 +00:00
String graphvizVersion) throws IOException {
2017-04-19 18:30:16 +00:00
exportDiagramError(os, exception, fileFormat, seed, getMetadata(), getFlashData(),
2017-07-03 17:59:53 +00:00
getFailureText1(exception, graphvizVersion, getFlashData()));
2015-08-05 20:17:01 +00:00
}
2015-04-07 18:18:37 +00:00
2017-04-19 18:30:16 +00:00
public static void exportDiagramError(OutputStream os, Throwable exception, FileFormatOption fileFormat, long seed,
2015-08-05 20:17:01 +00:00
String metadata, String flash, List<String> strings) throws IOException {
2016-11-18 21:12:09 +00:00
if (fileFormat.getFileFormat() == FileFormat.ATXT || fileFormat.getFileFormat() == FileFormat.UTXT) {
exportDiagramErrorText(os, exception, strings);
return;
}
2015-05-31 18:56:03 +00:00
strings.addAll(CommandExecutionResult.getStackTrace(exception));
2015-04-07 18:18:37 +00:00
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), 1.0, HtmlColorUtils.WHITE,
2015-08-05 20:17:01 +00:00
metadata, null, 0, 0, null, false);
2015-04-07 18:18:37 +00:00
final FlashCodeUtils utils = FlashCodeFactory.getFlashCodeUtils();
2018-09-23 12:15:14 +00:00
final BufferedImage im = utils.exportFlashcode(flash, Color.BLACK, Color.WHITE);
2015-04-07 18:18:37 +00:00
if (im != null) {
GraphvizCrash.addDecodeHint(strings);
}
2016-11-18 21:12:09 +00:00
final TextBlockBackcolored graphicStrings = GraphicStrings.createBlackOnWhite(strings, IconLoader.getRandom(),
2015-04-07 18:18:37 +00:00
GraphicPosition.BACKGROUND_CORNER_TOP_RIGHT);
if (im == null) {
2016-03-06 16:47:34 +00:00
imageBuilder.setUDrawable(graphicStrings);
2015-04-07 18:18:37 +00:00
} else {
2016-03-06 16:47:34 +00:00
imageBuilder.setUDrawable(new UDrawable() {
2015-04-07 18:18:37 +00:00
public void drawU(UGraphic ug) {
graphicStrings.drawU(ug);
final double height = graphicStrings.calculateDimension(ug.getStringBounder()).getHeight();
ug = ug.apply(new UTranslate(0, height));
2018-07-27 21:56:46 +00:00
ug.draw(new UImage(im).scaleNearestNeighbor(3));
2015-04-07 18:18:37 +00:00
}
});
}
2017-04-19 18:30:16 +00:00
imageBuilder.writeImageTOBEMOVED(fileFormat, seed, os);
2015-04-07 18:18:37 +00:00
}
2016-11-18 21:12:09 +00:00
private static void exportDiagramErrorText(OutputStream os, Throwable exception, List<String> strings) {
final PrintWriter pw = new PrintWriter(os);
exception.printStackTrace(pw);
pw.println();
pw.println();
for (String s : strings) {
s = s.replaceAll("\\</?\\w+?\\>", "");
pw.println(s);
}
pw.flush();
}
2016-03-06 16:47:34 +00:00
public String getFlashData() {
2015-04-07 18:18:37 +00:00
final UmlSource source = getSource();
2016-05-31 19:41:55 +00:00
if (source == null) {
return "";
}
return source.getPlainString();
2015-04-07 18:18:37 +00:00
}
2017-07-03 17:59:53 +00:00
static private List<String> getFailureText1(Throwable exception, String graphvizVersion, String textDiagram) {
final List<String> strings = GraphvizCrash.anErrorHasOccured(exception, textDiagram);
2013-12-10 19:36:50 +00:00
strings.add("PlantUML (" + Version.versionString() + ") cannot parse result from dot/GraphViz.");
if (exception instanceof EmptySvgException) {
strings.add("Because dot/GraphViz returns an empty string.");
}
2016-05-11 21:31:47 +00:00
GraphvizCrash.checkOldVersionWarning(strings);
2013-12-10 19:36:50 +00:00
if (graphvizVersion != null) {
strings.add(" ");
strings.add("GraphViz version used : " + graphvizVersion);
}
2016-05-11 21:31:47 +00:00
GraphvizCrash.pleaseGoTo(strings);
2015-04-07 18:18:37 +00:00
GraphvizCrash.addProperties(strings);
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.thisMayBeCaused(strings);
2013-12-10 19:36:50 +00:00
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.youShouldSendThisDiagram(strings);
2013-12-10 19:36:50 +00:00
strings.add(" ");
2015-04-07 18:18:37 +00:00
return strings;
2013-12-10 19:36:50 +00:00
}
2017-07-03 17:59:53 +00:00
public static List<String> getFailureText2(Throwable exception, String textDiagram) {
final List<String> strings = GraphvizCrash.anErrorHasOccured(exception, textDiagram);
2015-08-05 20:17:01 +00:00
strings.add("PlantUML (" + Version.versionString() + ") has crashed.");
2016-05-11 21:31:47 +00:00
GraphvizCrash.checkOldVersionWarning(strings);
2015-08-05 20:17:01 +00:00
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.youShouldSendThisDiagram(strings);
2015-08-05 20:17:01 +00:00
strings.add(" ");
return strings;
}
2013-12-10 19:36:50 +00:00
private void exportDiagramInternalMjpeg(OutputStream os) throws IOException {
final File f = new File("c:/test.avi");
final int nb = 150;
final double framerate = 30;
final MJPEGGenerator m = new MJPEGGenerator(f, 640, 480, framerate, nb);
for (int i = 0; i < nb; i++) {
final AffineTransform at = new AffineTransform();
final double coef = (nb - 1 - i) * 1.0 / nb;
at.setToShear(coef, coef);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// exportDiagramTOxxBEREMOVED(baos, null, 0, new FileFormatOption(FileFormat.PNG, at));
baos.close();
final BufferedImage im = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
m.addImage(im);
}
m.finishAVI();
}
private Dimension2D lastInfo;
2015-04-07 18:18:37 +00:00
private ImageData exportDiagramInternalPdf(OutputStream os, int index) throws IOException {
2011-08-08 17:48:29 +00:00
final File svg = FileUtils.createTempFile("pdf", ".svf");
final File pdfFile = FileUtils.createTempFile("pdf", ".pdf");
final OutputStream fos = new BufferedOutputStream(new FileOutputStream(svg));
2013-12-10 19:36:50 +00:00
final ImageData result = exportDiagram(fos, index, new FileFormatOption(FileFormat.SVG));
2011-08-08 17:48:29 +00:00
fos.close();
PdfConverter.convert(svg, pdfFile);
FileUtils.copyToStream(pdfFile, os);
2013-12-10 19:36:50 +00:00
return result;
2011-08-08 17:48:29 +00:00
}
2015-04-07 18:18:37 +00:00
protected abstract ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException;
2011-04-19 16:50:40 +00:00
2017-04-19 18:30:16 +00:00
final protected void exportCmap(SuggestedFile suggestedFile, int index, final ImageData cmapdata)
throws FileNotFoundException {
final String name = changeName(suggestedFile.getFile(index).getAbsolutePath());
2013-12-10 19:36:50 +00:00
final File cmapFile = new File(name);
2011-04-19 16:50:40 +00:00
PrintWriter pw = null;
try {
2013-12-10 19:36:50 +00:00
if (PSystemUtils.canFileBeWritten(cmapFile) == false) {
return;
}
2011-04-19 16:50:40 +00:00
pw = new PrintWriter(cmapFile);
2013-12-10 19:36:50 +00:00
pw.print(cmapdata.getCMapData(cmapFile.getName().substring(0, cmapFile.getName().length() - 6)));
2011-04-19 16:50:40 +00:00
pw.close();
} finally {
if (pw != null) {
pw.close();
}
}
}
static String changeName(String name) {
return name.replaceAll("(?i)\\.\\w{3}$", ".cmapx");
}
2013-12-10 19:36:50 +00:00
@Override
public String getWarningOrError() {
if (lastInfo == null) {
return null;
2011-04-19 16:50:40 +00:00
}
2013-12-10 19:36:50 +00:00
final double actualWidth = lastInfo.getWidth();
if (actualWidth == 0) {
return null;
2011-04-19 16:50:40 +00:00
}
2013-12-10 19:36:50 +00:00
final String value = getSkinParam().getValue("widthwarning");
if (value == null) {
return null;
}
if (value.matches("\\d+") == false) {
return null;
}
final int widthwarning = Integer.parseInt(value);
if (actualWidth > widthwarning) {
return "The image is " + ((int) actualWidth) + " pixel width. (Warning limit is " + widthwarning + ")";
}
return null;
2011-04-19 16:50:40 +00:00
}
2013-12-10 19:36:50 +00:00
public void addSprite(String name, Sprite sprite) {
skinParam.addSprite(name, sprite);
2011-04-19 16:50:40 +00:00
}
2016-03-06 16:47:34 +00:00
private boolean useJDot;
public void setUseJDot(boolean useJDot) {
this.useJDot = useJDot;
}
2017-10-07 09:46:53 +00:00
2018-10-21 19:44:14 +00:00
public static final boolean FORCE_JDOT = false;
2016-03-06 16:47:34 +00:00
public boolean isUseJDot() {
2018-10-21 19:44:14 +00:00
if (FORCE_JDOT)
return true;
2016-03-06 16:47:34 +00:00
return useJDot;
}
2016-04-04 19:05:10 +00:00
2019-08-26 17:07:21 +00:00
public CommandExecutionResult loadSkin(String newSkin) throws IOException {
getSkinParam().setDefaultSkin(newSkin + ".skin");
2019-01-16 18:34:41 +00:00
return CommandExecutionResult.ok();
2019-08-26 17:07:21 +00:00
// final String res = "/skin/" + filename + ".skin";
// final InputStream internalIs = UmlDiagram.class.getResourceAsStream(res);
// if (internalIs != null) {
// final BlocLines lines2 = BlocLines.load(internalIs, new LineLocationImpl(filename, null));
// return loadSkinInternal(lines2);
// }
// if (OptionFlags.ALLOW_INCLUDE == false) {
// return CommandExecutionResult.ok();
// }
// final File f = FileSystem.getInstance().getFile(filename + ".skin");
// if (f == null || f.exists() == false || f.canRead() == false) {
// return CommandExecutionResult.error("Cannot load skin from " + filename);
// }
// final BlocLines lines = BlocLines.load(f, new LineLocationImpl(f.getName(), null));
// return loadSkinInternal(lines);
}
// private CommandExecutionResult loadSkinInternal(final BlocLines lines) {
// final CommandSkinParam cmd1 = new CommandSkinParam();
// final CommandSkinParamMultilines cmd2 = new CommandSkinParamMultilines();
// for (int i = 0; i < lines.size(); i++) {
// final BlocLines ext1 = lines.subList(i, i + 1);
// if (cmd1.isValid(ext1) == CommandControl.OK) {
// cmd1.execute(this, ext1);
// } else if (cmd2.isValid(ext1) == CommandControl.OK_PARTIAL) {
// i = tryMultilines(cmd2, i, lines);
// }
// }
// return CommandExecutionResult.ok();
// }
2019-01-16 18:34:41 +00:00
private int tryMultilines(CommandSkinParamMultilines cmd2, int i, BlocLines lines) {
for (int j = i + 1; j <= lines.size(); j++) {
final BlocLines ext1 = lines.subList(i, j);
if (cmd2.isValid(ext1) == CommandControl.OK) {
cmd2.execute(this, ext1);
return j;
} else if (cmd2.isValid(ext1) == CommandControl.NOT_OK) {
return j;
}
}
return i;
}
2019-08-26 17:07:21 +00:00
2019-06-26 19:24:49 +00:00
public void setHideEmptyDescription(boolean hideEmptyDescription) {
}
2010-11-15 20:35:36 +00:00
}