1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 16:10:48 +00:00
plantuml/src/net/sourceforge/plantuml/descdiagram/command/CommandLinkElement.java

349 lines
12 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
*/
package net.sourceforge.plantuml.descdiagram.command;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.ColorParam;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Direction;
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.classdiagram.command.CommandLinkClass;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.command.regex.MyPattern;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexConcat;
import net.sourceforge.plantuml.command.regex.RegexLeaf;
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;
import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link;
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;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorParser;
import net.sourceforge.plantuml.graphic.color.ColorType;
import net.sourceforge.plantuml.graphic.color.Colors;
2013-12-10 19:36:50 +00:00
public class CommandLinkElement extends SingleLineCommand2<DescriptionDiagram> {
public CommandLinkElement() {
super(getRegexConcat());
}
static RegexConcat getRegexConcat() {
return new RegexConcat(
new RegexLeaf("^"), //
getGroup("ENT1"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("[%s]*"), //
new RegexLeaf("LABEL1", "(?:[%g]([^%g]+)[%g])?"), //
new RegexLeaf("[%s]*"), //
new RegexLeaf("HEAD2", "(0\\)|<<|[<^*+#0)]|<\\||[%s]+o)?"), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("BODY1", "([-=.~]+)"), //
new RegexLeaf("ARROW_STYLE1",
2015-07-11 09:32:49 +00:00
"(?:\\[((?:#\\w+|dotted|dashed|bold|hidden|norank)(?:,#\\w+|,dotted|,dashed|,bold|,hidden|,norank)*)\\])?"),
2013-12-10 19:36:50 +00:00
new RegexLeaf("DIRECTION", "(?:(left|right|up|down|le?|ri?|up?|do?)(?=[-=.~0()]))?"), //
new RegexLeaf("INSIDE", "(?:(0|\\(0\\)|\\(0|0\\))(?=[-=.~]))?"), //
new RegexLeaf("ARROW_STYLE2",
2015-07-11 09:32:49 +00:00
"(?:\\[((?:#\\w+|dotted|dashed|bold|hidden|norank)(?:,#\\w+|,dotted|,dashed|,bold|,hidden|,norank)*)\\])?"),
2013-12-10 19:36:50 +00:00
new RegexLeaf("BODY2", "([-=.~]*)"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("HEAD1", "(\\(0|>>|[>^*+#0(]|\\|>|o[%s]+)?"), //
new RegexLeaf("[%s]*"), //
new RegexLeaf("LABEL2", "(?:[%g]([^%g]+)[%g])?"), //
new RegexLeaf("[%s]*"), //
2013-12-10 19:36:50 +00:00
getGroup("ENT2"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("[%s]*"), //
2015-09-28 20:42:17 +00:00
color().getRegex(), //
new RegexLeaf("[%s]*"), //
new RegexLeaf("STEREOTYPE", "(\\<\\<.*\\>\\>)?"), //
new RegexLeaf("[%s]*"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("LABEL_LINK", "(?::[%s]*(.+))?$"));
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);
}
2013-12-10 19:36:50 +00:00
private LinkType getLinkType(RegexResult arg) {
final String head1 = trimAndLowerCase(arg.get("HEAD1", 0));
final String head2 = trimAndLowerCase(arg.get("HEAD2", 0));
LinkDecor d1 = LinkDecor.NONE;
LinkDecor d2 = LinkDecor.NONE;
if (head1.equals("(0")) {
d1 = LinkDecor.CIRCLE_CONNECT;
} else if (head1.equals("#")) {
d1 = LinkDecor.SQUARRE;
} else if (head1.equals("0")) {
d1 = LinkDecor.CIRCLE;
} else if (head1.equals("(")) {
d1 = LinkDecor.PARENTHESIS;
} else if (head1.equals(">")) {
d1 = LinkDecor.ARROW;
} else if (head1.equals("*")) {
d1 = LinkDecor.COMPOSITION;
} else if (head1.equals("o")) {
d1 = LinkDecor.AGREGATION;
} else if (head1.equals("+")) {
d1 = LinkDecor.PLUS;
} else if (head1.equals(">>")) {
d1 = LinkDecor.ARROW_TRIANGLE;
} else if (head1.equals("^")) {
d1 = LinkDecor.EXTENDS;
} else if (head1.equals("|>")) {
d1 = LinkDecor.EXTENDS;
}
if (head2.equals("0)")) {
d2 = LinkDecor.CIRCLE_CONNECT;
} else if (head2.equals("#")) {
d2 = LinkDecor.SQUARRE;
} else if (head2.equals("0")) {
d2 = LinkDecor.CIRCLE;
} else if (head2.equals(")")) {
d2 = LinkDecor.PARENTHESIS;
} else if (head2.equals("<")) {
d2 = LinkDecor.ARROW;
} else if (head2.equals("*")) {
d2 = LinkDecor.COMPOSITION;
} else if (head2.equals("o")) {
d2 = LinkDecor.AGREGATION;
} else if (head2.equals("+")) {
d2 = LinkDecor.PLUS;
} else if (head2.equals("<<")) {
d2 = LinkDecor.ARROW_TRIANGLE;
} else if (head2.equals("^")) {
d2 = LinkDecor.EXTENDS;
} else if (head2.equals("<|")) {
d2 = LinkDecor.EXTENDS;
}
LinkType result = new LinkType(d1, d2);
final String queue = getQueue(arg);
if (queue.contains(".")) {
result = result.getDashed();
} else if (queue.contains("~")) {
result = result.getDotted();
} else if (queue.contains("=")) {
result = result.getBold();
}
final String middle = arg.get("INSIDE", 0);
if ("0".equals(middle)) {
result = result.withMiddleCircle();
} else if ("0)".equals(middle)) {
result = result.withMiddleCircleCircled1();
} else if ("(0".equals(middle)) {
result = result.withMiddleCircleCircled2();
} else if ("(0)".equals(middle)) {
result = result.withMiddleCircleCircled();
}
return result;
}
private static String trimAndLowerCase(String s) {
if (s == null) {
return "";
}
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,
2015-09-28 20:42:17 +00:00
"([\\p{L}0-9_.]+|\\(\\)[%s]*[\\p{L}0-9_.]+|\\(\\)[%s]*[%g][^%g]+[%g]|:[^:]+:|(?!\\[\\*\\])\\[[^\\[\\]]+\\]|\\((?!\\*\\))[^)]+\\))");
2013-12-10 19:36:50 +00:00
}
static class Labels {
private String firstLabel;
private String secondLabel;
private String labelLink;
Labels(RegexResult arg) {
firstLabel = arg.get("LABEL1", 0);
secondLabel = arg.get("LABEL2", 0);
labelLink = arg.get("LABEL_LINK", 0);
if (labelLink != null) {
if (firstLabel == null && secondLabel == null) {
init();
}
labelLink = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(labelLink);
}
}
private void init() {
2015-04-07 18:18:37 +00:00
final Pattern p1 = MyPattern.cmpile("^[%g]([^%g]+)[%g]([^%g]+)[%g]([^%g]+)[%g]$");
2013-12-10 19:36:50 +00:00
final Matcher m1 = p1.matcher(labelLink);
if (m1.matches()) {
firstLabel = m1.group(1);
2015-09-28 20:42:17 +00:00
labelLink = StringUtils.trin(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(StringUtils
.trin(m1.group(2))));
2013-12-10 19:36:50 +00:00
secondLabel = m1.group(3);
return;
}
2015-04-07 18:18:37 +00:00
final Pattern p2 = MyPattern.cmpile("^[%g]([^%g]+)[%g]([^%g]+)$");
2013-12-10 19:36:50 +00:00
final Matcher m2 = p2.matcher(labelLink);
if (m2.matches()) {
firstLabel = m2.group(1);
2015-09-28 20:42:17 +00:00
labelLink = StringUtils.trin(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(StringUtils
.trin(m2.group(2))));
2013-12-10 19:36:50 +00:00
secondLabel = null;
return;
}
2015-04-07 18:18:37 +00:00
final Pattern p3 = MyPattern.cmpile("^([^%g]+)[%g]([^%g]+)[%g]$");
2013-12-10 19:36:50 +00:00
final Matcher m3 = p3.matcher(labelLink);
if (m3.matches()) {
firstLabel = null;
2015-09-28 20:42:17 +00:00
labelLink = StringUtils.trin(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(StringUtils
.trin(m3.group(1))));
2013-12-10 19:36:50 +00:00
secondLabel = m3.group(2);
}
}
}
@Override
protected CommandExecutionResult executeArg(DescriptionDiagram diagram, RegexResult arg) {
final Code ent1 = Code.of(arg.get("ENT1", 0));
final Code ent2 = Code.of(arg.get("ENT2", 0));
if (diagram.isGroup(ent1) && diagram.isGroup(ent2)) {
return executePackageLink(diagram, arg);
}
final IEntity cl1 = diagram.isGroup(ent1) ? diagram.getGroup(Code.of(arg.get("ENT1", 0))) : getOrCreateLeaf(
diagram, ent1);
final IEntity cl2 = diagram.isGroup(ent2) ? diagram.getGroup(Code.of(arg.get("ENT2", 0))) : getOrCreateLeaf(
diagram, ent2);
2015-09-28 20:42:17 +00:00
// if (arg.get("ENT1", 1) != null) {
// cl1.setStereotype(new Stereotype(arg.get("ENT1", 1)));
// }
// if (arg.get("ENT2", 1) != null) {
// cl2.setStereotype(new Stereotype(arg.get("ENT2", 1)));
// }
2013-12-10 19:36:50 +00:00
final LinkType linkType = getLinkType(arg);
final Direction dir = getDirection(arg);
final String queue;
if (dir == Direction.LEFT || dir == Direction.RIGHT) {
queue = "-";
} else {
queue = getQueue(arg);
}
final Labels labels = new Labels(arg);
Link link = new Link(cl1, cl2, linkType, Display.getWithNewlines(labels.labelLink), queue.length(),
labels.firstLabel, labels.secondLabel, diagram.getLabeldistance(), diagram.getLabelangle());
if (dir == Direction.LEFT || dir == Direction.UP) {
link = link.getInv();
}
2015-09-28 20:42:17 +00:00
Colors colors = color().getColor(arg, diagram.getSkinParam().getIHtmlColorSet());
colors = CommandLinkClass.applyStyle(arg.getLazzy("ARROW_STYLE", 0), link, colors);
if (arg.get("STEREOTYPE", 0) != null) {
final Stereotype stereotype = new Stereotype(arg.get("STEREOTYPE", 0));
colors = colors.applyStereotype(stereotype, diagram.getSkinParam(), ColorParam.componentArrow);
}
link.setColors(colors);
2013-12-10 19:36:50 +00:00
diagram.addLink(link);
return CommandExecutionResult.ok();
}
private ILeaf getOrCreateLeaf(DescriptionDiagram diagram, final Code code2) {
2015-04-07 18:18:37 +00:00
final String code = code2.getFullName();
2013-12-10 19:36:50 +00:00
if (code.startsWith("()")) {
2015-09-28 20:42:17 +00:00
return diagram.getOrCreateLeaf(Code.of(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(StringUtils
.trin(code.substring(2)))), LeafType.DESCRIPTION, USymbol.INTERFACE);
2013-12-10 19:36:50 +00:00
}
final char codeChar = code.length() > 2 ? code.charAt(0) : 0;
if (codeChar == '(') {
2015-04-07 18:18:37 +00:00
return diagram.getOrCreateLeaf(code2.eventuallyRemoveStartingAndEndingDoubleQuote("\"([:"),
LeafType.USECASE, USymbol.USECASE);
2013-12-10 19:36:50 +00:00
} else if (codeChar == ':') {
2015-04-07 18:18:37 +00:00
return diagram.getOrCreateLeaf(code2.eventuallyRemoveStartingAndEndingDoubleQuote("\"([:"),
LeafType.DESCRIPTION, USymbol.ACTOR);
2013-12-10 19:36:50 +00:00
} else if (codeChar == '[') {
2015-04-07 18:18:37 +00:00
final USymbol sym = diagram.getSkinParam().useUml2ForComponent() ? USymbol.COMPONENT2 : USymbol.COMPONENT1;
return diagram.getOrCreateLeaf(code2.eventuallyRemoveStartingAndEndingDoubleQuote("\"([:"),
LeafType.DESCRIPTION, sym);
2013-12-10 19:36:50 +00:00
}
2015-04-07 18:18:37 +00:00
return diagram.getOrCreateLeaf(code2, null, null);
2013-12-10 19:36:50 +00:00
}
private CommandExecutionResult executePackageLink(DescriptionDiagram diagram, RegexResult arg) {
final IEntity cl1 = diagram.getGroup(Code.of(arg.get("ENT1", 0)));
final IEntity cl2 = diagram.getGroup(Code.of(arg.get("ENT2", 0)));
final LinkType linkType = getLinkType(arg);
final Direction dir = getDirection(arg);
final String queue;
if (dir == Direction.LEFT || dir == Direction.RIGHT) {
queue = "-";
} else {
queue = getQueue(arg);
}
Link link = new Link(cl1, cl2, linkType, Display.getWithNewlines(arg.get("LABEL_LINK", 0)), queue.length());
if (dir == Direction.LEFT || dir == Direction.UP) {
link = link.getInv();
}
2015-04-07 18:18:37 +00:00
CommandLinkClass.applyStyle(arg.getLazzy("ARROW_STYLE", 0), link);
2013-12-10 19:36:50 +00:00
diagram.addLink(link);
return CommandExecutionResult.ok();
}
}