1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-31 15:40:48 +00:00
plantuml/src/net/sourceforge/plantuml/activitydiagram/command/CommandLinkActivity.java

269 lines
10 KiB
Java
Raw Normal View History

2011-01-05 18:23:06 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2011-01-05 18:23:06 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2011-01-05 18:23:06 +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
*
2011-01-05 18:23:06 +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-01-05 18:23:06 +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.activitydiagram.command;
import net.sourceforge.plantuml.Direction;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.LineLocation;
2015-09-06 17:28:59 +00:00
import net.sourceforge.plantuml.StringUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.UrlBuilder;
import net.sourceforge.plantuml.UrlBuilder.ModeUrl;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.activitydiagram.ActivityDiagram;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.classdiagram.command.CommandLinkClass;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.command.regex.IRegex;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.command.regex.RegexConcat;
import net.sourceforge.plantuml.command.regex.RegexLeaf;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexOptional;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.command.regex.RegexOr;
import net.sourceforge.plantuml.command.regex.RegexPartialMatch;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexResult;
import net.sourceforge.plantuml.cucadiagram.Code;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.cucadiagram.GroupType;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.cucadiagram.IEntity;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.LeafType;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.cucadiagram.Link;
import net.sourceforge.plantuml.cucadiagram.LinkDecor;
import net.sourceforge.plantuml.cucadiagram.LinkType;
2018-03-09 21:37:34 +00:00
import net.sourceforge.plantuml.cucadiagram.NamespaceStrategy;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2017-10-07 09:46:53 +00:00
import net.sourceforge.plantuml.descdiagram.command.CommandLinkElement;
2015-09-06 17:28:59 +00:00
import net.sourceforge.plantuml.graphic.color.ColorParser;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
2011-01-05 18:23:06 +00:00
public class CommandLinkActivity extends SingleLineCommand2<ActivityDiagram> {
2013-12-10 19:36:50 +00:00
public CommandLinkActivity() {
super(getRegexConcat());
2011-01-05 18:23:06 +00:00
}
2011-01-09 19:00:05 +00:00
2019-06-26 19:24:49 +00:00
private static IRegex getRegexConcat() {
return RegexConcat.build(CommandLinkActivity.class.getName(), RegexLeaf.start(), //
2013-12-10 19:36:50 +00:00
new RegexOptional(//
new RegexOr("FIRST", //
new RegexLeaf("STAR", "(\\(\\*(top)?\\))"), //
new RegexLeaf("CODE", "([\\p{L}0-9][\\p{L}0-9_.]*)"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("BAR", "(?:==+)[%s]*([\\p{L}0-9_.]+)[%s]*(?:==+)"), //
new RegexLeaf("QUOTED", "[%g]([^%g]+)[%g](?:[%s]+as[%s]+([\\p{L}0-9_.]+))?"))), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2011-02-14 11:56:34 +00:00
new RegexLeaf("STEREOTYPE", "(\\<\\<.*\\>\\>)?"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2015-09-06 17:28:59 +00:00
ColorParser.exp2(), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("URL", "(" + UrlBuilder.getRegexp() + ")?"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("ARROW_BODY1", "([-.]+)"), //
2017-10-07 09:46:53 +00:00
new RegexLeaf("ARROW_STYLE1", "(?:\\[(" + CommandLinkElement.LINE_STYLE + ")\\])?"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("ARROW_DIRECTION", "(\\*|left|right|up|down|le?|ri?|up?|do?)?"), //
2017-10-07 09:46:53 +00:00
new RegexLeaf("ARROW_STYLE2", "(?:\\[(" + CommandLinkElement.LINE_STYLE + ")\\])?"), //
2019-07-14 20:09:26 +00:00
new RegexLeaf("ARROW_BODY2", "([-.]*)"), //
new RegexLeaf("\\>"), //
2015-04-07 18:18:37 +00:00
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional(new RegexLeaf("BRACKET", "\\[([^\\]*]+[^\\]]*)\\]")), //
RegexLeaf.spaceZeroOrMore(), //
2013-12-10 19:36:50 +00:00
new RegexOr("FIRST2", //
2015-04-07 18:18:37 +00:00
new RegexLeaf("STAR2", "(\\(\\*(top|\\d+)?\\))"), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("OPENBRACKET2", "(\\{)"), //
new RegexLeaf("CODE2", "([\\p{L}0-9][\\p{L}0-9_.]*)"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("BAR2", "(?:==+)[%s]*([\\p{L}0-9_.]+)[%s]*(?:==+)"), //
new RegexLeaf("QUOTED2", "[%g]([^%g]+)[%g](?:[%s]+as[%s]+([\\p{L}0-9][\\p{L}0-9_.]*))?"), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("QUOTED_INVISIBLE2", "(\\w.*?)")), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2011-02-14 11:56:34 +00:00
new RegexLeaf("STEREOTYPE2", "(\\<\\<.*\\>\\>)?"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional( //
new RegexConcat( //
new RegexLeaf("in"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("PARTITION2", "([%g][^%g]+[%g]|\\S+)") //
)), //
RegexLeaf.spaceZeroOrMore(), //
2015-09-06 17:28:59 +00:00
ColorParser.exp3(), //
2019-06-26 19:24:49 +00:00
RegexLeaf.end());
2011-01-05 18:23:06 +00:00
}
2011-01-09 19:00:05 +00:00
2011-01-05 18:23:06 +00:00
@Override
2019-03-29 22:14:07 +00:00
protected CommandExecutionResult executeArg(ActivityDiagram diagram, LineLocation location, RegexResult arg) {
2015-04-07 18:18:37 +00:00
final IEntity entity1 = getEntity(diagram, arg, true);
2011-01-09 19:00:05 +00:00
if (entity1 == null) {
return CommandExecutionResult.error("No such activity");
}
2015-04-07 18:18:37 +00:00
if (arg.get("STEREOTYPE", 0) != null) {
entity1.setStereotype(new Stereotype(arg.get("STEREOTYPE", 0)));
2011-01-05 18:23:06 +00:00
}
2015-04-07 18:18:37 +00:00
if (arg.get("BACKCOLOR", 0) != null) {
2015-09-28 20:42:17 +00:00
entity1.setSpecificColorTOBEREMOVED(ColorType.BACK, diagram.getSkinParam().getIHtmlColorSet()
2015-04-07 18:18:37 +00:00
.getColorIfValid(arg.get("BACKCOLOR", 0)));
2011-01-05 18:23:06 +00:00
}
2015-04-07 18:18:37 +00:00
final IEntity entity2 = getEntity(diagram, arg, false);
2011-01-09 19:00:05 +00:00
if (entity2 == null) {
return CommandExecutionResult.error("No such activity");
}
2015-04-07 18:18:37 +00:00
if (arg.get("BACKCOLOR2", 0) != null) {
2015-09-28 20:42:17 +00:00
entity2.setSpecificColorTOBEREMOVED(ColorType.BACK, diagram.getSkinParam().getIHtmlColorSet()
2015-04-07 18:18:37 +00:00
.getColorIfValid(arg.get("BACKCOLOR2", 0)));
2011-01-05 18:23:06 +00:00
}
2015-04-07 18:18:37 +00:00
if (arg.get("STEREOTYPE2", 0) != null) {
entity2.setStereotype(new Stereotype(arg.get("STEREOTYPE2", 0)));
2011-01-05 18:23:06 +00:00
}
2015-04-07 18:18:37 +00:00
final Display linkLabel = Display.getWithNewlines(arg.get("BRACKET", 0));
final String arrowBody1 = CommandLinkClass.notNull(arg.get("ARROW_BODY1", 0));
final String arrowBody2 = CommandLinkClass.notNull(arg.get("ARROW_BODY2", 0));
final String arrowDirection = CommandLinkClass.notNull(arg.get("ARROW_DIRECTION", 0));
2011-01-05 18:23:06 +00:00
2015-04-07 18:18:37 +00:00
final String arrow = StringUtils.manageArrowForCuca(arrowBody1 + arrowDirection + arrowBody2 + ">");
2011-04-19 16:50:40 +00:00
int lenght = arrow.length() - 1;
2015-04-07 18:18:37 +00:00
if (arrowDirection.contains("*")) {
2011-04-19 16:50:40 +00:00
lenght = 2;
}
2011-01-05 18:23:06 +00:00
2013-12-10 19:36:50 +00:00
LinkType type = new LinkType(LinkDecor.ARROW, LinkDecor.NONE);
2015-04-07 18:18:37 +00:00
if ((arrowBody1 + arrowBody2).contains(".")) {
2017-10-07 09:46:53 +00:00
type = type.goDotted();
2013-12-10 19:36:50 +00:00
}
2019-07-14 20:09:26 +00:00
Link link = new Link(entity1, entity2, type, linkLabel, lenght, diagram.getSkinParam().getCurrentStyleBuilder());
2015-04-07 18:18:37 +00:00
if (arrowDirection.contains("*")) {
2011-04-19 16:50:40 +00:00
link.setConstraint(false);
}
2015-04-07 18:18:37 +00:00
final Direction direction = StringUtils.getArrowDirection(arrowBody1 + arrowDirection + arrowBody2 + ">");
2011-01-05 18:23:06 +00:00
if (direction == Direction.LEFT || direction == Direction.UP) {
link = link.getInv();
}
2015-04-07 18:18:37 +00:00
if (arg.get("URL", 0) != null) {
2013-12-10 19:36:50 +00:00
final UrlBuilder urlBuilder = new UrlBuilder(diagram.getSkinParam().getValue("topurl"), ModeUrl.STRICT);
2015-04-07 18:18:37 +00:00
final Url urlLink = urlBuilder.getUrl(arg.get("URL", 0));
2013-12-10 19:36:50 +00:00
link.setUrl(urlLink);
}
2011-01-05 18:23:06 +00:00
2018-06-25 19:05:58 +00:00
link.applyStyle(arg.getLazzy("ARROW_STYLE", 0));
2013-12-10 19:36:50 +00:00
diagram.addLink(link);
2011-01-05 18:23:06 +00:00
return CommandExecutionResult.ok();
}
2013-12-10 19:36:50 +00:00
static IEntity getEntity(ActivityDiagram system, RegexResult arg, final boolean start) {
2011-01-05 18:23:06 +00:00
final String suf = start ? "" : "2";
2013-12-10 19:36:50 +00:00
final String openBracket2 = arg.get("OPENBRACKET" + suf, 0);
if (openBracket2 != null) {
2011-01-05 18:23:06 +00:00
return system.createInnerActivity();
}
2013-12-10 19:36:50 +00:00
if (arg.get("STAR" + suf, 0) != null) {
2015-04-07 18:18:37 +00:00
final String suppId = arg.get("STAR" + suf, 1);
2011-01-05 18:23:06 +00:00
if (start) {
2015-04-07 18:18:37 +00:00
if (suppId != null) {
2011-02-14 11:56:34 +00:00
system.getStart().setTop(true);
}
2011-01-05 18:23:06 +00:00
return system.getStart();
}
2015-04-07 18:18:37 +00:00
return system.getEnd(suppId);
2011-01-05 18:23:06 +00:00
}
2013-12-10 19:36:50 +00:00
String partition = arg.get("PARTITION" + suf, 0);
if (partition != null) {
partition = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(partition);
}
final Code code = Code.of(arg.get("CODE" + suf, 0));
2011-01-05 18:23:06 +00:00
if (code != null) {
2013-12-10 19:36:50 +00:00
if (partition != null) {
2018-03-09 21:37:34 +00:00
system.gotoGroup2(Code.of(partition), Display.getWithNewlines(partition), GroupType.PACKAGE,
system.getRootGroup(), NamespaceStrategy.SINGLE);
2013-12-10 19:36:50 +00:00
}
final IEntity result = system.getOrCreate(code, Display.getWithNewlines(code),
2015-04-07 18:18:37 +00:00
CommandLinkActivity.getTypeIfExisting(system, code));
2013-12-10 19:36:50 +00:00
if (partition != null) {
system.endGroup();
}
return result;
2011-01-05 18:23:06 +00:00
}
2013-12-10 19:36:50 +00:00
final String bar = arg.get("BAR" + suf, 0);
2011-01-05 18:23:06 +00:00
if (bar != null) {
2013-12-10 19:36:50 +00:00
return system.getOrCreate(Code.of(bar), Display.getWithNewlines(bar), LeafType.SYNCHRO_BAR);
2011-01-05 18:23:06 +00:00
}
final RegexPartialMatch quoted = arg.get("QUOTED" + suf);
if (quoted.get(0) != null) {
2013-12-10 19:36:50 +00:00
final Code quotedCode = Code.of(quoted.get(1) == null ? quoted.get(0) : quoted.get(1));
if (partition != null) {
2018-03-09 21:37:34 +00:00
system.gotoGroup2(Code.of(partition), Display.getWithNewlines(partition), GroupType.PACKAGE,
system.getRootGroup(), NamespaceStrategy.SINGLE);
2013-12-10 19:36:50 +00:00
}
final IEntity result = system.getOrCreate(quotedCode, Display.getWithNewlines(quoted.get(0)),
2015-04-07 18:18:37 +00:00
CommandLinkActivity.getTypeIfExisting(system, quotedCode));
2013-12-10 19:36:50 +00:00
if (partition != null) {
system.endGroup();
}
return result;
2011-01-05 18:23:06 +00:00
}
2013-12-10 19:36:50 +00:00
final Code quotedInvisible = Code.of(arg.get("QUOTED_INVISIBLE" + suf, 0));
if (quotedInvisible != null) {
if (partition != null) {
2018-03-09 21:37:34 +00:00
system.gotoGroup2(Code.of(partition), Display.getWithNewlines(partition), GroupType.PACKAGE,
system.getRootGroup(), NamespaceStrategy.SINGLE);
2013-12-10 19:36:50 +00:00
}
final IEntity result = system.getOrCreate(quotedInvisible, Display.getWithNewlines(quotedInvisible),
LeafType.ACTIVITY);
if (partition != null) {
system.endGroup();
}
return result;
}
final String first = arg.get("FIRST" + suf, 0);
2011-01-05 18:23:06 +00:00
if (first == null) {
return system.getLastEntityConsulted();
}
2013-12-10 19:36:50 +00:00
return null;
2011-01-05 18:23:06 +00:00
}
2013-12-10 19:36:50 +00:00
static LeafType getTypeIfExisting(ActivityDiagram system, Code code) {
if (system.leafExist(code)) {
2015-04-07 18:18:37 +00:00
final IEntity ent = system.getLeafsget(code);
2017-04-05 17:37:42 +00:00
if (ent.getLeafType() == LeafType.BRANCH) {
2013-12-10 19:36:50 +00:00
return LeafType.BRANCH;
2011-01-05 18:23:06 +00:00
}
}
2013-12-10 19:36:50 +00:00
return LeafType.ACTIVITY;
2011-01-05 18:23:06 +00:00
}
2011-01-09 19:00:05 +00:00
2011-01-05 18:23:06 +00:00
}