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

228 lines
8.5 KiB
Java
Raw Normal View History

2015-04-07 18:26:58 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2015-04-07 18:26:58 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2015-04-07 18:26:58 +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:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-15 19:13:31 +00:00
*
2015-04-07 18:26:58 +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
2015-04-07 18:26:58 +00:00
*
*
*/
package net.sourceforge.plantuml.classdiagram.command;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.abel.LeafType;
2015-04-07 18:26:58 +00:00
import net.sourceforge.plantuml.classdiagram.ClassDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.decoration.symbol.USymbol;
import net.sourceforge.plantuml.decoration.symbol.USymbols;
2017-01-21 22:25:28 +00:00
import net.sourceforge.plantuml.descdiagram.command.CommandCreateElementFull;
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-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.Display;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.FontParam;
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.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf;
import net.sourceforge.plantuml.regex.RegexOr;
import net.sourceforge.plantuml.regex.RegexResult;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.stereo.Stereotag;
import net.sourceforge.plantuml.stereo.Stereotype;
2023-12-11 17:34:23 +00:00
import net.sourceforge.plantuml.stereo.StereotypePattern;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.url.Url;
import net.sourceforge.plantuml.url.UrlBuilder;
import net.sourceforge.plantuml.url.UrlMode;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.LineLocation;
2015-04-07 18:26:58 +00:00
public class CommandCreateElementFull2 extends SingleLineCommand2<ClassDiagram> {
private final Mode mode;
public static enum Mode {
NORMAL_KEYWORD, WITH_MIX_PREFIX
}
public CommandCreateElementFull2(Mode mode) {
super(getRegexConcat(mode));
this.mode = mode;
}
private static RegexConcat getRegexConcat(Mode mode) {
2019-07-14 20:09:26 +00:00
String regex = "(state|" + CommandCreateElementFull.ALL_TYPES + ")";
2015-04-07 18:26:58 +00:00
if (mode == Mode.WITH_MIX_PREFIX) {
2019-07-14 20:09:26 +00:00
return RegexConcat.build(CommandCreateElementFull2.class.getName() + mode, //
RegexLeaf.start(), //
new RegexLeaf("mix_"), //
new RegexLeaf("SYMBOL", regex), //
RegexLeaf.spaceOneOrMore(), //
new RegexOr(//
new RegexLeaf("CODE1", CommandCreateElementFull.CODE_WITH_QUOTE), //
new RegexConcat(//
new RegexLeaf("DISPLAY2", CommandCreateElementFull.DISPLAY), //
2023-12-11 17:34:23 +00:00
StereotypePattern.optional("STEREOTYPE2"), //
2019-07-14 20:09:26 +00:00
new RegexLeaf("as"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("CODE2", CommandCreateElementFull.CODE)) //
), //
RegexLeaf.spaceZeroOrMore(), //
2022-10-25 20:43:40 +00:00
new RegexLeaf("TAGS1", Stereotag.pattern() + "?"), //
2023-12-11 17:34:23 +00:00
StereotypePattern.optional("STEREOTYPE"), //
2022-10-25 20:43:40 +00:00
new RegexLeaf("TAGS2", Stereotag.pattern() + "?"), //
2019-07-14 20:09:26 +00:00
RegexLeaf.spaceZeroOrMore(), //
2022-12-17 11:01:10 +00:00
UrlBuilder.OPTIONAL, //
2019-07-14 20:09:26 +00:00
RegexLeaf.spaceZeroOrMore(), //
ColorParser.exp1(), //
RegexLeaf.end());
2015-04-07 18:26:58 +00:00
}
2019-07-14 20:09:26 +00:00
return RegexConcat.build(CommandCreateElementFull2.class.getName() + mode, //
RegexLeaf.start(), //
2015-04-20 19:45:13 +00:00
new RegexLeaf("SYMBOL", regex), //
2019-07-14 20:09:26 +00:00
RegexLeaf.spaceOneOrMore(), //
2015-04-07 18:26:58 +00:00
new RegexOr(//
2017-02-01 18:55:51 +00:00
new RegexLeaf("CODE1", CommandCreateElementFull.CODE_WITH_QUOTE), //
new RegexConcat(//
new RegexLeaf("DISPLAY2", CommandCreateElementFull.DISPLAY), //
2023-12-11 17:34:23 +00:00
StereotypePattern.optional("STEREOTYPE2"), //
2019-06-26 19:24:49 +00:00
new RegexLeaf("as"), //
RegexLeaf.spaceOneOrMore(), //
2017-02-01 18:55:51 +00:00
new RegexLeaf("CODE2", CommandCreateElementFull.CODE)) //
2015-04-07 18:26:58 +00:00
), //
2019-06-26 19:24:49 +00:00
RegexLeaf.spaceZeroOrMore(), //
2022-10-25 20:43:40 +00:00
new RegexLeaf("TAGS1", Stereotag.pattern() + "?"), //
2023-12-11 17:34:23 +00:00
StereotypePattern.optional("STEREOTYPE"), //
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(), //
2019-07-14 20:09:26 +00:00
ColorParser.exp1(), //
RegexLeaf.end());
2015-04-07 18:26:58 +00:00
}
@Override
2015-06-20 10:54:49 +00:00
final protected boolean isForbidden(CharSequence line) {
2022-05-31 16:31:10 +00:00
if (line.toString().matches("^[\\p{L}0-9_.]+$"))
2015-04-07 18:26:58 +00:00
return true;
2022-05-31 16:31:10 +00:00
2015-04-07 18:26:58 +00:00
return false;
}
@Override
2021-05-06 21:23:05 +00:00
protected CommandExecutionResult executeArg(ClassDiagram diagram, LineLocation location, RegexResult arg)
throws NoSuchColorException {
2022-05-31 16:31:10 +00:00
if (mode == Mode.NORMAL_KEYWORD && diagram.isAllowMixing() == false)
2019-06-26 19:24:49 +00:00
return CommandExecutionResult.error("Use 'allowmixing' if you want to mix classes and other UML elements.");
2022-05-31 16:31:10 +00:00
2015-04-07 18:26:58 +00:00
String codeRaw = arg.getLazzy("CODE", 0);
2023-02-08 18:38:46 +00:00
final String displayRaw = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(arg.getLazzy("DISPLAY", 0));
2015-04-07 18:26:58 +00:00
final char codeChar = getCharEncoding(codeRaw);
final char codeDisplay = getCharEncoding(displayRaw);
final String symbol;
if (codeRaw.startsWith("()")) {
symbol = "interface";
2015-05-31 18:56:03 +00:00
codeRaw = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(StringUtils.trin(codeRaw.substring(2)));
2015-04-07 18:26:58 +00:00
} else if (codeChar == '(' || codeDisplay == '(') {
symbol = "usecase";
} else if (codeChar == ':' || codeDisplay == ':') {
symbol = "actor";
} else if (codeChar == '[' || codeDisplay == '[') {
symbol = "component";
} else {
symbol = arg.get("SYMBOL", 0);
}
final LeafType type;
final USymbol usymbol;
if (symbol == null) {
type = LeafType.DESCRIPTION;
2020-06-21 20:31:45 +00:00
usymbol = diagram.getSkinParam().actorStyle().toUSymbol();
} else if (symbol.equalsIgnoreCase("port")) {
2022-09-01 17:40:58 +00:00
type = LeafType.PORTIN;
usymbol = null;
2022-07-27 13:51:02 +00:00
} else if (symbol.equalsIgnoreCase("portin")) {
type = LeafType.PORTIN;
usymbol = null;
} else if (symbol.equalsIgnoreCase("portout")) {
type = LeafType.PORTOUT;
usymbol = null;
2015-04-07 18:26:58 +00:00
} else if (symbol.equalsIgnoreCase("usecase")) {
type = LeafType.USECASE;
usymbol = null;
2022-05-31 16:31:10 +00:00
} else if (symbol.equalsIgnoreCase("usecase/")) {
type = LeafType.USECASE_BUSINESS;
usymbol = null;
2017-01-21 22:25:28 +00:00
} else if (symbol.equalsIgnoreCase("state")) {
type = LeafType.STATE;
usymbol = null;
2015-04-07 18:26:58 +00:00
} else {
2017-01-21 22:25:28 +00:00
type = LeafType.DESCRIPTION;
2022-02-17 18:27:32 +00:00
usymbol = USymbols.fromString(symbol, diagram.getSkinParam());
2022-05-31 16:31:10 +00:00
if (usymbol == null)
2017-01-21 22:25:28 +00:00
throw new IllegalStateException();
2015-04-07 18:26:58 +00:00
}
2019-12-10 21:45:49 +00:00
final String idShort = StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(codeRaw);
final Display display = Display.getWithNewlines(displayRaw == null ? idShort : displayRaw);
final Quark<Entity> quark = diagram.quarkInContext(true, idShort);
2023-02-08 18:38:46 +00:00
Entity entity = quark.getData();
if (entity == null)
entity = diagram.reallyCreateLeaf(quark, display, type, usymbol);
2015-04-07 18:26:58 +00:00
final String stereotype = arg.getLazzy("STEREOTYPE", 0);
2023-02-08 18:38:46 +00:00
entity.setDisplay(display);
2015-04-07 18:26:58 +00:00
entity.setUSymbol(usymbol);
2022-05-31 16:31:10 +00:00
if (stereotype != null)
2021-10-07 16:51:26 +00:00
entity.setStereotype(Stereotype.build(stereotype, diagram.getSkinParam().getCircledCharacterRadius(),
2021-02-02 10:12:15 +00:00
diagram.getSkinParam().getFont(null, false, FontParam.CIRCLED_CHARACTER),
diagram.getSkinParam().getIHtmlColorSet()));
2022-05-31 16:31:10 +00:00
2022-10-25 20:43:40 +00:00
CommandCreateClassMultilines.addTags(entity, arg.getLazzy("TAGS", 0));
2015-04-07 18:26:58 +00:00
final String urlString = arg.get("URL", 0);
if (urlString != null) {
2022-03-13 12:22:51 +00:00
final UrlBuilder urlBuilder = new UrlBuilder(diagram.getSkinParam().getValue("topurl"), UrlMode.STRICT);
2015-04-07 18:26:58 +00:00
final Url url = urlBuilder.getUrl(urlString);
entity.addUrl(url);
}
2021-02-02 10:12:15 +00:00
final String s = arg.get("COLOR", 0);
2015-04-07 18:26:58 +00:00
2022-10-25 20:43:40 +00:00
entity.setSpecificColorTOBEREMOVED(ColorType.BACK,
s == null ? null : diagram.getSkinParam().getIHtmlColorSet().getColor(s));
2015-04-07 18:26:58 +00:00
return CommandExecutionResult.ok();
}
private char getCharEncoding(final String codeRaw) {
return codeRaw != null && codeRaw.length() > 2 ? codeRaw.charAt(0) : 0;
}
}