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

165 lines
4.7 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +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
*
2010-11-15 20:35:36 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* 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.command;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.LineLocation;
import net.sourceforge.plantuml.StringLocated;
2022-03-19 12:48:23 +00:00
import net.sourceforge.plantuml.annotation.HaxeIgnored;
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.RegexResult;
import net.sourceforge.plantuml.core.Diagram;
2019-05-24 19:59:31 +00:00
import net.sourceforge.plantuml.error.PSystemError;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
public abstract class SingleLineCommand2<S extends Diagram> implements Command<S> {
2010-11-15 20:35:36 +00:00
2019-06-26 19:24:49 +00:00
private final IRegex pattern;
2019-03-01 22:16:29 +00:00
private final boolean doTrim;
2021-02-02 10:12:15 +00:00
2022-03-19 12:48:23 +00:00
@HaxeIgnored
2019-06-26 19:24:49 +00:00
public SingleLineCommand2(IRegex pattern) {
2019-03-01 22:16:29 +00:00
this(true, pattern);
}
2019-06-26 19:24:49 +00:00
public SingleLineCommand2(boolean doTrim, IRegex pattern) {
2019-03-01 22:16:29 +00:00
this.doTrim = doTrim;
2021-05-09 21:14:40 +00:00
this.pattern = Objects.requireNonNull(pattern);
2010-11-15 20:35:36 +00:00
}
2018-06-12 20:50:45 +00:00
public boolean syntaxWithFinalBracket() {
return false;
}
2011-03-20 21:40:07 +00:00
public String[] getDescription() {
2019-06-26 19:24:49 +00:00
return new String[] { pattern.getClass().getName() };
2011-03-20 21:40:07 +00:00
}
2010-11-15 20:35:36 +00:00
2019-03-29 22:14:07 +00:00
private String myTrim(StringLocated s) {
2022-05-31 16:31:10 +00:00
if (doTrim)
2019-06-26 19:24:49 +00:00
return s.getTrimmed().getString();
2022-05-31 16:31:10 +00:00
2019-03-29 22:14:07 +00:00
return s.getString();
2019-03-01 22:16:29 +00:00
}
2019-06-26 19:24:49 +00:00
private StringLocated myTrim2(StringLocated s) {
2022-05-31 16:31:10 +00:00
if (doTrim)
2019-06-26 19:24:49 +00:00
return s.getTrimmed();
return s;
2022-05-31 16:31:10 +00:00
2019-06-26 19:24:49 +00:00
}
2015-06-20 10:54:49 +00:00
final public CommandControl isValid(BlocLines lines) {
2022-05-31 16:31:10 +00:00
if (lines.size() == 2 && syntaxWithFinalBracket())
2018-06-12 20:50:45 +00:00
return isValidBracket(lines);
2022-05-31 16:31:10 +00:00
if (lines.size() != 1)
2010-11-15 20:35:36 +00:00
return CommandControl.NOT_OK;
2022-05-31 16:31:10 +00:00
if (isCommandForbidden())
2010-11-15 20:35:36 +00:00
return CommandControl.NOT_OK;
2022-05-31 16:31:10 +00:00
final StringLocated line2 = myTrim2(lines.getFirst());
2019-06-26 19:24:49 +00:00
if (syntaxWithFinalBracket() && line2.getString().endsWith("{") == false) {
final String vline = lines.getAt(0).getString() + " {";
2022-05-31 16:31:10 +00:00
if (isValid(BlocLines.singleString(vline)) == CommandControl.OK)
2018-06-12 20:50:45 +00:00
return CommandControl.OK_PARTIAL;
2022-05-31 16:31:10 +00:00
2018-06-12 20:50:45 +00:00
return CommandControl.NOT_OK;
}
2022-05-31 16:31:10 +00:00
2019-06-26 19:24:49 +00:00
final boolean result = pattern.match(line2);
2022-05-31 16:31:10 +00:00
if (result)
2022-06-06 12:36:58 +00:00
return finalVerification();
2022-05-31 16:31:10 +00:00
2022-06-06 12:36:58 +00:00
return CommandControl.NOT_OK;
2010-11-15 20:35:36 +00:00
}
2018-06-12 20:50:45 +00:00
private CommandControl isValidBracket(BlocLines lines) {
assert lines.size() == 2;
assert syntaxWithFinalBracket();
2022-05-31 16:31:10 +00:00
if (myTrim(lines.getAt(1)).equals("{") == false)
2018-06-12 20:50:45 +00:00
return CommandControl.NOT_OK;
2022-05-31 16:31:10 +00:00
final String vline = lines.getAt(0).getString() + " {";
2019-03-29 22:14:07 +00:00
return isValid(BlocLines.singleString(vline));
2018-06-12 20:50:45 +00:00
}
2010-11-15 20:35:36 +00:00
protected boolean isCommandForbidden() {
return false;
}
2022-06-06 12:36:58 +00:00
protected CommandControl finalVerification() {
return CommandControl.OK;
2010-11-15 20:35:36 +00:00
}
2015-06-20 10:54:49 +00:00
public final CommandExecutionResult execute(S system, BlocLines lines) {
2018-06-12 20:50:45 +00:00
if (syntaxWithFinalBracket() && lines.size() == 2) {
assert myTrim(lines.getAt(1)).equals("{");
lines = BlocLines.singleString(lines.getFirst().getString() + " {");
2018-06-12 20:50:45 +00:00
}
2022-05-31 16:31:10 +00:00
if (lines.size() != 1)
2010-11-15 20:35:36 +00:00
throw new IllegalArgumentException();
2022-05-31 16:31:10 +00:00
final StringLocated first = lines.getFirst();
2019-03-29 22:14:07 +00:00
final String line = myTrim(first);
2022-05-31 16:31:10 +00:00
if (isForbidden(line))
2017-03-15 19:13:31 +00:00
return CommandExecutionResult.error("Syntax error: " + line);
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
final RegexResult arg = pattern.matcher(line);
2022-05-31 16:31:10 +00:00
if (arg == null)
2010-11-15 20:35:36 +00:00
return CommandExecutionResult.error("Cannot parse line " + line);
2022-05-31 16:31:10 +00:00
if (system instanceof PSystemError)
2015-04-07 18:18:37 +00:00
return CommandExecutionResult.error("PSystemError cannot be cast");
2022-05-31 16:31:10 +00:00
2021-02-02 10:12:15 +00:00
try {
return executeArg(system, first.getLocation(), arg);
} catch (NoSuchColorException e) {
return CommandExecutionResult.badColor();
}
2010-11-15 20:35:36 +00:00
}
2015-06-20 10:54:49 +00:00
protected boolean isForbidden(CharSequence line) {
2010-11-15 20:35:36 +00:00
return false;
}
2021-02-02 10:12:15 +00:00
protected abstract CommandExecutionResult executeArg(S system, LineLocation location, RegexResult arg)
throws NoSuchColorException;
2010-11-15 20:35:36 +00:00
}