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

438 lines
14 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2015-04-07 18:18:37 +00:00
* (C) Copyright 2009-2014, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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
*
2015-07-11 09:32:49 +00:00
* Revision $Revision: 16549 $
2010-11-15 20:35:36 +00:00
*
*/
package net.sourceforge.plantuml;
2013-12-10 19:36:50 +00:00
import java.awt.Font;
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;
import java.util.ArrayList;
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;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
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;
import net.sourceforge.plantuml.graphic.HtmlColorUtils;
import net.sourceforge.plantuml.graphic.QuoteUtils;
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;
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.Sprite;
import net.sourceforge.plantuml.ugraphic.UAntiAliasing;
import net.sourceforge.plantuml.ugraphic.UFont;
2015-04-07 18:18:37 +00:00
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
2013-12-10 19:36:50 +00:00
public abstract class UmlDiagram extends AbstractPSystem implements Diagram {
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;
2015-07-11 09:32:49 +00:00
private Display title = Display.NULL;
private Display header = Display.NULL;
private Display footer = Display.NULL;
private Display legend = Display.NULL;
2013-12-10 19:36:50 +00:00
private HorizontalAlignment legendAlignment = HorizontalAlignment.CENTER;
2015-04-07 18:18:37 +00:00
private VerticalAlignment legendVerticalAlignment = VerticalAlignment.BOTTOM;
2013-12-10 19:36:50 +00:00
private HorizontalAlignment headerAlignment = HorizontalAlignment.RIGHT;
private HorizontalAlignment footerAlignment = HorizontalAlignment.CENTER;
2010-11-15 20:35:36 +00:00
private final Pragma pragma = new Pragma();
private Scale scale;
2015-04-07 18:18:37 +00:00
private Animation animation;
2010-11-15 20:35:36 +00:00
2015-04-07 18:18:37 +00:00
private final SkinParam skinParam = new SkinParam();
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
final public void setTitle(Display strings) {
2010-11-15 20:35:36 +00:00
this.title = strings;
}
2013-12-10 19:36:50 +00:00
final public Display 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
}
2013-12-10 19:36:50 +00:00
public final Display getHeader() {
2010-11-15 20:35:36 +00:00
return header;
}
2013-12-10 19:36:50 +00:00
public final void setHeader(Display header) {
2010-11-15 20:35:36 +00:00
this.header = header;
}
2013-12-10 19:36:50 +00:00
public final Display getFooter() {
2010-11-15 20:35:36 +00:00
return footer;
}
2013-12-10 19:36:50 +00:00
public final void setFooter(Display footer) {
2010-11-15 20:35:36 +00:00
this.footer = footer;
}
2013-12-10 19:36:50 +00:00
public final HorizontalAlignment getHeaderAlignment() {
return headerAlignment;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public final void setHeaderAlignment(HorizontalAlignment headerAlignment) {
this.headerAlignment = headerAlignment;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public final HorizontalAlignment getFooterAlignment() {
return footerAlignment;
2010-11-15 20:35:36 +00:00
}
2015-05-31 18:56:03 +00:00
public final HorizontalAlignment getAlignmentTeoz(FontParam param) {
if (param == FontParam.FOOTER) {
return getFooterAlignment();
}
if (param == FontParam.HEADER) {
return getHeaderAlignment();
}
throw new IllegalArgumentException();
}
public final Display getFooterOrHeaderTeoz(FontParam param) {
if (param == FontParam.FOOTER) {
return getFooter();
}
if (param == FontParam.HEADER) {
return getHeader();
}
throw new IllegalArgumentException();
}
2013-12-10 19:36:50 +00:00
public final void setFooterAlignment(HorizontalAlignment footerAlignment) {
this.footerAlignment = footerAlignment;
2010-11-15 20:35:36 +00:00
}
abstract public UmlDiagramType getUmlDiagramType();
public Pragma getPragma() {
return pragma;
}
final public void setScale(Scale scale) {
this.scale = scale;
}
2011-01-05 18:23:06 +00:00
2010-11-15 20:35:36 +00:00
final public Scale getScale() {
return scale;
}
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;
}
2013-12-10 19:36:50 +00:00
final public ImageData exportDiagram(OutputStream os, int index, FileFormatOption fileFormatOption)
2011-04-19 16:50:40 +00:00
throws IOException {
2013-12-10 19:36:50 +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();
exportDiagramError(os, e.getCause(), fileFormatOption, e.getGraphvizVersion(), e.getDebugData());
} catch (Exception e) {
e.printStackTrace();
exportDiagramError(os, e, fileFormatOption, null, null);
}
return new ImageDataSimple();
2011-04-19 16:50:40 +00:00
}
2013-12-10 19:36:50 +00:00
private void exportDiagramError(OutputStream os, Throwable exception, FileFormatOption fileFormat,
String graphvizVersion, String svg) throws IOException {
final UFont font = new UFont("SansSerif", Font.PLAIN, 12);
2015-04-07 18:18:37 +00:00
final List<String> strings = getFailureText(exception, graphvizVersion);
final String flash = getFlashData();
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,
getMetadata(), null, 0, 0, null, getSkinParam().handwritten());
final FlashCodeUtils utils = FlashCodeFactory.getFlashCodeUtils();
final BufferedImage im = utils.exportFlashcode(flash);
if (im != null) {
GraphvizCrash.addDecodeHint(strings);
}
final GraphicStrings graphicStrings = new GraphicStrings(strings, font, HtmlColorUtils.BLACK,
HtmlColorUtils.WHITE, UAntiAliasing.ANTI_ALIASING_ON, IconLoader.getRandom(),
GraphicPosition.BACKGROUND_CORNER_TOP_RIGHT);
if (im == null) {
imageBuilder.addUDrawable(graphicStrings);
} else {
imageBuilder.addUDrawable(new UDrawable() {
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));
}
});
}
2015-05-31 18:56:03 +00:00
imageBuilder.writeImageTOBEMOVED(fileFormat, os);
2015-04-07 18:18:37 +00:00
}
private String getFlashData() {
// for (Map.Entry<Object, Object> ent : System.getProperties().entrySet()) {
// System.err.println("p1=" + ent.getKey() + " " + ent.getValue());
// }
final StringBuilder result = new StringBuilder();
final UmlSource source = getSource();
result.append(source.getPlainString());
return result.toString();
}
private List<String> getFailureText(Throwable exception, String graphvizVersion) {
2013-12-10 19:36:50 +00:00
final List<String> strings = new ArrayList<String>();
strings.add("An error has occured : " + exception);
final String quote = QuoteUtils.getSomeQuote();
strings.add("<i>" + quote);
strings.add(" ");
strings.add("PlantUML (" + Version.versionString() + ") cannot parse result from dot/GraphViz.");
if (exception instanceof EmptySvgException) {
strings.add("Because dot/GraphViz returns an empty string.");
}
if (graphvizVersion != null) {
strings.add(" ");
strings.add("GraphViz version used : " + graphvizVersion);
}
strings.add(" ");
2015-04-07 18:18:37 +00:00
GraphvizCrash.addProperties(strings);
strings.add(" ");
2013-12-10 19:36:50 +00:00
strings.add("This may be caused by :");
strings.add(" - a bug in PlantUML");
strings.add(" - a problem in GraphViz");
strings.add(" ");
strings.add("You should send this diagram and this image to <b>plantuml@gmail.com</b> to solve this issue.");
strings.add("You can try to turn arround this issue by simplifing your diagram.");
strings.add(" ");
2015-04-07 18:18:37 +00:00
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
2013-12-10 19:36:50 +00:00
final protected void exportCmap(File suggestedFile, final ImageData cmapdata) throws FileNotFoundException {
final String name = changeName(suggestedFile.getAbsolutePath());
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
}
2013-12-10 19:36:50 +00:00
public final Display getLegend() {
return legend;
2011-04-19 16:50:40 +00:00
}
2013-12-10 19:36:50 +00:00
public final HorizontalAlignment getLegendAlignment() {
return legendAlignment;
2011-04-19 16:50:40 +00:00
}
2015-04-07 18:18:37 +00:00
public final VerticalAlignment getLegendVerticalAlignment() {
return legendVerticalAlignment;
}
public final void setLegend(Display legend, HorizontalAlignment horizontalAlignment, VerticalAlignment valignment) {
2013-12-10 19:36:50 +00:00
this.legend = legend;
this.legendAlignment = horizontalAlignment;
2015-04-07 18:18:37 +00:00
this.legendVerticalAlignment = valignment;
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
}