1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-15 14:42:24 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/BodierLikeClassOrObject.java

259 lines
7.5 KiB
Java
Raw Normal View History

2020-04-05 15:13:04 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2020-04-05 15:13:04 +00:00
*
* Project Info: http://plantuml.com
*
* 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
*
* 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.cucadiagram;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2020-04-05 15:13:04 +00:00
import java.util.Set;
import net.sourceforge.plantuml.ISkinParam;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.UrlBuilder;
import net.sourceforge.plantuml.baraye.EntityImp;
2022-05-31 16:31:10 +00:00
import net.sourceforge.plantuml.creole.Parser;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.creole.legacy.CreoleParser;
2021-10-20 16:33:09 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
2020-04-05 15:13:04 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockLineBefore;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.skin.VisibilityModifier;
2022-02-12 17:27:51 +00:00
import net.sourceforge.plantuml.style.PName;
2020-12-01 21:39:27 +00:00
import net.sourceforge.plantuml.style.Style;
2020-04-05 15:13:04 +00:00
2020-12-14 18:31:06 +00:00
public class BodierLikeClassOrObject implements Bodier {
2020-04-05 15:13:04 +00:00
2021-05-14 08:42:57 +00:00
private final List<CharSequence> rawBody = new ArrayList<>();
2020-04-05 15:13:04 +00:00
private final Set<VisibilityModifier> hides;
private LeafType type;
private List<Member> methodsToDisplay;
private List<Member> fieldsToDisplay;
private EntityImp leaf;
2020-04-05 15:13:04 +00:00
2021-10-20 16:33:09 +00:00
@Override
2020-04-05 15:13:04 +00:00
public void muteClassToObject() {
methodsToDisplay = null;
fieldsToDisplay = null;
type = LeafType.OBJECT;
}
2020-12-14 18:31:06 +00:00
BodierLikeClassOrObject(LeafType type, Set<VisibilityModifier> hides) {
2022-05-31 16:31:10 +00:00
if (type == LeafType.MAP)
2020-04-05 15:13:04 +00:00
throw new IllegalArgumentException();
2022-05-31 16:31:10 +00:00
2021-05-14 08:42:57 +00:00
this.type = Objects.requireNonNull(type);
2020-12-14 18:31:06 +00:00
assert type.isLikeClass() || type == LeafType.OBJECT;
2020-04-05 15:13:04 +00:00
this.hides = hides;
}
2021-10-20 16:33:09 +00:00
@Override
public void setLeaf(EntityImp leaf) {
2021-05-14 08:42:57 +00:00
this.leaf = Objects.requireNonNull(leaf);
2020-04-05 15:13:04 +00:00
}
2021-10-20 16:33:09 +00:00
@Override
2022-04-10 19:24:55 +00:00
public boolean addFieldOrMethod(String s) {
2020-04-05 15:13:04 +00:00
// Empty cache
methodsToDisplay = null;
fieldsToDisplay = null;
rawBody.add(s);
2022-04-10 19:24:55 +00:00
return true;
2020-04-05 15:13:04 +00:00
}
private boolean isBodyEnhanced() {
2022-05-31 16:31:10 +00:00
for (CharSequence s : rawBody)
2022-08-24 16:46:33 +00:00
if (BodyEnhanced1.isBlockSeparator(s) || CreoleParser.isTableLine(s.toString())
|| Parser.isTreeStart(s.toString()))
2020-04-05 15:13:04 +00:00
return true;
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
return false;
}
2020-12-14 18:31:06 +00:00
private boolean isMethod(CharSequence s) {
final String purged = s.toString().replaceAll(UrlBuilder.getRegexp(), "");
2022-05-31 16:31:10 +00:00
if (purged.contains("{method}"))
2020-12-14 18:31:06 +00:00
return true;
2022-05-31 16:31:10 +00:00
if (purged.contains("{field}"))
2020-12-14 18:31:06 +00:00
return false;
2022-05-31 16:31:10 +00:00
2020-12-14 18:31:06 +00:00
return purged.contains("(") || purged.contains(")");
2020-04-05 15:13:04 +00:00
}
2021-10-20 16:33:09 +00:00
@Override
2020-12-14 18:31:06 +00:00
public Display getMethodsToDisplay() {
2020-04-05 15:13:04 +00:00
if (methodsToDisplay == null) {
2021-05-14 08:42:57 +00:00
methodsToDisplay = new ArrayList<>();
2020-04-05 15:13:04 +00:00
for (int i = 0; i < rawBody.size(); i++) {
2020-12-14 18:31:06 +00:00
final CharSequence s = rawBody.get(i);
2022-05-31 16:31:10 +00:00
if (isMethod(i, rawBody) == false)
2020-04-05 15:13:04 +00:00
continue;
2022-05-31 16:31:10 +00:00
if (s.length() == 0 && methodsToDisplay.size() == 0)
2020-04-05 15:13:04 +00:00
continue;
2022-05-31 16:31:10 +00:00
2020-12-14 18:31:06 +00:00
final Member m = Member.method(s);
2022-05-31 16:31:10 +00:00
if (hides == null || hides.contains(m.getVisibilityModifier()) == false)
2020-04-05 15:13:04 +00:00
methodsToDisplay.add(m);
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
}
removeFinalEmptyMembers(methodsToDisplay);
}
2020-12-14 18:31:06 +00:00
return Display.create(methodsToDisplay);
2020-04-05 15:13:04 +00:00
}
2020-12-14 18:31:06 +00:00
private boolean isMethod(int i, List<CharSequence> rawBody) {
2020-04-05 15:13:04 +00:00
if (i > 0 && i < rawBody.size() - 1 && rawBody.get(i).length() == 0 && isMethod(rawBody.get(i - 1))
&& isMethod(rawBody.get(i + 1))) {
return true;
}
return isMethod(rawBody.get(i));
}
2021-10-20 16:33:09 +00:00
@Override
2020-12-14 18:31:06 +00:00
public Display getFieldsToDisplay() {
2020-04-05 15:13:04 +00:00
if (fieldsToDisplay == null) {
2021-05-14 08:42:57 +00:00
fieldsToDisplay = new ArrayList<>();
2020-12-14 18:31:06 +00:00
for (CharSequence s : rawBody) {
2022-05-31 16:31:10 +00:00
if (type != LeafType.OBJECT && isMethod(s) == true)
2020-04-05 15:13:04 +00:00
continue;
2022-05-31 16:31:10 +00:00
if (s.length() == 0 && fieldsToDisplay.size() == 0)
2020-04-05 15:13:04 +00:00
continue;
2022-05-31 16:31:10 +00:00
2020-12-14 18:31:06 +00:00
final Member m = Member.field(s);
2022-05-31 16:31:10 +00:00
if (hides == null || hides.contains(m.getVisibilityModifier()) == false)
2020-04-05 15:13:04 +00:00
fieldsToDisplay.add(m);
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
}
removeFinalEmptyMembers(fieldsToDisplay);
}
2020-12-14 18:31:06 +00:00
return Display.create(fieldsToDisplay);
2020-04-05 15:13:04 +00:00
}
private void removeFinalEmptyMembers(List<Member> result) {
2022-05-31 16:31:10 +00:00
while (result.size() > 0 && StringUtils.trin(result.get(result.size() - 1).getDisplay(false)).length() == 0)
2020-04-05 15:13:04 +00:00
result.remove(result.size() - 1);
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
}
2021-10-20 16:33:09 +00:00
@Override
2020-04-05 15:13:04 +00:00
public boolean hasUrl() {
2022-05-31 16:31:10 +00:00
for (CharSequence cs : getFieldsToDisplay())
2021-06-27 16:50:40 +00:00
if (cs instanceof Member) {
final Member m = (Member) cs;
2022-05-31 16:31:10 +00:00
if (m.hasUrl())
2021-06-27 16:50:40 +00:00
return true;
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
}
2022-05-31 16:31:10 +00:00
for (CharSequence cs : getMethodsToDisplay())
2021-06-27 16:50:40 +00:00
if (cs instanceof Member) {
final Member m = (Member) cs;
2022-05-31 16:31:10 +00:00
if (m.hasUrl())
2021-06-27 16:50:40 +00:00
return true;
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
}
return false;
}
2020-12-14 18:31:06 +00:00
private List<CharSequence> rawBodyWithoutHidden() {
2021-05-14 08:42:57 +00:00
final List<CharSequence> result = new ArrayList<>();
2020-12-14 18:31:06 +00:00
for (CharSequence s : rawBody) {
final Member m;
2022-05-31 16:31:10 +00:00
if (isMethod(s))
2020-12-14 18:31:06 +00:00
m = Member.method(s);
2022-05-31 16:31:10 +00:00
else
2020-12-14 18:31:06 +00:00
m = Member.field(s);
2022-05-31 16:31:10 +00:00
if (hides.contains(m.getVisibilityModifier()) == false)
2020-12-14 18:31:06 +00:00
result.add(m);
2020-04-05 15:13:04 +00:00
}
return result;
}
2021-10-20 16:33:09 +00:00
@Override
2022-10-05 20:32:57 +00:00
public TextBlock getBody(ISkinParam skinParam, boolean showMethods, boolean showFields, Stereotype stereotype,
Style style, FontConfiguration fontConfiguration) {
2020-12-14 18:31:06 +00:00
2022-05-31 16:31:10 +00:00
if (BodyFactory.BODY3)
2022-10-05 20:32:57 +00:00
return new Body3(rawBody, skinParam, stereotype, style);
2020-12-14 18:31:06 +00:00
2020-04-05 15:13:04 +00:00
if (type.isLikeClass() && isBodyEnhanced()) {
2022-05-31 16:31:10 +00:00
if (showMethods || showFields)
return BodyFactory.create1(skinParam.getDefaultTextAlignment(HorizontalAlignment.LEFT),
2022-05-21 09:41:00 +00:00
rawBodyWithoutHidden(), skinParam, stereotype, leaf, style);
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
return null;
}
2022-05-31 16:31:10 +00:00
if (leaf == null)
2020-04-05 15:13:04 +00:00
throw new IllegalStateException();
2022-05-31 16:31:10 +00:00
2020-04-05 15:13:04 +00:00
if (type == LeafType.OBJECT) {
2022-05-31 16:31:10 +00:00
if (showFields == false)
2022-02-12 17:27:51 +00:00
return new TextBlockLineBefore(style.value(PName.LineThickness).asDouble(), TextBlockUtils.empty(0, 0));
2022-05-31 16:31:10 +00:00
return BodyFactory.create1(skinParam.getDefaultTextAlignment(HorizontalAlignment.LEFT),
2022-05-21 09:41:00 +00:00
rawBodyWithoutHidden(), skinParam, stereotype, leaf, style);
2020-04-05 15:13:04 +00:00
}
2020-12-14 18:31:06 +00:00
assert type.isLikeClass();
2022-05-31 16:31:10 +00:00
final MethodsOrFieldsArea fields = new MethodsOrFieldsArea(getFieldsToDisplay(), skinParam, leaf, style);
2020-12-14 18:31:06 +00:00
2022-05-31 16:31:10 +00:00
final MethodsOrFieldsArea methods = new MethodsOrFieldsArea(getMethodsToDisplay(), skinParam, leaf, style);
if (showFields && showMethods == false)
2020-04-05 15:13:04 +00:00
return fields.asBlockMemberImpl();
2022-05-31 16:31:10 +00:00
else if (showMethods && showFields == false)
2020-04-05 15:13:04 +00:00
return methods.asBlockMemberImpl();
2022-05-31 16:31:10 +00:00
else if (showFields == false && showMethods == false)
2020-04-05 15:13:04 +00:00
return TextBlockUtils.empty(0, 0);
final TextBlock bb1 = fields.asBlockMemberImpl();
final TextBlock bb2 = methods.asBlockMemberImpl();
return TextBlockUtils.mergeTB(bb1, bb2, HorizontalAlignment.LEFT);
}
2021-10-20 16:33:09 +00:00
@Override
2020-12-14 18:31:06 +00:00
public List<CharSequence> getRawBody() {
2020-04-05 15:13:04 +00:00
return Collections.unmodifiableList(rawBody);
}
}