2020-04-05 15:13:04 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import net.sourceforge.plantuml.FontParam;
|
|
|
|
import net.sourceforge.plantuml.ISkinParam;
|
|
|
|
import net.sourceforge.plantuml.StringUtils;
|
2020-12-14 18:31:06 +00:00
|
|
|
import net.sourceforge.plantuml.UrlBuilder;
|
|
|
|
import net.sourceforge.plantuml.creole.legacy.CreoleParser;
|
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;
|
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
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
private final List<CharSequence> rawBody = new ArrayList<CharSequence>();
|
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 ILeaf leaf;
|
|
|
|
|
|
|
|
public void muteClassToObject() {
|
|
|
|
methodsToDisplay = null;
|
|
|
|
fieldsToDisplay = null;
|
|
|
|
type = LeafType.OBJECT;
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
BodierLikeClassOrObject(LeafType type, Set<VisibilityModifier> hides) {
|
2020-04-05 15:13:04 +00:00
|
|
|
if (type == LeafType.MAP) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
if (type == null) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
assert type.isLikeClass() || type == LeafType.OBJECT;
|
2020-04-05 15:13:04 +00:00
|
|
|
this.hides = hides;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLeaf(ILeaf leaf) {
|
|
|
|
if (leaf == null) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
this.leaf = leaf;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addFieldOrMethod(String s) {
|
|
|
|
// Empty cache
|
|
|
|
methodsToDisplay = null;
|
|
|
|
fieldsToDisplay = null;
|
|
|
|
rawBody.add(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBodyEnhanced() {
|
2020-12-14 18:31:06 +00:00
|
|
|
for (CharSequence s : rawBody) {
|
|
|
|
if (BodyEnhanced1.isBlockSeparator(s) || CreoleParser.isTableLine(s.toString())) {
|
2020-04-05 15:13:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
private boolean isMethod(CharSequence s) {
|
|
|
|
final String purged = s.toString().replaceAll(UrlBuilder.getRegexp(), "");
|
|
|
|
if (purged.contains("{method}")) {
|
|
|
|
return true;
|
2020-04-05 15:13:04 +00:00
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
if (purged.contains("{field}")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return purged.contains("(") || purged.contains(")");
|
2020-04-05 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
public Display getMethodsToDisplay() {
|
2020-04-05 15:13:04 +00:00
|
|
|
if (methodsToDisplay == null) {
|
|
|
|
methodsToDisplay = new ArrayList<Member>();
|
|
|
|
for (int i = 0; i < rawBody.size(); i++) {
|
2020-12-14 18:31:06 +00:00
|
|
|
final CharSequence s = rawBody.get(i);
|
2020-04-05 15:13:04 +00:00
|
|
|
if (isMethod(i, rawBody) == false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (s.length() == 0 && methodsToDisplay.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
final Member m = Member.method(s);
|
2020-04-05 15:13:04 +00:00
|
|
|
if (hides == null || hides.contains(m.getVisibilityModifier()) == false) {
|
|
|
|
methodsToDisplay.add(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
public Display getFieldsToDisplay() {
|
2020-04-05 15:13:04 +00:00
|
|
|
if (fieldsToDisplay == null) {
|
|
|
|
fieldsToDisplay = new ArrayList<Member>();
|
2020-12-14 18:31:06 +00:00
|
|
|
for (CharSequence s : rawBody) {
|
2020-04-05 15:13:04 +00:00
|
|
|
if (isMethod(s) == true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (s.length() == 0 && fieldsToDisplay.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
final Member m = Member.field(s);
|
2020-04-05 15:13:04 +00:00
|
|
|
if (hides == null || hides.contains(m.getVisibilityModifier()) == false) {
|
|
|
|
fieldsToDisplay.add(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
while (result.size() > 0 && StringUtils.trin(result.get(result.size() - 1).getDisplay(false)).length() == 0) {
|
|
|
|
result.remove(result.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasUrl() {
|
2020-12-14 18:31:06 +00:00
|
|
|
for (CharSequence cs : getFieldsToDisplay()) {
|
|
|
|
final Member m = (Member) cs;
|
2020-04-05 15:13:04 +00:00
|
|
|
if (m.hasUrl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
for (CharSequence cs : getMethodsToDisplay()) {
|
|
|
|
final Member m = (Member) cs;
|
2020-04-05 15:13:04 +00:00
|
|
|
if (m.hasUrl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
private List<CharSequence> rawBodyWithoutHidden() {
|
|
|
|
final List<CharSequence> result = new ArrayList<CharSequence>();
|
|
|
|
for (CharSequence s : rawBody) {
|
|
|
|
final Member m;
|
|
|
|
if (isMethod(s)) {
|
|
|
|
m = Member.method(s);
|
|
|
|
} else {
|
|
|
|
m = Member.field(s);
|
|
|
|
}
|
2020-04-05 15:13:04 +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;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:39:27 +00:00
|
|
|
public TextBlock getBody(FontParam fontParam, ISkinParam skinParam, boolean showMethods, boolean showFields,
|
|
|
|
Stereotype stereotype, Style style) {
|
2020-12-14 18:31:06 +00:00
|
|
|
|
|
|
|
if (BodyFactory.BODY3) {
|
|
|
|
return new Body3(rawBody, fontParam, skinParam, stereotype, style);
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:13:04 +00:00
|
|
|
if (type.isLikeClass() && isBodyEnhanced()) {
|
|
|
|
if (showMethods || showFields) {
|
2021-03-04 18:11:16 +00:00
|
|
|
return BodyFactory.create1(skinParam.getDefaultTextAlignment(HorizontalAlignment.LEFT),
|
|
|
|
rawBodyWithoutHidden(), fontParam, skinParam, stereotype, leaf, style);
|
2020-04-05 15:13:04 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (leaf == null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
if (type == LeafType.OBJECT) {
|
|
|
|
if (showFields == false) {
|
|
|
|
return new TextBlockLineBefore(TextBlockUtils.empty(0, 0));
|
|
|
|
}
|
2021-03-04 18:11:16 +00:00
|
|
|
return BodyFactory.create1(skinParam.getDefaultTextAlignment(HorizontalAlignment.LEFT),
|
|
|
|
rawBodyWithoutHidden(), fontParam, skinParam, stereotype, leaf, style);
|
2020-04-05 15:13:04 +00:00
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
assert type.isLikeClass();
|
|
|
|
|
|
|
|
final MethodsOrFieldsArea fields = new MethodsOrFieldsArea(getFieldsToDisplay(), fontParam, skinParam,
|
|
|
|
stereotype, leaf, style);
|
|
|
|
|
2020-04-05 15:13:04 +00:00
|
|
|
final MethodsOrFieldsArea methods = new MethodsOrFieldsArea(getMethodsToDisplay(), fontParam, skinParam,
|
2020-12-01 21:39:27 +00:00
|
|
|
stereotype, leaf, style);
|
2020-04-05 15:13:04 +00:00
|
|
|
if (showFields && showMethods == false) {
|
|
|
|
return fields.asBlockMemberImpl();
|
|
|
|
} else if (showMethods && showFields == false) {
|
|
|
|
return methods.asBlockMemberImpl();
|
|
|
|
} else if (showFields == false && showMethods == false) {
|
|
|
|
return TextBlockUtils.empty(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
final TextBlock bb1 = fields.asBlockMemberImpl();
|
|
|
|
final TextBlock bb2 = methods.asBlockMemberImpl();
|
|
|
|
return TextBlockUtils.mergeTB(bb1, bb2, HorizontalAlignment.LEFT);
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:31:06 +00:00
|
|
|
public List<CharSequence> getRawBody() {
|
2020-04-05 15:13:04 +00:00
|
|
|
return Collections.unmodifiableList(rawBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|