plantuml/src/net/sourceforge/plantuml/descdiagram/command/CommandLinkElement.java

324 lines
12 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
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
* Contribution : Hisashi Miyashita
2013-12-10 19:36:50 +00:00
*
*/
package net.sourceforge.plantuml.descdiagram.command;
import net.sourceforge.plantuml.Direction;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.LineLocation;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.StringUtils;
2013-12-10 19:36:50 +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;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexConcat;
import net.sourceforge.plantuml.command.regex.RegexLeaf;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.command.regex.RegexOptional;
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.IEntity;
import net.sourceforge.plantuml.cucadiagram.ILeaf;
2019-12-10 21:45:49 +00:00
import net.sourceforge.plantuml.cucadiagram.Ident;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link;
2022-08-24 16:46:33 +00:00
import net.sourceforge.plantuml.cucadiagram.LinkArg;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.LinkDecor;
import net.sourceforge.plantuml.cucadiagram.LinkType;
import net.sourceforge.plantuml.cucadiagram.Stereotype;
import net.sourceforge.plantuml.descdiagram.DescriptionDiagram;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.USymbol;
2022-02-17 18:27:32 +00:00
import net.sourceforge.plantuml.graphic.USymbols;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorParser;
import net.sourceforge.plantuml.graphic.color.ColorType;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
2013-12-10 19:36:50 +00:00
public class CommandLinkElement extends SingleLineCommand2<DescriptionDiagram> {
2019-01-16 18:34:41 +00:00
private static final String KEY1 = "dotted|dashed|plain|bold|hidden|norank|single|thickness=\\d+";
private static final String KEY2 = ",dotted|,dashed|,plain|,bold|,hidden|,norank|,single|,thickness=\\d+";
public static final String LINE_STYLE = "(?:#\\w+|" + CommandLinkElement.KEY1 + ")(?:,#\\w+|"
2017-10-07 09:46:53 +00:00
+ CommandLinkElement.KEY2 + ")*";
2019-01-16 18:34:41 +00:00
private static final String LINE_STYLE_MUTILPLES = LINE_STYLE + "(?:(?:;" + LINE_STYLE + ")*)";
public static final String STYLE_COLORS_MULTIPLES = "-\\[(" + LINE_STYLE_MUTILPLES + "*)\\]->";
2017-10-07 09:46:53 +00:00
2013-12-10 19:36:50 +00:00
public CommandLinkElement() {
super(getRegexConcat());
}
2019-06-26 19:24:49 +00:00
static IRegex getRegexConcat() {
return RegexConcat.build(CommandLinkElement.class.getName(), RegexLeaf.start(), //
2013-12-10 19:36:50 +00:00
getGroup("ENT1"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2020-05-17 21:15:50 +00:00
new RegexOptional(new RegexLeaf("FIRST_LABEL", "[%g]([^%g]+)[%g]")), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2020-08-25 17:24:17 +00:00
new RegexLeaf("HEAD2", "(0\\)|<<|<_|[<^*+#0@)]|<\\|[\\|\\:]?|[%s]+o)?"), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("BODY1", "([-=.~]+)"), //
2019-01-16 18:34:41 +00:00
new RegexLeaf("ARROW_STYLE1", "(?:\\[(" + LINE_STYLE_MUTILPLES + ")\\])?"), //
2019-06-26 19:24:49 +00:00
new RegexOptional(new RegexLeaf("DIRECTION", "(left|right|up|down|le?|ri?|up?|do?)(?=[-=.~0()])")), //
new RegexOptional(new RegexLeaf("INSIDE", "(0|\\(0\\)|\\(0|0\\))(?=[-=.~])")), //
2019-01-16 18:34:41 +00:00
new RegexLeaf("ARROW_STYLE2", "(?:\\[(" + LINE_STYLE + ")\\])?"), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("BODY2", "([-=.~]*)"), //
2020-08-25 17:24:17 +00:00
new RegexLeaf("HEAD1", "(\\(0|>>|_>|[>^*+#0@(]|[\\:\\|]?\\|>|\\\\\\\\|o[%s]+)?"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2020-05-17 21:15:50 +00:00
new RegexOptional(new RegexLeaf("SECOND_LABEL", "[%g]([^%g]+)[%g]")), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2013-12-10 19:36:50 +00:00
getGroup("ENT2"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2015-09-28 20:42:17 +00:00
color().getRegex(), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2015-09-28 20:42:17 +00:00
new RegexLeaf("STEREOTYPE", "(\\<\\<.*\\>\\>)?"), //
2021-03-09 18:02:38 +00:00
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf("LABEL_LINK", "(?::[%s]*(.+))?"), //
RegexLeaf.end());
2013-12-10 19:36:50 +00:00
}
2015-09-28 20:42:17 +00:00
private static ColorParser color() {
return ColorParser.simpleColor(ColorType.LINE);
}
2020-08-25 17:24:17 +00:00
private String getHead(RegexResult arg, final String key) {
String result = arg.get(key, 0);
result = trimAndLowerCase(result);
return result.replace("_", "");
}
2013-12-10 19:36:50 +00:00
private LinkType getLinkType(RegexResult arg) {
2020-08-25 17:24:17 +00:00
final String head1 = getHead(arg, "HEAD1");
final String head2 = getHead(arg, "HEAD2");
2013-12-10 19:36:50 +00:00
LinkDecor d1 = LinkDecor.NONE;
LinkDecor d2 = LinkDecor.NONE;
2022-04-10 19:24:55 +00:00
if (head1.equals("(0"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.CIRCLE_CONNECT;
2022-04-10 19:24:55 +00:00
else if (head1.equals("#"))
2017-04-05 17:37:42 +00:00
d1 = LinkDecor.SQUARE;
2022-04-10 19:24:55 +00:00
else if (head1.equals("0"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.CIRCLE;
2022-04-10 19:24:55 +00:00
else if (head1.equals("@"))
2019-12-10 21:45:49 +00:00
d1 = LinkDecor.CIRCLE_FILL;
2022-04-10 19:24:55 +00:00
else if (head1.equals("("))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.PARENTHESIS;
2022-04-10 19:24:55 +00:00
else if (head1.equals(">"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.ARROW;
2022-04-10 19:24:55 +00:00
else if (head1.equals("*"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.COMPOSITION;
2022-04-10 19:24:55 +00:00
else if (head1.equals("o"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.AGREGATION;
2022-04-10 19:24:55 +00:00
else if (head1.equals("+"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.PLUS;
2022-04-10 19:24:55 +00:00
else if (head1.equals("\\\\"))
2020-01-12 22:13:17 +00:00
d1 = LinkDecor.HALF_ARROW;
2022-04-10 19:24:55 +00:00
else if (head1.equals(">>"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.ARROW_TRIANGLE;
2022-04-10 19:24:55 +00:00
else if (head1.equals("^"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.EXTENDS;
2022-04-10 19:24:55 +00:00
else if (head1.equals(":|>"))
d1 = LinkDecor.DEFINEDBY;
2022-04-10 19:24:55 +00:00
else if (head1.equals("||>"))
d1 = LinkDecor.REDEFINES;
2022-04-10 19:24:55 +00:00
else if (head1.equals("|>"))
2013-12-10 19:36:50 +00:00
d1 = LinkDecor.EXTENDS;
2022-04-10 19:24:55 +00:00
if (head2.equals("0)"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.CIRCLE_CONNECT;
2022-04-10 19:24:55 +00:00
else if (head2.equals("#"))
2017-04-05 17:37:42 +00:00
d2 = LinkDecor.SQUARE;
2022-04-10 19:24:55 +00:00
else if (head2.equals("0"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.CIRCLE;
2022-04-10 19:24:55 +00:00
else if (head2.equals("@"))
2019-12-10 21:45:49 +00:00
d2 = LinkDecor.CIRCLE_FILL;
2022-04-10 19:24:55 +00:00
else if (head2.equals(")"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.PARENTHESIS;
2022-04-10 19:24:55 +00:00
else if (head2.equals("<"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.ARROW;
2022-04-10 19:24:55 +00:00
else if (head2.equals("*"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.COMPOSITION;
2022-04-10 19:24:55 +00:00
else if (head2.equals("o"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.AGREGATION;
2022-04-10 19:24:55 +00:00
else if (head2.equals("+"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.PLUS;
2022-04-10 19:24:55 +00:00
else if (head2.equals("<<"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.ARROW_TRIANGLE;
2022-04-10 19:24:55 +00:00
else if (head2.equals("^"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.EXTENDS;
2022-04-10 19:24:55 +00:00
else if (head2.equals("<|:"))
d2 = LinkDecor.DEFINEDBY;
2022-04-10 19:24:55 +00:00
else if (head2.equals("<||"))
d2 = LinkDecor.REDEFINES;
2022-04-10 19:24:55 +00:00
else if (head2.equals("<|"))
2013-12-10 19:36:50 +00:00
d2 = LinkDecor.EXTENDS;
LinkType result = new LinkType(d1, d2);
final String queue = getQueue(arg);
2022-04-10 19:24:55 +00:00
if (queue.contains("."))
2017-10-07 09:46:53 +00:00
result = result.goDashed();
2022-04-10 19:24:55 +00:00
else if (queue.contains("~"))
2017-10-07 09:46:53 +00:00
result = result.goDotted();
2022-04-10 19:24:55 +00:00
else if (queue.contains("="))
2017-10-07 09:46:53 +00:00
result = result.goBold();
2013-12-10 19:36:50 +00:00
final String middle = arg.get("INSIDE", 0);
2022-04-10 19:24:55 +00:00
if ("0".equals(middle))
2013-12-10 19:36:50 +00:00
result = result.withMiddleCircle();
2022-04-10 19:24:55 +00:00
else if ("0)".equals(middle))
2013-12-10 19:36:50 +00:00
result = result.withMiddleCircleCircled1();
2022-04-10 19:24:55 +00:00
else if ("(0".equals(middle))
2013-12-10 19:36:50 +00:00
result = result.withMiddleCircleCircled2();
2022-04-10 19:24:55 +00:00
else if ("(0)".equals(middle))
2013-12-10 19:36:50 +00:00
result = result.withMiddleCircleCircled();
2022-04-10 19:24:55 +00:00
2013-12-10 19:36:50 +00:00
return result;
}
private static String trimAndLowerCase(String s) {
2022-04-10 19:24:55 +00:00
if (s == null)
2013-12-10 19:36:50 +00:00
return "";
2022-04-10 19:24:55 +00:00
2015-05-31 18:56:03 +00:00
return StringUtils.goLowerCase(StringUtils.trin(s));
2013-12-10 19:36:50 +00:00
}
private Direction getDirection(RegexResult arg) {
final String dir = arg.get("DIRECTION", 0);
if (dir == null) {
return StringUtils.getQueueDirection(getQueue(arg));
// return Direction.DOWN;
}
return StringUtils.getQueueDirection(dir);
}
private String getQueue(RegexResult arg) {
return arg.get("BODY1", 0) + arg.get("BODY2", 0);
}
private static RegexLeaf getGroup(String name) {
return new RegexLeaf(name,
2021-05-23 15:35:13 +00:00
"([%pLN_.]+|\\(\\)[%s]*[%pLN_.]+|\\(\\)[%s]*[%g][^%g]+[%g]|:[^:]+:|(?!\\[\\*\\])\\[[^\\[\\]]+\\]|\\((?!\\*\\))[^)]+\\))");
2013-12-10 19:36:50 +00:00
}
@Override
2021-03-07 12:23:24 +00:00
protected CommandExecutionResult executeArg(DescriptionDiagram diagram, LineLocation location, RegexResult arg)
throws NoSuchColorException {
2019-12-10 21:45:49 +00:00
final String ent1String = arg.get("ENT1", 0);
final String ent2String = arg.get("ENT2", 0);
2020-01-12 22:13:17 +00:00
final Ident ident1 = diagram.buildFullyQualified(ent1String);
final Ident ident2 = diagram.buildFullyQualified(ent2String);
Ident ident1pure = Ident.empty().add(ent1String, diagram.getNamespaceSeparator());
Ident ident2pure = Ident.empty().add(ent2String, diagram.getNamespaceSeparator());
final Code code1 = diagram.V1972() ? ident1 : diagram.buildCode(ent1String);
final Code code2 = diagram.V1972() ? ident2 : diagram.buildCode(ent2String);
2013-12-10 19:36:50 +00:00
final LinkType linkType = getLinkType(arg);
final Direction dir = getDirection(arg);
final String queue;
2022-04-10 19:24:55 +00:00
if (dir == Direction.LEFT || dir == Direction.RIGHT)
2013-12-10 19:36:50 +00:00
queue = "-";
2022-04-10 19:24:55 +00:00
else
2013-12-10 19:36:50 +00:00
queue = getQueue(arg);
2022-04-10 19:24:55 +00:00
2013-12-10 19:36:50 +00:00
final Labels labels = new Labels(arg);
2020-01-12 22:13:17 +00:00
final IEntity cl1;
final IEntity cl2;
if (diagram.isGroup(code1) && diagram.isGroup(code2)) {
cl1 = diagram.V1972() ? diagram.getGroupStrict(diagram.buildLeafIdent(ent1String))
: diagram.getGroup(diagram.buildCode(ent1String));
cl2 = diagram.V1972() ? diagram.getGroupStrict(diagram.buildLeafIdent(ent2String))
: diagram.getGroup(diagram.buildCode(ent2String));
2020-01-12 22:13:17 +00:00
} else {
cl1 = getFoo1(diagram, code1, ident1, ident1pure);
cl2 = getFoo1(diagram, code2, ident2, ident2pure);
}
2022-08-24 16:46:33 +00:00
final LinkArg linkArg = LinkArg.build(Display.getWithNewlines(labels.getLabelLink()), queue.length(),
diagram.getSkinParam().classAttributeIconSize() > 0);
Link link = new Link(diagram.getSkinParam().getCurrentStyleBuilder(), cl1, cl2, linkType,
2022-10-05 20:32:57 +00:00
linkArg.withQuantifier(labels.getFirstLabel(), labels.getSecondLabel())
2022-08-24 16:46:33 +00:00
.withDistanceAngle(diagram.getLabeldistance(), diagram.getLabelangle()));
2020-05-17 21:15:50 +00:00
link.setLinkArrow(labels.getLinkArrow());
2022-04-10 19:24:55 +00:00
if (dir == Direction.LEFT || dir == Direction.UP)
2013-12-10 19:36:50 +00:00
link = link.getInv();
2022-04-10 19:24:55 +00:00
2022-09-18 17:08:06 +00:00
link.setColors(color().getColor(arg, diagram.getSkinParam().getIHtmlColorSet()));
link.applyStyle(arg.getLazzy("ARROW_STYLE", 0));
2015-09-28 20:42:17 +00:00
if (arg.get("STEREOTYPE", 0) != null) {
2021-10-07 16:51:26 +00:00
final Stereotype stereotype = Stereotype.build(arg.get("STEREOTYPE", 0));
2022-04-10 19:24:55 +00:00
link.setStereotype(stereotype);
2015-09-28 20:42:17 +00:00
}
2013-12-10 19:36:50 +00:00
diagram.addLink(link);
return CommandExecutionResult.ok();
}
2020-01-12 22:13:17 +00:00
private IEntity getFoo1(DescriptionDiagram diagram, Code code, Ident ident, Ident pure) {
2022-04-10 19:24:55 +00:00
if (!diagram.V1972() && diagram.isGroup(code))
2019-12-10 21:45:49 +00:00
return diagram.getGroup(code);
2022-04-10 19:24:55 +00:00
if (diagram.V1972() && diagram.isGroupStrict(ident))
2020-01-12 22:13:17 +00:00
return diagram.getGroupStrict(ident);
2022-04-10 19:24:55 +00:00
2019-12-10 21:45:49 +00:00
final String codeString = code.getName();
if (ident.getLast().startsWith("()")) {
ident = ident.removeStartingParenthesis();
2022-02-17 18:27:32 +00:00
return getOrCreateLeaf1972(diagram, ident, ident.toCode(diagram), LeafType.DESCRIPTION, USymbols.INTERFACE,
2020-01-12 22:13:17 +00:00
pure);
2019-12-10 21:45:49 +00:00
}
final char codeChar = codeString.length() > 2 ? codeString.charAt(0) : 0;
2020-01-12 22:13:17 +00:00
final String tmp3 = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(codeString, "\"([:");
final Ident ident3 = diagram.buildFullyQualified(tmp3);
final Code code3 = diagram.V1972() ? ident3 : diagram.buildCode(tmp3);
2013-12-10 19:36:50 +00:00
if (codeChar == '(') {
2022-02-17 18:27:32 +00:00
return getOrCreateLeaf1972(diagram, ident3, code3, LeafType.USECASE, USymbols.USECASE, pure);
2013-12-10 19:36:50 +00:00
} else if (codeChar == ':') {
return getOrCreateLeaf1972(diagram, ident3, code3, LeafType.DESCRIPTION,
2020-06-21 20:31:45 +00:00
diagram.getSkinParam().actorStyle().toUSymbol(), pure);
2013-12-10 19:36:50 +00:00
} else if (codeChar == '[') {
2020-06-21 20:31:45 +00:00
final USymbol sym = diagram.getSkinParam().componentStyle().toUSymbol();
2020-01-12 22:13:17 +00:00
return getOrCreateLeaf1972(diagram, ident3, code3, LeafType.DESCRIPTION, sym, pure);
2013-12-10 19:36:50 +00:00
}
2020-01-12 22:13:17 +00:00
return getOrCreateLeaf1972(diagram, ident, code, null, null, pure);
2013-12-10 19:36:50 +00:00
}
private ILeaf getOrCreateLeaf1972(DescriptionDiagram diagram, Ident ident, Code code, LeafType type, USymbol symbol,
Ident pure) {
2020-01-12 22:13:17 +00:00
if (diagram.V1972()) {
final ILeaf result = pure.size() == 1 ? diagram.getLeafVerySmart(ident) : diagram.getLeafStrict(ident);
// final ILeaf result = diagram.getLeafSmart(ident);
2022-04-10 19:24:55 +00:00
if (result != null)
2020-01-12 22:13:17 +00:00
return result;
2022-04-10 19:24:55 +00:00
2013-12-10 19:36:50 +00:00
}
2020-01-12 22:13:17 +00:00
return diagram.getOrCreateLeaf(ident, code, type, symbol);
2013-12-10 19:36:50 +00:00
}
}