1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-31 23:50:49 +00:00
plantuml/src/net/sourceforge/plantuml/creole/legacy/CreoleParser.java

188 lines
6.5 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
*
*
*/
2020-05-17 21:15:50 +00:00
package net.sourceforge.plantuml.creole.legacy;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
import java.util.Arrays;
2022-11-04 17:36:03 +00:00
import java.util.HashMap;
import java.util.Iterator;
2015-04-07 18:18:37 +00:00
import java.util.List;
2022-11-04 17:36:03 +00:00
import java.util.Map;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2015-04-07 18:18:37 +00:00
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.EmbeddedDiagram;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ISkinSimple;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.SpriteContainerEmpty;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2020-05-17 21:15:50 +00:00
import net.sourceforge.plantuml.creole.CreoleContext;
import net.sourceforge.plantuml.creole.CreoleMode;
import net.sourceforge.plantuml.creole.Parser;
import net.sourceforge.plantuml.creole.Sheet;
import net.sourceforge.plantuml.creole.SheetBuilder;
import net.sourceforge.plantuml.creole.Stripe;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.creole.atom.Atom;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2020-01-12 22:13:17 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorRuntimeException;
2013-12-10 19:36:50 +00:00
2020-05-17 21:15:50 +00:00
public class CreoleParser implements SheetBuilder {
2013-12-10 19:36:50 +00:00
private final FontConfiguration fontConfiguration;
2015-04-07 18:18:37 +00:00
private final ISkinSimple skinParam;
private final HorizontalAlignment horizontalAlignment;
2020-01-12 22:13:17 +00:00
private final CreoleMode creoleMode;
2020-05-17 21:15:50 +00:00
private final FontConfiguration stereotype;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
public CreoleParser(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
2020-05-17 21:15:50 +00:00
ISkinSimple skinParam, CreoleMode creoleMode, FontConfiguration stereotype) {
this.stereotype = stereotype;
2020-01-12 22:13:17 +00:00
this.creoleMode = creoleMode;
2013-12-10 19:36:50 +00:00
this.fontConfiguration = fontConfiguration;
2021-05-09 21:14:40 +00:00
this.skinParam = Objects.requireNonNull(skinParam);
2015-04-07 18:18:37 +00:00
this.horizontalAlignment = horizontalAlignment;
2013-12-10 19:36:50 +00:00
}
2020-01-12 22:13:17 +00:00
private Stripe createStripe(String line, CreoleContext context, Stripe lastStripe,
FontConfiguration fontConfiguration) {
2022-09-23 16:53:33 +00:00
if (lastStripe instanceof StripeRaw) {
final StripeRaw code = (StripeRaw) lastStripe;
2020-09-30 20:57:58 +00:00
if (code.isTerminated()) {
lastStripe = null;
} else {
final boolean terminated = code.addAndCheckTermination(line);
return null;
}
}
2016-01-30 12:20:07 +00:00
if (lastStripe instanceof StripeTable && isTableLine(line)) {
2013-12-10 19:36:50 +00:00
final StripeTable table = (StripeTable) lastStripe;
2020-03-18 10:50:02 +00:00
table.analyzeAndAddLine(line);
2013-12-10 19:36:50 +00:00
return null;
2020-05-17 21:15:50 +00:00
} else if (lastStripe instanceof StripeTree && Parser.isTreeStart(StringUtils.trinNoTrace(line))) {
2015-05-31 18:56:03 +00:00
final StripeTree tree = (StripeTree) lastStripe;
tree.analyzeAndAdd(line);
return null;
2016-01-30 12:20:07 +00:00
} else if (isTableLine(line)) {
2013-12-10 19:36:50 +00:00
return new StripeTable(fontConfiguration, skinParam, line);
2020-05-17 21:15:50 +00:00
} else if (Parser.isTreeStart(line)) {
2015-05-31 18:56:03 +00:00
return new StripeTree(fontConfiguration, skinParam, line);
2020-09-30 20:57:58 +00:00
} else if (Parser.isCodeStart(line)) {
2022-09-23 16:53:33 +00:00
return new StripeCode(fontConfiguration.changeFamily(Parser.MONOSPACED));
// ::comment when WASM
2022-09-23 16:53:33 +00:00
} else if (Parser.isLatexStart(line)) {
return new StripeLatex(fontConfiguration);
// ::done
2013-12-10 19:36:50 +00:00
}
2020-01-12 22:13:17 +00:00
return new CreoleStripeSimpleParser(line, context, fontConfiguration, skinParam, creoleMode)
2015-05-31 18:56:03 +00:00
.createStripe(context);
}
2020-12-14 18:31:06 +00:00
public static boolean isTableLine(String line) {
2018-03-09 21:37:34 +00:00
return line.matches("^(\\<#\\w+(,#?\\w+)?\\>)?\\|(\\=)?.*\\|$");
2016-01-30 12:20:07 +00:00
}
public static boolean doesStartByColor(String line) {
2020-03-18 10:50:02 +00:00
return line.matches("^\\=?\\s*(\\<#\\w+(,#?\\w+)?\\>).*");
2016-01-30 12:20:07 +00:00
}
2022-11-04 17:36:03 +00:00
private final Map<Display, Sheet> cache = new HashMap<>();
2013-12-10 19:36:50 +00:00
public Sheet createSheet(Display display) {
2022-11-04 17:36:03 +00:00
Sheet result = cache.get(display);
if (result == null) {
result = createSheetSlow(display, false);
cache.put(display, result);
}
return result;
}
private Sheet createSheetSlow(Display display, boolean checkColor) {
2015-04-07 18:18:37 +00:00
final Sheet sheet = new Sheet(horizontalAlignment);
2015-07-11 09:32:49 +00:00
if (Display.isNull(display) == false) {
2015-04-07 18:18:37 +00:00
final CreoleContext context = new CreoleContext();
2022-11-04 17:36:03 +00:00
final Iterator<CharSequence> it = display.iterator();
while (it.hasNext()) {
final CharSequence cs = it.next();
2015-04-07 18:18:37 +00:00
final Stripe stripe;
2022-11-04 17:36:03 +00:00
final String type = EmbeddedDiagram.getEmbeddedType(StringUtils.trinNoTrace(cs));
if (type != null) {
final Atom embeddedDiagram = EmbeddedDiagram.createAndSkip(type, it, skinParam);
if (checkColor)
stripe = null;
else {
stripe = new Stripe() {
public Atom getLHeader() {
return null;
}
public List<Atom> getAtoms() {
return Arrays.asList(embeddedDiagram);
}
};
}
2020-01-12 22:13:17 +00:00
} else if (cs instanceof Stereotype) {
2022-06-06 12:36:58 +00:00
if (display.showStereotype())
for (String st : ((Stereotype) cs).getLabels(skinParam.guillemet()))
sheet.add(createStripe(st, context, sheet.getLastStripe(), stereotype));
2022-05-31 16:31:10 +00:00
2020-01-12 22:13:17 +00:00
continue;
2015-04-07 18:18:37 +00:00
} else {
2022-11-04 17:36:03 +00:00
stripe = createStripe(skinParam.guillemet().manageGuillemet(cs.toString()), context,
sheet.getLastStripe(), fontConfiguration);
2015-04-07 18:18:37 +00:00
}
2022-05-31 16:31:10 +00:00
if (stripe != null)
2015-04-07 18:18:37 +00:00
sheet.add(stripe);
2022-05-31 16:31:10 +00:00
2013-12-10 19:36:50 +00:00
}
}
return sheet;
}
2021-02-02 10:12:15 +00:00
public static void checkColor(Display result) throws NoSuchColorException {
FontConfiguration fc = FontConfiguration.blackBlueTrue(UFont.byDefault(10));
try {
new CreoleParser(fc, HorizontalAlignment.LEFT, new SpriteContainerEmpty(), CreoleMode.FULL, fc)
2022-11-04 17:36:03 +00:00
.createSheetSlow(result, true);
2021-02-02 10:12:15 +00:00
} catch (NoSuchColorRuntimeException e) {
throw new NoSuchColorException();
}
}
2013-12-10 19:36:50 +00:00
}