1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-06 02:10:53 +00:00
plantuml/src/net/sourceforge/plantuml/classdiagram/command/CommandCreateClassMultilines.java

372 lines
14 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
*
*
*/
package net.sourceforge.plantuml.classdiagram.command;
import net.sourceforge.plantuml.FontParam;
2022-12-17 11:10:05 +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;
2022-03-13 12:22:51 +00:00
import net.sourceforge.plantuml.UrlMode;
2022-11-15 23:15:21 +00:00
import net.sourceforge.plantuml.baraye.EntityImp;
import net.sourceforge.plantuml.baraye.Quark;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.classdiagram.ClassDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.CommandMultilines2;
import net.sourceforge.plantuml.command.MultilinesStrategy;
2022-09-23 16:53:33 +00:00
import net.sourceforge.plantuml.command.Trim;
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.RegexOr;
import net.sourceforge.plantuml.command.regex.RegexResult;
import net.sourceforge.plantuml.creole.CreoleMode;
2013-12-10 19:36:50 +00:00
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;
2018-03-09 21:37:34 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotag;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
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;
import net.sourceforge.plantuml.graphic.color.Colors;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.skin.VisibilityModifier;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.BlocLines;
import net.sourceforge.plantuml.utils.StringLocated;
2013-12-10 19:36:50 +00:00
public class CommandCreateClassMultilines extends CommandMultilines2<ClassDiagram> {
2022-01-01 17:05:19 +00:00
private static final String CODE = CommandLinkClass.getSeparator() + "?[%pLN_$]+" + "(?:"
+ CommandLinkClass.getSeparator() + "[%pLN_$]+)*";
2015-04-07 18:18:37 +00:00
public static final String CODES = CODE + "(?:\\s*,\\s*" + CODE + ")*";
2013-12-10 19:36:50 +00:00
enum Mode {
EXTENDS, IMPLEMENTS
};
public CommandCreateClassMultilines() {
2022-09-23 16:53:33 +00:00
super(getRegexConcat(), MultilinesStrategy.REMOVE_STARTING_QUOTE, Trim.BOTH);
2013-12-10 19:36:50 +00:00
}
@Override
public String getPatternEnd() {
2021-05-23 15:35:13 +00:00
return "^[%s]*\\}[%s]*$";
2013-12-10 19:36:50 +00:00
}
2019-06-26 19:24:49 +00:00
private static IRegex getRegexConcat() {
return RegexConcat.build(CommandCreateClassMultilines.class.getName(), RegexLeaf.start(), //
2017-02-01 18:55:51 +00:00
new RegexLeaf("VISIBILITY", "(" + VisibilityModifier.regexForVisibilityCharacterInClassName() + ")?"), //
2022-06-27 16:33:45 +00:00
new RegexLeaf("TYPE",
"(interface|enum|annotation|abstract[%s]+class|static[%s]+class|abstract|class|entity|protocol|struct|exception|metaclass|stereotype)"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceOneOrMore(), //
2013-12-10 19:36:50 +00:00
new RegexOr(//
new RegexConcat(//
2017-07-03 17:59:53 +00:00
new RegexLeaf("DISPLAY1", CommandCreateClass.DISPLAY_WITH_GENERIC), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("as"), //
RegexLeaf.spaceOneOrMore(), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("CODE1", "(" + CommandCreateClass.CODE + ")")), //
new RegexConcat(//
new RegexLeaf("CODE2", "(" + CommandCreateClass.CODE + ")"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("as"), //
RegexLeaf.spaceOneOrMore(), //
2017-07-03 17:59:53 +00:00
new RegexLeaf("DISPLAY2", CommandCreateClass.DISPLAY_WITH_GENERIC)), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("CODE3", "(" + CommandCreateClass.CODE + ")"), //
2015-04-07 18:18:37 +00:00
new RegexLeaf("CODE4", "[%g]([^%g]+)[%g]")), //
2020-04-05 15:13:04 +00:00
new RegexOptional(new RegexConcat(RegexLeaf.spaceZeroOrMore(),
new RegexLeaf("GENERIC", "\\<(" + GenericRegexProducer.PATTERN + ")\\>"))), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2022-10-25 20:43:40 +00:00
new RegexLeaf("TAGS1", Stereotag.pattern() + "?"), //
RegexLeaf.spaceZeroOrMore(), //
2013-12-10 19:36:50 +00:00
new RegexLeaf("STEREO", "(\\<\\<.+\\>\\>)?"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2022-10-25 20:43:40 +00:00
new RegexLeaf("TAGS2", Stereotag.pattern() + "?"), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2022-12-17 11:01:10 +00:00
UrlBuilder.OPTIONAL, //
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(), //
2020-04-05 15:13:04 +00:00
new RegexOptional(new RegexConcat(new RegexLeaf("##"),
new RegexLeaf("LINECOLOR", "(?:\\[(dotted|dashed|bold)\\])?(\\w+)?"))), //
new RegexOptional(new RegexConcat(RegexLeaf.spaceOneOrMore(),
2022-11-04 17:36:03 +00:00
new RegexLeaf("EXTENDS",
"(extends)[%s]+(" + CommandCreateClassMultilines.CODES + "|[%g]([^%g]+)[%g])"))), //
2020-04-05 15:13:04 +00:00
new RegexOptional(new RegexConcat(RegexLeaf.spaceOneOrMore(),
2022-11-04 17:36:03 +00:00
new RegexLeaf("IMPLEMENTS",
"(implements)[%s]+(" + CommandCreateClassMultilines.CODES + "|[%g]([^%g]+)[%g])"))), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf("\\{"), //
RegexLeaf.spaceZeroOrMore(), //
RegexLeaf.end() //
2020-04-05 15:13:04 +00:00
);
2013-12-10 19:36:50 +00:00
}
2019-03-29 22:14:07 +00:00
2018-06-12 20:50:45 +00:00
@Override
public boolean syntaxWithFinalBracket() {
return true;
}
2015-09-28 20:42:17 +00:00
private static ColorParser color() {
return ColorParser.simpleColor(ColorType.BACK);
}
2019-09-22 17:20:16 +00:00
@Override
2021-02-02 10:12:15 +00:00
protected CommandExecutionResult executeNow(ClassDiagram diagram, BlocLines lines) throws NoSuchColorException {
2015-06-20 10:54:49 +00:00
lines = lines.trimSmart(1);
final RegexResult line0 = getStartingPattern().matcher(lines.getFirst().getTrimmed().getString());
final String typeString = StringUtils.goUpperCase(line0.get("TYPE", 0));
final LeafType type = LeafType.getLeafType(typeString);
final String visibilityString = line0.get("VISIBILITY", 0);
VisibilityModifier visibilityModifier = null;
if (visibilityString != null)
visibilityModifier = VisibilityModifier.getVisibilityModifier(visibilityString + "FOO", false);
final String idShort = diagram.cleanIdForQuark(line0.getLazzy("CODE", 0));
final String displayString = line0.getLazzy("DISPLAY", 0);
final String genericOption = line0.getLazzy("DISPLAY", 1);
final String generic = genericOption != null ? genericOption : line0.get("GENERIC", 0);
final String stereotype = line0.get("STEREO", 0);
final Quark quark = diagram.quarkInContext(idShort, true);
Display display = Display.getWithNewlines(displayString);
if (Display.isNull(display))
display = Display.getWithNewlines(quark.getName()).withCreoleMode(CreoleMode.SIMPLE_LINE);
EntityImp entity = (EntityImp) quark.getData();
if (entity == null) {
entity = diagram.reallyCreateLeaf(quark, display, type, null);
} else {
if (entity.muteToType(type, null) == false)
return CommandExecutionResult.error("Cannot create " + idShort + " because it already exists");
entity.setDisplay(display);
}
diagram.setLastEntity(entity);
entity.setVisibilityModifier(visibilityModifier);
if (stereotype != null) {
entity.setStereotype(Stereotype.build(stereotype, diagram.getSkinParam().getCircledCharacterRadius(),
diagram.getSkinParam().getFont(null, false, FontParam.CIRCLED_CHARACTER),
diagram.getSkinParam().getIHtmlColorSet()));
entity.setStereostyle(stereotype);
}
final String urlString = line0.get("URL", 0);
if (urlString != null) {
final UrlBuilder urlBuilder = new UrlBuilder(diagram.getSkinParam().getValue("topurl"), UrlMode.STRICT);
final Url url = urlBuilder.getUrl(urlString);
entity.addUrl(url);
}
Colors colors = color().getColor(line0, diagram.getSkinParam().getIHtmlColorSet());
final String s1 = line0.get("LINECOLOR", 1);
final HColor lineColor = s1 == null ? null : diagram.getSkinParam().getIHtmlColorSet().getColor(s1);
if (lineColor != null)
colors = colors.add(ColorType.LINE, lineColor);
if (line0.get("LINECOLOR", 0) != null)
colors = colors.addLegacyStroke(line0.get("LINECOLOR", 0));
entity.setColors(colors);
if (generic != null)
entity.setGeneric(generic);
if (typeString.contains("STATIC"))
entity.setStatic(true);
2022-06-27 16:33:45 +00:00
2015-08-05 20:17:01 +00:00
if (lines.size() > 1) {
2020-08-25 17:24:17 +00:00
entity.setCodeLine(lines.getAt(0).getLocation());
2015-08-05 20:17:01 +00:00
lines = lines.subExtract(1, 1);
2020-04-05 15:13:04 +00:00
// final Url url = null;
2018-03-09 21:37:34 +00:00
// if (lines.size() > 0) {
2020-04-05 15:13:04 +00:00
// final UrlBuilder urlBuilder = new
// UrlBuilder(diagram.getSkinParam().getValue("topurl"), ModeUrl.STRICT);
2018-03-09 21:37:34 +00:00
// url = urlBuilder.getUrl(lines.getFirst499().toString());
// } else {
// url = null;
// }
// if (url != null) {
// lines = lines.subExtract(1, 0);
// }
2019-03-29 22:14:07 +00:00
for (StringLocated s : lines) {
2022-06-27 16:33:45 +00:00
if (s.getString().length() > 0 && VisibilityModifier.isVisibilityCharacter(s.getString()))
2015-08-05 20:17:01 +00:00
diagram.setVisibilityModifierPresent(true);
2022-06-27 16:33:45 +00:00
2020-04-05 15:13:04 +00:00
entity.getBodier().addFieldOrMethod(s.getString());
2013-12-10 19:36:50 +00:00
}
2020-04-05 15:13:04 +00:00
// if (url != null) {
// entity.addUrl(url);
// }
2013-12-10 19:36:50 +00:00
}
2015-04-07 18:18:37 +00:00
manageExtends("EXTENDS", diagram, line0, entity);
manageExtends("IMPLEMENTS", diagram, line0, entity);
2022-10-25 20:43:40 +00:00
addTags(entity, line0.getLazzy("TAGS", 0));
2013-12-10 19:36:50 +00:00
return CommandExecutionResult.ok();
}
public static void addTags(EntityImp entity, String tags) {
2022-06-27 16:33:45 +00:00
if (tags == null)
2018-03-09 21:37:34 +00:00
return;
2022-06-27 16:33:45 +00:00
2018-03-09 21:37:34 +00:00
for (String tag : tags.split("[ ]+")) {
assert tag.startsWith("$");
tag = tag.substring(1);
entity.addStereotag(new Stereotag(tag));
}
}
public static void manageExtends(String keyword, ClassDiagram diagram, RegexResult arg, final EntityImp entity) {
2019-06-26 19:24:49 +00:00
if (arg.get(keyword, 0) != null) {
final Mode mode = arg.get(keyword, 0).equalsIgnoreCase("extends") ? Mode.EXTENDS : Mode.IMPLEMENTS;
2013-12-10 19:36:50 +00:00
LeafType type2 = LeafType.CLASS;
2022-06-27 16:33:45 +00:00
if (mode == Mode.IMPLEMENTS)
2013-12-10 19:36:50 +00:00
type2 = LeafType.INTERFACE;
2022-06-27 16:33:45 +00:00
if (mode == Mode.EXTENDS && entity.getLeafType() == LeafType.INTERFACE)
2013-12-10 19:36:50 +00:00
type2 = LeafType.INTERFACE;
2022-06-27 16:33:45 +00:00
2022-10-05 20:32:57 +00:00
final String codes = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(arg.get(keyword, 1));
2015-04-07 18:18:37 +00:00
for (String s : codes.split(",")) {
2019-12-10 21:45:49 +00:00
final String idShort = StringUtils.trin(s);
// final Quark ident = diagram
// .buildFromName(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(idShort));
// final Quark other = diagram.buildFromFullPath(idShort);
// final IEntity cl2 = diagram.getOrCreateLeaf(ident, other, type2, null);
final Quark quark = diagram.quarkInContext(diagram.cleanIdForQuark(idShort), true);
EntityImp cl2 = (EntityImp) quark.getData();
if (cl2 == null)
cl2 = diagram.reallyCreateLeaf(quark, Display.getWithNewlines(quark.getName()), type2, null);
2015-04-07 18:18:37 +00:00
LinkType typeLink = new LinkType(LinkDecor.NONE, LinkDecor.EXTENDS);
2022-06-27 16:33:45 +00:00
if (type2 == LeafType.INTERFACE && entity.getLeafType() != LeafType.INTERFACE)
2017-10-07 09:46:53 +00:00
typeLink = typeLink.goDashed();
2022-06-27 16:33:45 +00:00
2022-08-24 16:46:33 +00:00
final LinkArg linkArg = LinkArg.noDisplay(2);
final Link link = new Link(diagram.getEntityFactory(), diagram.getSkinParam().getCurrentStyleBuilder(),
2022-11-04 17:36:03 +00:00
cl2, entity, typeLink, linkArg.withQuantifier(null, null)
.withDistanceAngle(diagram.getLabeldistance(), diagram.getLabelangle()));
2019-07-14 20:09:26 +00:00
diagram.addLink(link);
2013-12-10 19:36:50 +00:00
}
}
}
private EntityImp executeArg0(ClassDiagram diagram, RegexResult line0) throws NoSuchColorException {
2013-12-10 19:36:50 +00:00
final String typeString = StringUtils.goUpperCase(line0.get("TYPE", 0));
final LeafType type = LeafType.getLeafType(typeString);
2022-06-27 16:33:45 +00:00
final String visibilityString = line0.get("VISIBILITY", 0);
2016-07-25 19:25:28 +00:00
VisibilityModifier visibilityModifier = null;
2022-06-27 16:33:45 +00:00
if (visibilityString != null)
2017-02-01 18:55:51 +00:00
visibilityModifier = VisibilityModifier.getVisibilityModifier(visibilityString + "FOO", false);
2013-12-10 19:36:50 +00:00
final String idShort = diagram.cleanIdForQuark(line0.getLazzy("CODE", 0));
final String displayString = line0.getLazzy("DISPLAY", 0);
2022-06-27 16:33:45 +00:00
final String genericOption = line0.getLazzy("DISPLAY", 1);
final String generic = genericOption != null ? genericOption : line0.get("GENERIC", 0);
2013-12-10 19:36:50 +00:00
2022-06-27 16:33:45 +00:00
final String stereotype = line0.get("STEREO", 0);
2013-12-10 19:36:50 +00:00
final Quark quark = diagram.quarkInContext(idShort, true);
Display display = Display.getWithNewlines(displayString);
if (Display.isNull(display))
display = Display.getWithNewlines(quark.getName()).withCreoleMode(CreoleMode.SIMPLE_LINE);
EntityImp entity = (EntityImp) quark.getData();
if (entity == null) {
entity = diagram.reallyCreateLeaf(quark, display, type, null);
2013-12-10 19:36:50 +00:00
} else {
// if (entity.muteToType(type, null) == false)
// return null;
entity.setDisplay(display);
2022-06-27 16:33:45 +00:00
2013-12-10 19:36:50 +00:00
}
2022-11-15 23:15:21 +00:00
diagram.setLastEntity(entity);
entity.setVisibilityModifier(visibilityModifier);
2013-12-10 19:36:50 +00:00
if (stereotype != null) {
entity.setStereotype(Stereotype.build(stereotype, diagram.getSkinParam().getCircledCharacterRadius(),
2020-04-05 15:13:04 +00:00
diagram.getSkinParam().getFont(null, false, FontParam.CIRCLED_CHARACTER),
diagram.getSkinParam().getIHtmlColorSet()));
entity.setStereostyle(stereotype);
2013-12-10 19:36:50 +00:00
}
2022-06-27 16:33:45 +00:00
final String urlString = line0.get("URL", 0);
2013-12-10 19:36:50 +00:00
if (urlString != null) {
2022-03-13 12:22:51 +00:00
final UrlBuilder urlBuilder = new UrlBuilder(diagram.getSkinParam().getValue("topurl"), UrlMode.STRICT);
2013-12-10 19:36:50 +00:00
final Url url = urlBuilder.getUrl(urlString);
entity.addUrl(url);
2013-12-10 19:36:50 +00:00
}
2022-09-18 17:08:06 +00:00
Colors colors = color().getColor(line0, diagram.getSkinParam().getIHtmlColorSet());
2022-06-27 16:33:45 +00:00
final String s = line0.get("LINECOLOR", 1);
2013-12-10 19:36:50 +00:00
2022-10-05 20:32:57 +00:00
final HColor lineColor = s == null ? null : diagram.getSkinParam().getIHtmlColorSet().getColor(s);
2022-06-27 16:33:45 +00:00
if (lineColor != null)
2015-09-28 20:42:17 +00:00
colors = colors.add(ColorType.LINE, lineColor);
2013-12-10 19:36:50 +00:00
2022-06-27 16:33:45 +00:00
if (line0.get("LINECOLOR", 0) != null)
colors = colors.addLegacyStroke(line0.get("LINECOLOR", 0));
entity.setColors(colors);
2013-12-10 19:36:50 +00:00
2022-06-27 16:33:45 +00:00
if (generic != null)
entity.setGeneric(generic);
2022-06-27 16:33:45 +00:00
if (typeString.contains("STATIC"))
entity.setStatic(true);
return entity;
2013-12-10 19:36:50 +00:00
}
}