1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-10 20:22:26 +00:00
plantuml/src/net/sourceforge/plantuml/command/PSystemCommandFactory.java

220 lines
6.6 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
2017-03-15 19:13:31 +00:00
* 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
*
2013-12-10 19:36:50 +00:00
*
*/
package net.sourceforge.plantuml.command;
2022-12-16 16:20:14 +00:00
import java.util.ArrayList;
2013-12-10 19:36:50 +00:00
import java.util.List;
2022-09-20 20:35:41 +00:00
import java.util.Map;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.AbstractPSystem;
2022-11-04 17:36:03 +00:00
import net.sourceforge.plantuml.EmbeddedDiagram;
2013-12-10 19:36:50 +00:00
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;
2022-03-19 12:48:23 +00:00
import net.sourceforge.plantuml.annotation.HaxeIgnored;
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;
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
2020-09-30 20:57:58 +00:00
public abstract class PSystemCommandFactory extends PSystemAbstractFactory {
2013-12-10 19:36:50 +00:00
2022-12-16 16:20:14 +00:00
private final List<Command> cmds = new ArrayList<>();
2013-12-10 19:36:50 +00:00
2022-12-16 16:20:14 +00:00
protected abstract void initCommandsList(List<Command> cmds);
2022-01-28 21:45:34 +00:00
2022-09-20 20:35:41 +00:00
public abstract AbstractPSystem createEmptyDiagram(UmlSource source, Map<String, String> skinParam);
2022-01-28 21:45:34 +00:00
2022-03-19 12:48:23 +00:00
@HaxeIgnored
2020-09-30 20:57:58 +00:00
protected PSystemCommandFactory() {
2013-12-10 19:36:50 +00:00
this(DiagramType.UML);
}
2020-09-30 20:57:58 +00:00
protected PSystemCommandFactory(DiagramType type) {
2013-12-10 19:36:50 +00:00
super(type);
}
2021-05-23 15:35:13 +00:00
@Override
2022-09-20 20:35:41 +00:00
final public Diagram createSystem(UmlSource source, Map<String, String> skinParam) {
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();
2022-01-28 21:45:34 +00:00
if (StartUtils.isArobaseStartDiagram(startLine.getString()) == false)
2013-12-10 19:36:50 +00:00
throw new UnsupportedOperationException();
if (source.isEmpty()) {
2022-01-28 21:45:34 +00:00
if (it.hasNext())
2019-06-26 19:24:49 +00:00
it.next();
2022-01-28 21:45:34 +00:00
2019-05-24 19:59:31 +00:00
return buildEmptyError(source, startLine.getLocation(), it.getTrace());
2013-12-10 19:36:50 +00:00
}
2022-09-18 17:08:06 +00:00
AbstractPSystem sys = createEmptyDiagram(source, skinParam);
2013-12-10 19:36:50 +00:00
while (it.hasNext()) {
2019-03-29 22:14:07 +00:00
if (StartUtils.isArobaseEndDiagram(it.peek().getString())) {
2022-01-28 21:45:34 +00:00
if (sys == null)
2016-05-31 19:41:55 +00:00
return null;
2022-01-28 21:45:34 +00:00
2016-05-31 19:41:55 +00:00
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();
2022-01-28 21:45:34 +00:00
if (sys.isOk() == false)
2013-12-10 19:36:50 +00:00
return null;
2022-01-28 21:45:34 +00:00
2013-12-10 19:36:50 +00:00
return sys;
}
2018-06-12 20:50:45 +00:00
sys = executeFewLines(sys, source, it);
2022-01-28 21:45:34 +00:00
if (sys instanceof PSystemError)
2013-12-10 19:36:50 +00:00
return sys;
2022-01-28 21:45:34 +00:00
2013-12-10 19:36:50 +00:00
}
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) {
2021-02-02 10:12:15 +00:00
final ErrorUml err = new ErrorUml(ErrorUmlType.SYNTAX_ERROR, "Syntax Error?", 0, 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) {
2021-02-02 10:12:15 +00:00
final LineLocation location = ((StringLocated) step.blocLines.getFirst()).getLocation();
final ErrorUml err = new ErrorUml(ErrorUmlType.EXECUTION_ERROR, result.getError(), result.getScore(),
location);
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
}
2022-01-28 21:45:34 +00:00
if (result.getNewDiagram() != null)
2018-06-12 20:50:45 +00:00
sys = result.getNewDiagram();
2022-01-28 21:45:34 +00:00
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) {
final BlocLines single = BlocLines.single(it.peek());
2022-12-16 16:20:14 +00:00
synchronized (cmds) {
if (cmds.size() == 0)
initCommandsList(cmds);
}
2022-01-28 21:45:34 +00:00
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);
2022-01-28 21:45:34 +00:00
if (lines == null)
2018-06-12 20:50:45 +00:00
continue;
2022-01-28 21:45:34 +00:00
2018-06-12 20:50:45 +00:00
it.copyStateFrom(cloned);
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) {
2022-03-19 12:48:23 +00:00
BlocLines lines = BlocLines.create();
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);
2022-01-28 21:45:34 +00:00
if (result == CommandControl.NOT_OK)
2015-05-31 18:56:03 +00:00
return null;
2022-01-28 21:45:34 +00:00
if (result == CommandControl.OK)
2015-05-31 18:56:03 +00:00
return lines;
2022-01-28 21:45:34 +00:00
2016-05-31 19:41:55 +00:00
nb++;
2022-01-28 21:45:34 +00:00
if (cmd instanceof CommandDecoratorMultine && nb > ((CommandDecoratorMultine) cmd).getNbMaxLines())
2016-05-31 19:41:55 +00:00
return null;
2022-01-28 21:45:34 +00:00
2015-05-31 18:56:03 +00:00
}
return null;
2013-12-10 19:36:50 +00:00
}
2022-11-04 17:36:03 +00:00
private static BlocLines addOneSingleLineManageEmbedded2(IteratorCounter2 it, BlocLines lines) {
2019-03-29 22:14:07 +00:00
final StringLocated linetoBeAdded = it.next();
lines = lines.add(linetoBeAdded);
2022-11-04 17:36:03 +00:00
if (EmbeddedDiagram.getEmbeddedType(linetoBeAdded.getTrimmed().getString()) != null) {
int nested = 1;
2015-04-07 18:18:37 +00:00
while (it.hasNext()) {
2019-03-29 22:14:07 +00:00
final StringLocated s = it.next();
lines = lines.add(s);
2022-11-04 17:36:03 +00:00
if (EmbeddedDiagram.getEmbeddedType(s.getTrimmed().getString()) != null)
// if (s.getTrimmed().getString().startsWith(EmbeddedDiagram.EMBEDDED_START))
nested++;
else if (s.getTrimmed().getString().equals(EmbeddedDiagram.EMBEDDED_END)) {
nested--;
if (nested == 0)
return lines;
}
2015-04-07 18:18:37 +00:00
}
}
2022-11-04 17:36:03 +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
}