1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 04:32:26 +00:00
plantuml/src/net/sourceforge/plantuml/descdiagram/command/CommandLinkElement.java

334 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;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2023-02-06 21:04:53 +00:00
import net.sourceforge.plantuml.baraye.Entity;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2;
import net.sourceforge.plantuml.cucadiagram.Display;
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;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.color.ColorParser;
import net.sourceforge.plantuml.klimt.color.ColorType;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
2023-02-06 21:04:53 +00:00
import net.sourceforge.plantuml.plasma.Quark;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.regex.IRegex;
import net.sourceforge.plantuml.regex.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf;
import net.sourceforge.plantuml.regex.RegexOptional;
import net.sourceforge.plantuml.regex.RegexResult;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Direction;
import net.sourceforge.plantuml.utils.LineLocation;
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 + ")\\])?"), //
2022-11-04 17:36:03 +00:00
new RegexOptional(new RegexLeaf("DIRECTION", "(left|right|up|down|le?|ri?|up?|do?)(?=[-=.~0()\\[])")), //
2019-06-26 19:24:49 +00:00
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) {
2022-10-18 20:57:44 +00:00
return new RegexLeaf(name, "(" + //
"[%pLN_.]+" + //
"|" + //
"[%g][^%g]+[%g]" + //
"|" + //
"\\(\\)[%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 {
final String ent1 = arg.get("ENT1", 0);
final String ent2 = arg.get("ENT2", 0);
2023-02-08 18:38:46 +00:00
final String ent1clean = diagram.cleanId(ent1);
final String ent2clean = diagram.cleanId(ent2);
// final String ent1 = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(ent1String);
// final String ent2 = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(ent2String);
// final Quark ident1 = diagram.buildFullyQualified(ent1);
// final Quark ident2 = diagram.buildFullyQualified(ent2);
// Quark ident1pure = diagram.getPlasma().root().child(ent1);
// Quark ident2pure = diagram.getPlasma().root().child(ent2);
// final Quark code1 = diagram.buildFromFullPath(ent1String);
// final Quark code2 = diagram.buildFromFullPath(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);
2023-02-06 21:04:53 +00:00
final Entity cl1;
final Entity cl2;
if (diagram.isGroup(ent1clean) && diagram.isGroup(ent2clean)) {
cl1 = diagram.getGroup(ent1clean);
cl2 = diagram.getGroup(ent2clean);
2020-01-12 22:13:17 +00:00
} else {
cl1 = getDummy(diagram, ent1);
cl2 = getDummy(diagram, ent2);
2020-01-12 22:13:17 +00:00
}
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.getEntityFactory(), diagram.getSkinParam().getCurrentStyleBuilder(), cl1, cl2,
2022-11-04 17:36:03 +00:00
linkType, 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();
}
// private String removeStartingParenthesis(String s) {
// if (s.startsWith("()"))
// return s.substring(2);
// return s;
// }
2022-04-10 19:24:55 +00:00
2023-02-06 21:04:53 +00:00
private Entity getDummy(DescriptionDiagram diagram, String ident) {
if (ident.startsWith("()")) {
2023-02-08 18:38:46 +00:00
ident = diagram.cleanId(ident);
final Quark<Entity> quark = diagram.quarkInContext(ident, false);
if (quark.getData() != null)
2023-02-08 18:38:46 +00:00
return quark.getData();
return diagram.reallyCreateLeaf(quark, Display.getWithNewlines(quark.getName()), LeafType.DESCRIPTION,
USymbols.INTERFACE);
2019-12-10 21:45:49 +00:00
}
final char codeChar = ident.length() > 2 ? ident.charAt(0) : 0;
2023-02-08 18:38:46 +00:00
ident = diagram.cleanId(ident);
final Quark<Entity> quark = diagram.quarkInContext(ident, false);
if (diagram.isGroup(quark))
2023-02-08 18:38:46 +00:00
return quark.getData();
if (quark.getData() != null)
2023-02-08 18:38:46 +00:00
return quark.getData();
final Display display = Display.getWithNewlines(quark.getName());
2013-12-10 19:36:50 +00:00
if (codeChar == '(') {
2023-02-08 18:38:46 +00:00
return diagram.reallyCreateLeaf(quark, display, LeafType.USECASE, USymbols.USECASE);
2013-12-10 19:36:50 +00:00
} else if (codeChar == ':') {
2023-02-08 18:38:46 +00:00
return diagram.reallyCreateLeaf(quark, display, LeafType.DESCRIPTION,
diagram.getSkinParam().actorStyle().toUSymbol());
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();
2023-02-08 18:38:46 +00:00
return diagram.reallyCreateLeaf(quark, display, LeafType.DESCRIPTION, sym);
2013-12-10 19:36:50 +00:00
}
2023-02-08 18:38:46 +00:00
return diagram.reallyCreateLeaf(quark, display, LeafType.STILL_UNKNOWN, null);
2013-12-10 19:36:50 +00:00
}
}