1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/UmlDiagram.java

446 lines
14 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
*
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;
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;
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;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
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;
2016-01-30 12:20:07 +00:00
import net.sourceforge.plantuml.cucadiagram.DisplayPositionned;
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;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
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;
import net.sourceforge.plantuml.graphic.VerticalAlignment;
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;
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;
2016-01-30 12:20:07 +00:00
import net.sourceforge.plantuml.ugraphic.sprite.Sprite;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.version.Version;
2010-11-15 20:35:36 +00:00
2016-01-30 12:20:07 +00:00
public abstract class UmlDiagram extends AbstractPSystem implements Diagram, Annotated {
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;
2016-01-30 12:20:07 +00:00
private DisplayPositionned title = DisplayPositionned.none(HorizontalAlignment.CENTER, VerticalAlignment.TOP);
private DisplayPositionned caption = DisplayPositionned.none(HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
private DisplayPositionned header = DisplayPositionned.none(HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
private DisplayPositionned footer = DisplayPositionned.none(HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
private DisplayPositionned legend = DisplayPositionned.none(HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
2013-12-10 19:36:50 +00:00
2010-11-15 20:35:36 +00:00
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
2017-02-15 21:34:36 +00:00
private final SkinParam skinParam = SkinParam.create(getUmlDiagramType());
2010-11-15 20:35:36 +00:00
2016-01-30 12:20:07 +00:00
final public void setTitle(DisplayPositionned title) {
2018-03-09 21:37:34 +00:00
if (DisplayPositionned.isNull(title) || title.getDisplay().isWhite()) {
return;
}
2016-01-30 12:20:07 +00:00
this.title = title;
2010-11-15 20:35:36 +00:00
}
2016-01-30 12:20:07 +00:00
final public void setCaption(DisplayPositionned caption) {
this.caption = caption;
}
final public DisplayPositionned getCaption() {
return caption;
}
@Override
final public DisplayPositionned getTitle() {
2010-11-15 20:35:36 +00:00
return title;
}
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
}
2016-01-30 12:20:07 +00:00
public final DisplayPositionned getHeader() {
2010-11-15 20:35:36 +00:00
return header;
}
2016-01-30 12:20:07 +00:00
public final void setHeader(DisplayPositionned header) {
2010-11-15 20:35:36 +00:00
this.header = header;
}
2016-01-30 12:20:07 +00:00
public final DisplayPositionned getFooter() {
2010-11-15 20:35:36 +00:00
return footer;
}
2016-01-30 12:20:07 +00:00
public final void setFooter(DisplayPositionned footer) {
2010-11-15 20:35:36 +00:00
this.footer = footer;
}
2016-01-30 12:20:07 +00:00
public final DisplayPositionned 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;
}
2011-01-05 18:23:06 +00:00
public final double getDpiFactor(FileFormatOption fileFormatOption) {
if (getSkinParam().getDpi() == 96) {
return 1.0;
}
return getSkinParam().getDpi() / 96.0;
}
public final int getDpi(FileFormatOption fileFormatOption) {
return getSkinParam().getDpi();
}
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();
2016-09-29 19:51:18 +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
}
return new ImageDataSimple();
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();
final BufferedImage im = utils.exportFlashcode(flash);
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));
ug.draw(new UImage(im));
}
});
}
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-01-30 12:20:07 +00:00
public final DisplayPositionned getLegend() {
2013-12-10 19:36:50 +00:00
return legend;
2011-04-19 16:50:40 +00:00
}
2016-01-30 12:20:07 +00:00
public final void setLegend(DisplayPositionned legend) {
2013-12-10 19:36:50 +00:00
this.legend = legend;
}
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
2016-03-06 16:47:34 +00:00
public boolean isUseJDot() {
return useJDot;
}
2016-04-04 19:05:10 +00:00
public void setDotExecutable(String dotExecutable) {
skinParam.setDotExecutable(dotExecutable);
}
2010-11-15 20:35:36 +00:00
}