1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-30 07:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/command/UmlDiagramFactory.java

282 lines
8.6 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.sourceforge.plantuml.AbstractPSystem;
import net.sourceforge.plantuml.ErrorUml;
import net.sourceforge.plantuml.ErrorUmlType;
2019-05-24 19:59:31 +00:00
import net.sourceforge.plantuml.LineLocation;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.StringLocated;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.classdiagram.command.CommandHideShowByGender;
import net.sourceforge.plantuml.classdiagram.command.CommandHideShowByVisibility;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.DiagramType;
import net.sourceforge.plantuml.core.UmlSource;
2019-05-24 19:59:31 +00:00
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.error.PSystemErrorUtils;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.sequencediagram.command.CommandSkin;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.sprite.CommandListSprite;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.statediagram.command.CommandHideEmptyDescription;
2019-09-14 18:12:04 +00:00
import net.sourceforge.plantuml.style.CommandStyleImport;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.style.CommandStyleMultilinesCSS;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.utils.StartUtils;
2015-06-20 10:54:49 +00:00
import net.sourceforge.plantuml.version.IteratorCounter2;
2013-12-10 19:36:50 +00:00
public abstract class UmlDiagramFactory extends PSystemAbstractFactory {
2019-06-26 19:24:49 +00:00
private List<Command> cmds;
2013-12-10 19:36:50 +00:00
protected UmlDiagramFactory() {
this(DiagramType.UML);
}
protected UmlDiagramFactory(DiagramType type) {
super(type);
}
final public Diagram createSystem(UmlSource source) {
2015-06-20 10:54:49 +00:00
final IteratorCounter2 it = source.iterator2();
2019-03-29 22:14:07 +00:00
final StringLocated startLine = it.next();
if (StartUtils.isArobaseStartDiagram(startLine.getString()) == false) {
2013-12-10 19:36:50 +00:00
throw new UnsupportedOperationException();
}
if (source.isEmpty()) {
2019-06-26 19:24:49 +00:00
if (it.hasNext()) {
it.next();
}
2019-05-24 19:59:31 +00:00
return buildEmptyError(source, startLine.getLocation(), it.getTrace());
2013-12-10 19:36:50 +00:00
}
AbstractPSystem sys = createEmptyDiagram();
while (it.hasNext()) {
2019-03-29 22:14:07 +00:00
if (StartUtils.isArobaseEndDiagram(it.peek().getString())) {
2016-05-31 19:41:55 +00:00
if (sys == null) {
return null;
}
final String err = sys.checkFinalError();
2013-12-10 19:36:50 +00:00
if (err != null) {
2019-05-24 19:59:31 +00:00
final LineLocation location = it.next().getLocation();
return buildExecutionError(source, err, location, it.getTrace());
2013-12-10 19:36:50 +00:00
}
if (source.getTotalLineCount() == 2) {
2019-05-24 19:59:31 +00:00
final LineLocation location = it.next().getLocation();
return buildEmptyError(source, location, it.getTrace());
2013-12-10 19:36:50 +00:00
}
sys.makeDiagramReady();
if (sys.isOk() == false) {
return null;
}
sys.setSource(source);
return sys;
}
2018-06-12 20:50:45 +00:00
sys = executeFewLines(sys, source, it);
2013-12-10 19:36:50 +00:00
if (sys instanceof PSystemError) {
return sys;
}
}
sys.setSource(source);
return sys;
}
2018-06-12 20:50:45 +00:00
private AbstractPSystem executeFewLines(AbstractPSystem sys, UmlSource source, final IteratorCounter2 it) {
final Step step = getCandidate(it);
if (step == null) {
final ErrorUml err = new ErrorUml(ErrorUmlType.SYNTAX_ERROR, "Syntax Error?", it.peek().getLocation());
2019-05-24 19:59:31 +00:00
it.next();
return PSystemErrorUtils.buildV2(source, err, null, it.getTrace());
2018-06-12 20:50:45 +00:00
}
2013-12-10 19:36:50 +00:00
2018-06-12 20:50:45 +00:00
final CommandExecutionResult result = sys.executeCommand(step.command, step.blocLines);
if (result.isOk() == false) {
final ErrorUml err = new ErrorUml(ErrorUmlType.EXECUTION_ERROR, result.getError(),
2019-03-29 22:14:07 +00:00
((StringLocated) step.blocLines.getFirst499()).getLocation());
2019-05-24 19:59:31 +00:00
sys = PSystemErrorUtils.buildV2(source, err, result.getDebugLines(), it.getTrace());
2018-06-12 20:50:45 +00:00
}
if (result.getNewDiagram() != null) {
sys = result.getNewDiagram();
2013-12-10 19:36:50 +00:00
}
return sys;
2018-06-12 20:50:45 +00:00
2013-12-10 19:36:50 +00:00
}
2018-06-12 20:50:45 +00:00
static class Step {
final Command command;
final BlocLines blocLines;
Step(Command command, BlocLines blocLines) {
this.command = command;
this.blocLines = blocLines;
2015-05-31 18:56:03 +00:00
}
2018-06-12 20:50:45 +00:00
2015-05-31 18:56:03 +00:00
}
2018-06-12 20:50:45 +00:00
private Step getCandidate(final IteratorCounter2 it) {
2019-03-29 22:14:07 +00:00
final BlocLines single = BlocLines.single2(it.peek());
2019-06-26 19:24:49 +00:00
if (cmds == null) {
cmds = createCommands();
}
2015-05-31 18:56:03 +00:00
for (Command cmd : cmds) {
2018-06-12 20:50:45 +00:00
final CommandControl result = cmd.isValid(single);
2015-05-31 18:56:03 +00:00
if (result == CommandControl.OK) {
2018-06-12 20:50:45 +00:00
it.next();
return new Step(cmd, single);
2013-12-10 19:36:50 +00:00
}
2018-06-12 20:50:45 +00:00
if (result == CommandControl.OK_PARTIAL) {
final IteratorCounter2 cloned = it.cloneMe();
final BlocLines lines = isMultilineCommandOk(cloned, cmd);
if (lines == null) {
continue;
2016-01-09 12:15:40 +00:00
}
2018-06-12 20:50:45 +00:00
it.copyStateFrom(cloned);
assert lines != null;
return new Step(cmd, lines);
2013-12-10 19:36:50 +00:00
}
}
2018-06-12 20:50:45 +00:00
return null;
2015-05-31 18:56:03 +00:00
}
2013-12-10 19:36:50 +00:00
2015-06-20 10:54:49 +00:00
private BlocLines isMultilineCommandOk(IteratorCounter2 it, Command cmd) {
BlocLines lines = new BlocLines();
2016-05-31 19:41:55 +00:00
int nb = 0;
2015-05-31 18:56:03 +00:00
while (it.hasNext()) {
2015-06-20 10:54:49 +00:00
lines = addOneSingleLineManageEmbedded2(it, lines);
2015-05-31 18:56:03 +00:00
final CommandControl result = cmd.isValid(lines);
if (result == CommandControl.NOT_OK) {
return null;
}
if (result == CommandControl.OK) {
return lines;
}
2016-05-31 19:41:55 +00:00
nb++;
if (cmd instanceof CommandDecoratorMultine && nb > ((CommandDecoratorMultine) cmd).getNbMaxLines()) {
return null;
}
2015-05-31 18:56:03 +00:00
}
return null;
2013-12-10 19:36:50 +00:00
}
2015-06-20 10:54:49 +00:00
private BlocLines addOneSingleLineManageEmbedded2(IteratorCounter2 it, BlocLines lines) {
2019-03-29 22:14:07 +00:00
final StringLocated linetoBeAdded = it.next();
2015-06-20 10:54:49 +00:00
lines = lines.add2(linetoBeAdded);
2019-06-26 19:24:49 +00:00
if (linetoBeAdded.getTrimmed().getString().equals("{{")) {
2015-04-07 18:18:37 +00:00
while (it.hasNext()) {
2019-03-29 22:14:07 +00:00
final StringLocated s = it.next();
2015-06-20 10:54:49 +00:00
lines = lines.add2(s);
2019-06-26 19:24:49 +00:00
if (s.getTrimmed().getString().equals("}}")) {
2015-06-20 10:54:49 +00:00
return lines;
2015-04-07 18:18:37 +00:00
}
}
}
2015-06-20 10:54:49 +00:00
return lines;
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
// -----------------------------------
protected abstract List<Command> createCommands();
public abstract AbstractPSystem createEmptyDiagram();
2019-04-21 20:40:01 +00:00
final protected void addCommonCommands1(List<Command> cmds) {
addTitleCommands(cmds);
addCommonCommands2(cmds);
addCommonHides(cmds);
2019-08-26 17:07:21 +00:00
cmds.add(new CommandStyleMultilinesCSS());
2019-09-14 18:12:04 +00:00
cmds.add(new CommandStyleImport());
2019-04-21 20:40:01 +00:00
}
2013-12-10 19:36:50 +00:00
2019-04-21 20:40:01 +00:00
final protected void addCommonCommands2(List<Command> cmds) {
2019-09-22 17:20:16 +00:00
// cmds.add(new CommandListSprite());
2019-04-21 20:40:01 +00:00
cmds.add(new CommandNope());
cmds.add(new CommandPragma());
2013-12-10 19:36:50 +00:00
cmds.add(new CommandSkinParam());
cmds.add(new CommandSkinParamMultilines());
2019-04-21 20:40:01 +00:00
cmds.add(new CommandSkin());
2013-12-10 19:36:50 +00:00
cmds.add(new CommandMinwidth());
cmds.add(new CommandRotate());
cmds.add(new CommandScale());
cmds.add(new CommandScaleWidthAndHeight());
cmds.add(new CommandScaleWidthOrHeight());
2016-02-07 21:13:01 +00:00
cmds.add(new CommandScaleMaxWidth());
cmds.add(new CommandScaleMaxHeight());
2015-09-06 17:28:59 +00:00
cmds.add(new CommandScaleMaxWidthAndHeight());
2015-04-07 18:18:37 +00:00
cmds.add(new CommandAffineTransform());
cmds.add(new CommandAffineTransformMultiline());
2013-12-10 19:36:50 +00:00
final FactorySpriteCommand factorySpriteCommand = new FactorySpriteCommand();
2015-09-06 17:28:59 +00:00
cmds.add(factorySpriteCommand.createMultiLine(false));
2013-12-10 19:36:50 +00:00
cmds.add(factorySpriteCommand.createSingleLine());
cmds.add(new CommandSpriteFile());
2019-04-21 20:40:01 +00:00
}
2015-05-31 18:56:03 +00:00
2019-04-21 20:40:01 +00:00
final protected void addCommonHides(List<Command> cmds) {
2019-06-26 19:24:49 +00:00
cmds.add(new CommandHideEmptyDescription());
2019-04-21 20:40:01 +00:00
cmds.add(new CommandHideUnlinked());
2016-11-04 21:39:29 +00:00
cmds.add(new CommandHideShowByVisibility());
cmds.add(new CommandHideShowByGender());
2019-04-21 20:40:01 +00:00
}
final protected void addTitleCommands(List<Command> cmds) {
cmds.add(new CommandTitle());
cmds.add(new CommandMainframe());
cmds.add(new CommandCaption());
cmds.add(new CommandMultilinesTitle());
cmds.add(new CommandMultilinesLegend());
2013-12-10 19:36:50 +00:00
2019-04-21 20:40:01 +00:00
cmds.add(new CommandFooter());
cmds.add(new CommandMultilinesFooter());
cmds.add(new CommandHeader());
cmds.add(new CommandMultilinesHeader());
2013-12-10 19:36:50 +00:00
}
final public List<String> getDescription() {
final List<String> result = new ArrayList<String>();
for (Command cmd : createCommands()) {
result.addAll(Arrays.asList(cmd.getDescription()));
}
return Collections.unmodifiableList(result);
}
}