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

155 lines
6.1 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +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:
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2011-08-08 17:48:29 +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
2011-08-08 17:48:29 +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
2022-08-17 17:34:24 +00:00
*
2011-08-08 17:48:29 +00:00
*
*/
package net.sourceforge.plantuml.svek;
import java.io.IOException;
import java.io.OutputStream;
2013-12-10 19:36:50 +00:00
import java.util.ArrayList;
2011-08-08 17:48:29 +00:00
import java.util.List;
2022-10-05 20:32:57 +00:00
import net.sourceforge.plantuml.AnnotatedBuilder;
2016-01-30 12:20:07 +00:00
import net.sourceforge.plantuml.AnnotatedWorker;
2016-05-11 21:31:47 +00:00
import net.sourceforge.plantuml.BaseFile;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.FileFormatOption;
2016-05-11 21:31:47 +00:00
import net.sourceforge.plantuml.NamedOutputStream;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.UmlDiagramType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.ImageData;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.cucadiagram.CucaDiagram;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Link;
import net.sourceforge.plantuml.cucadiagram.dot.CucaDiagramSimplifierActivity;
import net.sourceforge.plantuml.cucadiagram.dot.CucaDiagramSimplifierState;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.DotData;
2016-08-25 20:45:37 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2013-12-10 19:36:50 +00:00
public final class CucaDiagramFileMakerSvek implements CucaDiagramFileMaker {
2011-08-08 17:48:29 +00:00
private final CucaDiagram diagram;
2015-04-07 18:18:37 +00:00
public CucaDiagramFileMakerSvek(CucaDiagram diagram) throws IOException {
2011-08-08 17:48:29 +00:00
this.diagram = diagram;
}
2013-12-10 19:36:50 +00:00
public ImageData createFile(OutputStream os, List<String> dotStrings, FileFormatOption fileFormatOption)
2011-08-08 17:48:29 +00:00
throws IOException {
try {
2011-09-07 20:41:58 +00:00
return createFileInternal(os, dotStrings, fileFormatOption);
2011-08-08 17:48:29 +00:00
} catch (InterruptedException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2013-12-10 19:36:50 +00:00
throw new IOException(e);
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
}
2018-08-26 12:09:50 +00:00
private GeneralImageBuilder createDotDataImageBuilder(DotMode dotMode, StringBounder stringBounder) {
2015-04-07 18:18:37 +00:00
final DotData dotData = new DotData(diagram.getEntityFactory().getRootGroup(), getOrderedLinks(),
diagram.getLeafsvalues(), diagram.getUmlDiagramType(), diagram.getSkinParam(), diagram, diagram,
2022-09-20 20:35:41 +00:00
diagram.getEntityFactory(), diagram.isHideEmptyDescriptionForState(), dotMode,
2022-08-26 16:00:28 +00:00
diagram.getNamespaceSeparator(), diagram.getPragma());
2020-02-18 21:24:31 +00:00
final boolean intricated = diagram.mergeIntricated();
return new GeneralImageBuilder(intricated, dotData, diagram.getEntityFactory(), diagram.getSource(),
diagram.getPragma(), stringBounder, diagram.getUmlDiagramType().getStyleName());
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
private ImageData createFileInternal(OutputStream os, List<String> dotStrings, FileFormatOption fileFormatOption)
2011-08-08 17:48:29 +00:00
throws IOException, InterruptedException {
2020-12-06 21:43:09 +00:00
final StringBounder stringBounder = fileFormatOption.getDefaultStringBounder(diagram.getSkinParam());
2022-08-19 16:34:21 +00:00
if (diagram.getUmlDiagramType() == UmlDiagramType.ACTIVITY)
2020-12-06 21:43:09 +00:00
new CucaDiagramSimplifierActivity(diagram, dotStrings, stringBounder);
2022-08-19 16:34:21 +00:00
else if (diagram.getUmlDiagramType() == UmlDiagramType.STATE)
2020-12-06 21:43:09 +00:00
new CucaDiagramSimplifierState(diagram, dotStrings, stringBounder);
2011-08-08 17:48:29 +00:00
2020-12-06 21:43:09 +00:00
GeneralImageBuilder svek2 = createDotDataImageBuilder(DotMode.NORMAL, stringBounder);
2016-05-11 21:31:47 +00:00
BaseFile basefile = null;
2022-08-19 16:34:21 +00:00
if (fileFormatOption.isDebugSvek() && os instanceof NamedOutputStream)
2016-05-11 21:31:47 +00:00
basefile = ((NamedOutputStream) os).getBasefile();
2016-07-25 19:25:28 +00:00
TextBlockBackcolored result = svek2.buildImage(basefile, diagram.getDotStringSkek());
2013-12-10 19:36:50 +00:00
if (result instanceof GraphvizCrash) {
2020-12-06 21:43:09 +00:00
svek2 = createDotDataImageBuilder(DotMode.NO_LEFT_RIGHT_AND_XLABEL, stringBounder);
2016-07-25 19:25:28 +00:00
result = svek2.buildImage(basefile, diagram.getDotStringSkek());
2013-12-10 19:36:50 +00:00
}
2022-08-19 16:34:21 +00:00
// TODO There is something strange with the left margin of mainframe, I think
// because AnnotatedWorker is used here
// It can be looked at in another PR
2022-10-05 20:32:57 +00:00
final AnnotatedBuilder builder = new AnnotatedBuilder(diagram, diagram.getSkinParam(), stringBounder);
result = new AnnotatedWorker(diagram, diagram.getSkinParam(), stringBounder, builder).addAdd(result);
2011-08-08 17:48:29 +00:00
2022-08-19 16:34:21 +00:00
// TODO UmlDiagram.getWarningOrError() looks similar so this might be
// simplified? - will leave for a separate PR
2013-12-10 19:36:50 +00:00
final String widthwarning = diagram.getSkinParam().getValue("widthwarning");
2016-07-25 19:25:28 +00:00
String warningOrError = null;
2022-08-19 16:34:21 +00:00
if (widthwarning != null && widthwarning.matches("\\d+"))
2016-07-25 19:25:28 +00:00
warningOrError = svek2.getWarningOrError(Integer.parseInt(widthwarning));
2022-08-19 16:34:21 +00:00
// Sorry about this hack. There is a side effect in
// SvekResult::calculateDimension()
result.calculateDimension(stringBounder); // Ensure text near the margins is not cut off
return diagram.createImageBuilder(fileFormatOption).annotations(false) // backwards compatibility
// (AnnotatedWorker is used above)
.drawable(result).status(result instanceof GraphvizCrash ? 503 : 0).warningOrError(warningOrError)
.write(os);
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
private List<Link> getOrderedLinks() {
2021-05-14 08:42:57 +00:00
final List<Link> result = new ArrayList<>();
2022-08-19 16:34:21 +00:00
for (Link l : diagram.getLinks())
2013-12-10 19:36:50 +00:00
addLinkNew(result, l);
2022-08-19 16:34:21 +00:00
2013-12-10 19:36:50 +00:00
return result;
}
2011-09-07 20:41:58 +00:00
2013-12-10 19:36:50 +00:00
private void addLinkNew(List<Link> result, Link link) {
for (int i = 0; i < result.size(); i++) {
final Link other = result.get(i);
if (other.sameConnections(link)) {
2022-08-19 16:34:21 +00:00
while (i < result.size() && result.get(i).sameConnections(link))
2013-12-10 19:36:50 +00:00
i++;
2022-08-19 16:34:21 +00:00
if (i == result.size())
2013-12-10 19:36:50 +00:00
result.add(link);
2022-08-19 16:34:21 +00:00
else
2013-12-10 19:36:50 +00:00
result.add(i, link);
2022-08-19 16:34:21 +00:00
2013-12-10 19:36:50 +00:00
return;
}
}
result.add(link);
}
2011-09-07 20:41:58 +00:00
2011-08-08 17:48:29 +00:00
}