This commit is contained in:
Arnaud Roques 2023-02-06 22:11:35 +01:00
parent 678949b44c
commit 62d819dae4
5 changed files with 1229 additions and 0 deletions

View File

@ -0,0 +1,107 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, 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 com.plantuml.wasm;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collections;
import java.util.List;
import net.sourceforge.plantuml.BlockUml;
import net.sourceforge.plantuml.BlockUmlBuilder;
import net.sourceforge.plantuml.ErrorUml;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.preproc.Defines;
public class Png {
public static int convert(String pathOut, String text) {
WasmLog.start = System.currentTimeMillis();
WasmLog.log("Starting processing");
try {
final FileFormatOption format = new FileFormatOption(FileFormat.PNG);
text = cleanText(text);
final BlockUmlBuilder builder = new BlockUmlBuilder(Collections.<String>emptyList(), UTF_8,
Defines.createEmpty(), new StringReader(text), null, "string");
List<BlockUml> blocks = builder.getBlockUmls();
if (blocks.size() == 0)
return 43;
final Diagram system = blocks.get(0).getDiagram();
if (system instanceof PSystemError) {
final ErrorUml error = ((PSystemError) system).getFirstError();
WasmLog.log("[" + error.getPosition() + "] " + error.getError());
return 44;
}
WasmLog.log("...processing...");
final FileOutputStream fos = new FileOutputStream(new File(pathOut));
WasmLog.log("...loading data...");
final ImageData imageData = system.exportDiagram(fos, 0, format);
WasmLog.log("Done!");
fos.close();
return 0;
} catch (Throwable t) {
WasmLog.log("Fatal error " + t);
}
return 47;
}
private static String cleanText(String text) {
if (text.endsWith("\n") == false)
text = text + "\n";
if (text.endsWith("@start") == false)
text = "@startuml\n" + text + "@enduml\n";
return text;
}
}

View File

@ -0,0 +1,107 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, 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 com.plantuml.wasm;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collections;
import java.util.List;
import net.sourceforge.plantuml.BlockUml;
import net.sourceforge.plantuml.BlockUmlBuilder;
import net.sourceforge.plantuml.ErrorUml;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.preproc.Defines;
public class Svg {
public static int convert(String pathOut, String text) {
WasmLog.start = System.currentTimeMillis();
WasmLog.log("Starting processing");
try {
final FileFormatOption format = new FileFormatOption(FileFormat.SVG);
text = cleanText(text);
final BlockUmlBuilder builder = new BlockUmlBuilder(Collections.<String>emptyList(), UTF_8,
Defines.createEmpty(), new StringReader(text), null, "string");
List<BlockUml> blocks = builder.getBlockUmls();
if (blocks.size() == 0)
return 43;
final Diagram system = blocks.get(0).getDiagram();
if (system instanceof PSystemError) {
final ErrorUml error = ((PSystemError) system).getFirstError();
WasmLog.log("[" + error.getPosition() + "] " + error.getError());
return 44;
}
WasmLog.log("...processing...");
final FileOutputStream fos = new FileOutputStream(new File(pathOut));
WasmLog.log("...loading data...");
final ImageData imageData = system.exportDiagram(fos, 0, format);
WasmLog.log("Done!");
fos.close();
return 0;
} catch (Throwable t) {
WasmLog.log("Fatal error " + t);
}
return 47;
}
private static String cleanText(String text) {
if (text.endsWith("\n") == false)
text = text + "\n";
if (text.endsWith("@start") == false)
text = "@startuml\n" + text + "@enduml\n";
return text;
}
}

View File

@ -0,0 +1,705 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, 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
* Contribution: Hisashi Miyashita
* Contribution: Miguel Esteves
*
*/
package net.sourceforge.plantuml.baraye;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import net.sourceforge.plantuml.Hideable;
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.LineConfigurable;
import net.sourceforge.plantuml.Removeable;
import net.sourceforge.plantuml.SpecificBackcolorable;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.command.Position;
import net.sourceforge.plantuml.cucadiagram.Bodier;
import net.sourceforge.plantuml.cucadiagram.CucaNote;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.cucadiagram.DisplayPositioned;
import net.sourceforge.plantuml.cucadiagram.EntityPosition;
import net.sourceforge.plantuml.cucadiagram.GroupType;
import net.sourceforge.plantuml.cucadiagram.ICucaDiagram;
import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link;
import net.sourceforge.plantuml.cucadiagram.Stereostyles;
import net.sourceforge.plantuml.cucadiagram.Stereotag;
import net.sourceforge.plantuml.cucadiagram.Stereotype;
import net.sourceforge.plantuml.cucadiagram.Together;
import net.sourceforge.plantuml.cucadiagram.dot.Neighborhood;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockEmpty;
import net.sourceforge.plantuml.graphic.USymbol;
import net.sourceforge.plantuml.graphic.USymbols;
import net.sourceforge.plantuml.graphic.color.Colors;
import net.sourceforge.plantuml.klimt.color.ColorType;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.FontParam;
import net.sourceforge.plantuml.klimt.font.UFont;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.plasma.Quark;
import net.sourceforge.plantuml.skin.VisibilityModifier;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.svek.IEntityImage;
import net.sourceforge.plantuml.svek.Kal;
import net.sourceforge.plantuml.svek.Margins;
import net.sourceforge.plantuml.svek.PackageStyle;
import net.sourceforge.plantuml.svek.SingleStrategy;
import net.sourceforge.plantuml.svek.image.EntityImageStateCommon;
import net.sourceforge.plantuml.text.Guillemet;
import net.sourceforge.plantuml.url.Url;
import net.sourceforge.plantuml.utils.Direction;
import net.sourceforge.plantuml.utils.LineLocation;
final public class Entity implements SpecificBackcolorable, Hideable, Removeable, LineConfigurable {
private final EntityFactory entityFactory;
private Quark quark;
private Url url;
private final Bodier bodier;
private final String uid;
private Display display = Display.empty();
private DisplayPositioned legend = null;
private LeafType leafType;
private Stereotype stereotype;
private Stereostyles stereostyles = Stereostyles.NONE;
private String generic;
private GroupType groupType;
// Other
private Margins margins = Margins.NONE;
private final Collection<String> portShortNames = new HashSet<>();
private int xposition;
private IEntityImage svekImage;
private USymbol symbol;
private final int rawLayout;
private char concurrentSeparator;
private LineLocation codeLine;
private Set<Stereotag> tags = new LinkedHashSet<>();
private final List<CucaNote> notesTop = new ArrayList<>();
private final List<CucaNote> notesBottom = new ArrayList<>();
private Together together;
//
public void addNote(Display note, Position position, Colors colors) {
if (position == Position.TOP)
notesTop.add(CucaNote.build(note, position, colors));
else if (position == Position.BOTTOM)
notesBottom.add(CucaNote.build(note, position, colors));
}
//
public List<CucaNote> getNotes(Position position) {
if (position == Position.TOP)
return Collections.unmodifiableList(notesTop);
if (position == Position.BOTTOM)
return Collections.unmodifiableList(notesBottom);
throw new IllegalArgumentException();
}
public void addStereotag(Stereotag tag) {
this.tags.add(tag);
}
public Set<Stereotag> stereotags() {
return Collections.unmodifiableSet(tags);
}
// Back to Entity
private Entity(Quark quark, EntityFactory entityFactory, Bodier bodier, int rawLayout) {
this.quark = Objects.requireNonNull(quark);
if (quark.isRoot())
this.uid = "clroot";
else
this.uid = StringUtils.getUid("cl", entityFactory.getDiagram().getUniqueSequence());
this.entityFactory = entityFactory;
this.bodier = bodier;
this.rawLayout = rawLayout;
}
Entity(Quark quark, EntityFactory entityFactory, Bodier bodier, LeafType leafType, int rawLayout) {
this(Objects.requireNonNull(quark), entityFactory, bodier, rawLayout);
this.leafType = leafType;
}
Entity(Quark quark, EntityFactory entityFactory, Bodier bodier, GroupType groupType, int rawLayout) {
this(Objects.requireNonNull(quark), entityFactory, bodier, rawLayout);
this.groupType = groupType;
}
public LeafType getLeafType() {
return leafType;
}
public void muteToType(LeafType newType) {
if (leafType == LeafType.CLASS && newType == LeafType.OBJECT)
bodier.muteClassToObject();
this.groupType = null;
this.leafType = newType;
}
public void muteToGroupType(GroupType newType) {
this.groupType = newType;
this.leafType = null;
}
public boolean muteToType(LeafType newType, USymbol newSymbol) {
// checkNotGroup();
Objects.requireNonNull(newType);
if (leafType != LeafType.STILL_UNKNOWN) {
if (newType == this.leafType)
return true;
if (leafType != LeafType.ANNOTATION && leafType != LeafType.ABSTRACT_CLASS && leafType != LeafType.CLASS
&& leafType != LeafType.ENUM && leafType != LeafType.INTERFACE) {
return false;
// throw new IllegalArgumentException("type=" + leafType);
}
if (newType != LeafType.ANNOTATION && newType != LeafType.ABSTRACT_CLASS && newType != LeafType.CLASS
&& newType != LeafType.ENUM && newType != LeafType.INTERFACE && newType != LeafType.OBJECT) {
return false;
// throw new IllegalArgumentException("newtype=" + newType);
}
}
if (leafType == LeafType.CLASS && newType == LeafType.OBJECT)
bodier.muteClassToObject();
this.leafType = newType;
this.symbol = newSymbol;
return true;
}
public Quark getQuark() {
return quark;
}
public Display getDisplay() {
return display;
}
public void setDisplay(Display display) {
this.display = display;
}
public String getUid() {
return uid;
}
public Stereotype getStereotype() {
return stereotype;
}
public final void setStereotype(Stereotype stereotype) {
this.stereotype = stereotype;
}
public String toString() {
return quark.toString() + " " + display + "(" + leafType + ")[" + groupType + "] " + getUid();
}
public final Url getUrl99() {
return url;
}
public boolean hasUrl() {
if (Display.isNull(display) == false && display.hasUrl())
return true;
if (bodier == null)
return false;
if (bodier.hasUrl())
return true;
return url != null;
}
public final void addUrl(Url url) {
this.url = url;
}
public final Margins getMargins() {
checkNotGroup();
return margins;
}
public final void ensureMargins(Margins newMargins) {
// checkNotGroup();
this.margins = this.margins.merge(newMargins);
}
public int getXposition() {
checkNotGroup();
return xposition;
}
public void setXposition(int pos) {
checkNotGroup();
xposition = pos;
}
public final IEntityImage getSvekImage() {
checkNotGroup();
return svekImage;
}
public final void setSvekImage(IEntityImage svekImage) {
checkNotGroup();
this.svekImage = svekImage;
}
public final void setGeneric(String generic) {
checkNotGroup();
this.generic = generic;
}
public final String getGeneric() {
checkNotGroup();
return generic;
}
public Bodier getBodier() {
return bodier;
}
public EntityPosition getEntityPosition() {
// if (leafType == LeafType.PORT)
// return EntityPosition.PORT;
if (leafType == LeafType.PORTIN)
return EntityPosition.PORTIN;
if (leafType == LeafType.PORTOUT)
return EntityPosition.PORTOUT;
if (leafType != LeafType.STATE)
return EntityPosition.NORMAL;
if (quark.isRoot())
return EntityPosition.NORMAL;
final Stereotype stereotype = getStereotype();
if (stereotype == null)
return EntityPosition.NORMAL;
return EntityPosition.fromStereotype(stereotype.getLabel(Guillemet.DOUBLE_COMPARATOR));
}
// ----------
private void checkGroup() {
if (isGroup() == false)
throw new UnsupportedOperationException();
}
private void checkNotGroup() {
if (isGroup())
throw new UnsupportedOperationException();
}
public GroupType getGroupType() {
checkGroup();
return groupType;
}
public PackageStyle getPackageStyle() {
checkGroup();
if (stereotype == null)
return null;
return stereotype.getPackageStyle();
}
public boolean isGroup() {
if (groupType != null && leafType != null)
throw new IllegalStateException();
assert groupType == null || leafType == null;
if (groupType != null)
return true;
if (leafType != null)
return false;
throw new IllegalStateException();
}
// ---- other
public void overrideImage(IEntityImage img, LeafType leafType) {
checkGroup();
this.svekImage = img;
this.url = null;
for (final Link link : new ArrayList<>(entityFactory.getLinks()))
if (EntityUtils.isPureInnerLink12(this, link))
entityFactory.removeLink(link);
this.groupType = null;
this.leafType = leafType;
}
public USymbol getUSymbol() {
if (getLeafType() == LeafType.CIRCLE)
return USymbols.INTERFACE;
return symbol;
}
public void setUSymbol(USymbol symbol) {
this.symbol = symbol;
}
public SingleStrategy getSingleStrategy() {
return SingleStrategy.SQUARE;
}
public boolean isHidden() {
if (getParentContainer() != null && getParentContainer().isHidden())
return true;
return isHiddenInternal();
}
private boolean isHiddenInternal() {
if (quark.isRoot())
return false;
if (isGroup()) {
if (entityFactory.isHidden(this))
return true;
if (leafs().size() == 0)
return false;
for (Entity leaf : leafs())
if (leaf.isHiddenInternal() == false)
return false;
for (Entity g : groups())
if (g.isHiddenInternal() == false)
return false;
return true;
}
return entityFactory.isHidden(this);
}
public boolean isRemoved() {
if (getParentContainer() != null && getParentContainer().isRemoved())
return true;
return isRemovedInternal();
}
private boolean isRemovedInternal() {
if (isGroup()) {
if (entityFactory.isRemoved(this))
return true;
if (leafs().size() == 0 && groups().size() == 0)
return false;
for (Entity leaf : leafs())
if (((Entity) leaf).isRemovedInternal() == false)
return false;
for (Entity g : groups())
if (((Entity) g).isRemovedInternal() == false)
return false;
return true;
}
return entityFactory.isRemoved(this);
}
public boolean isAloneAndUnlinked() {
if (isGroup())
return false;
for (Link link : entityFactory.getLinks())
if (link.contains(this)) {
final Entity other = (Entity) link.getOther(this);
final boolean removed = entityFactory.isRemovedIgnoreUnlinked(other);
if (removed == false && link.getType().isInvisible() == false)
return false;
}
return true;
}
private FontParam getTitleFontParam() {
return getGroupType() == GroupType.STATE ? FontParam.STATE : FontParam.PACKAGE;
}
public FontConfiguration getFontConfigurationForTitle(final ISkinParam skinParam) {
final FontParam fontParam = getTitleFontParam();
final HColor fontHtmlColor = skinParam.getFontHtmlColor(getStereotype(), fontParam, FontParam.PACKAGE);
final UFont font = skinParam.getFont(getStereotype(), true, fontParam, FontParam.PACKAGE);
final FontConfiguration fontConfiguration = FontConfiguration.create(font, fontHtmlColor,
skinParam.getHyperlinkColor(), skinParam.useUnderlineForHyperlink(), skinParam.getTabSize());
return fontConfiguration;
}
public final int getRawLayout() {
return rawLayout;
}
public char getConcurrentSeparator() {
return concurrentSeparator;
}
public void setConcurrentSeparator(char separator) {
this.concurrentSeparator = separator;
}
private Neighborhood neighborhood;
public void setNeighborhood(Neighborhood neighborhood) {
this.neighborhood = neighborhood;
}
public Neighborhood getNeighborhood() {
return neighborhood;
}
private final Map<String, Display> tips = new LinkedHashMap<String, Display>();
public void putTip(String member, Display display) {
tips.put(member, display);
}
public Map<String, Display> getTips() {
return Collections.unmodifiableMap(tips);
}
private Colors colors = Colors.empty();
public Colors getColors() {
return colors;
}
public void setColors(Colors colors) {
this.colors = colors;
}
public void setSpecificColorTOBEREMOVED(ColorType type, HColor color) {
if (color != null)
this.colors = colors.add(type, color);
}
public Collection<String> getPortShortNames() {
checkNotGroup();
return Collections.unmodifiableCollection(portShortNames);
}
public void addPortShortName(String portShortName) {
portShortNames.add(portShortName);
}
private VisibilityModifier visibility;
public void setVisibilityModifier(VisibilityModifier visibility) {
this.visibility = visibility;
}
public VisibilityModifier getVisibilityModifier() {
return visibility;
}
public void setLegend(DisplayPositioned legend) {
checkGroup();
this.legend = legend;
}
public DisplayPositioned getLegend() {
return legend;
}
public String getCodeLine() {
if (this.codeLine == null)
return null;
return "" + this.codeLine.getPosition();
}
public void setCodeLine(LineLocation codeLine) {
this.codeLine = codeLine;
}
//
public void setStereostyle(String stereo) {
this.stereostyles = Stereostyles.build(stereo);
}
//
public Stereostyles getStereostyles() {
return stereostyles;
}
private final Map<Direction, List<Kal>> kals = new EnumMap<>(Direction.class);
public void addKal(Kal kal) {
final Direction position = kal.getPosition();
List<Kal> list = kals.get(position);
if (list == null) {
list = new ArrayList<>();
kals.put(position, list);
}
list.add(kal);
}
public List<Kal> getKals(Direction position) {
final List<Kal> result = kals.get(position);
if (result == null)
return Collections.emptyList();
return Collections.unmodifiableList(result);
}
public ICucaDiagram getDiagram() {
return entityFactory.getDiagram();
}
private boolean isStatic;
//
public void setStatic(boolean isStatic) {
this.isStatic = isStatic;
}
//
public boolean isStatic() {
return isStatic;
}
// For group
public TextBlock getStateHeader(ISkinParam skinParam) {
checkGroup();
final Style style = EntityImageStateCommon.getStyleStateHeader(this, skinParam);
final List<CharSequence> details = getBodier().getRawBody();
if (details.size() == 0)
return new TextBlockEmpty();
if (style == null)
throw new IllegalArgumentException();
final FontConfiguration fontConfiguration = FontConfiguration.create(skinParam, style);
Display display = null;
for (CharSequence s : details)
if (display == null)
display = Display.getWithNewlines(s.toString());
else
display = display.addAll(Display.getWithNewlines(s.toString()));
return display.create(fontConfiguration, HorizontalAlignment.LEFT, skinParam);
}
public Together getTogether() {
return together;
}
public void setTogether(Together together) {
this.together = together;
}
public Entity getParentContainer() {
if (quark.isRoot())
return null;
return (Entity) quark.getParent().getData();
}
public Collection<Entity> leafs() {
final List<Entity> result = new ArrayList<>();
for (Quark quark : quark.getChildren()) {
final Entity data = (Entity) quark.getData();
if (data != null && data.isGroup() == false)
result.add(data);
}
return Collections.unmodifiableCollection(result);
}
public Collection<Entity> groups() {
final List<Entity> result = new ArrayList<>();
for (Quark quark : quark.getChildren()) {
final Entity data = (Entity) quark.getData();
if (data != null && data.isGroup())
result.add(data);
}
return Collections.unmodifiableCollection(result);
}
public int countChildren() {
return getQuark().countChildren();
}
public boolean isRoot() {
return getQuark().isRoot();
}
final public boolean isEmpty() {
return countChildren() == 0;
}
public String getName() {
return getQuark().getName();
}
}

View File

@ -0,0 +1,169 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, 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.plasma;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Plasma {
private String separator;
private final Quark root;
private final List<Quark> quarks = new ArrayList<>();
public Plasma(String separator) {
this.root = new Quark(this, null, "");
this.separator = separator;
this.quarks.add(root);
}
public Quark root() {
return root;
}
public final String getSeparator() {
return separator;
}
public final void setSeparator(String separator) {
if (separator == null)
separator = "\u0000";
this.separator = separator;
}
public final boolean hasSeparator() {
return this.separator != null && this.separator != "\u0000";
}
public Quark parse(Quark root, String full) {
final List<String> result = root.getSignature();
while (true) {
int idx = full.indexOf(separator);
if (idx == -1) {
result.add(full);
return ensurePresent(result);
}
if (idx > 0) {
result.add(full.substring(0, idx));
ensurePresent(new ArrayList<>(result));
}
full = full.substring(idx + separator.length());
}
}
Quark ensurePresent(List<String> result) {
Quark quark = getIfExists(result);
if (quark == null) {
if (result.size() == 0) {
// quark = new Quark(this, null, result);
throw new UnsupportedOperationException();
} else {
final Quark parent = ensurePresent(result.subList(0, result.size() - 1));
quark = new Quark(this, parent, result.get(result.size() - 1));
}
// System.err.println("PUTTING " + quark);
quarks.add(quark);
}
return quark;
}
public Collection<Quark> quarks() {
return Collections.unmodifiableCollection(quarks);
}
public Quark firstWithName(String name) {
for (Quark quark : quarks)
if (quark.getName().equals(name))
return quark;
return null;
}
public int countByName(String name) {
int count = 0;
for (Quark quark : quarks)
if (quark.getName().equals(name))
count++;
return count;
}
public Quark getIfExistsFromFullPath(String full) {
for (Quark quark : quarks)
if (quark.toString(separator).equals(full))
return quark;
return null;
}
public Quark getIfExists(List<String> signature) {
for (Quark quark : quarks)
if (quark.getSignature().equals(signature))
return quark;
return null;
}
public int countChildren(Quark parent) {
int count = 0;
for (Quark quark : quarks)
if (quark.getParent() == parent)
count++;
return count;
}
public List<Quark> getChildren(Quark parent) {
final List<Quark> result = new ArrayList<>();
for (Quark quark : quarks)
if (quark.getParent() == parent)
result.add(quark);
return Collections.unmodifiableList(result);
}
public void moveAllChildOfToAnewFather(Quark oldFather, Quark newFather) {
for (Quark quark : quarks) {
if (quark == newFather)
continue;
if (quark.getParent() == oldFather)
quark.setParent(newFather);
}
}
}

View File

@ -0,0 +1,141 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, 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.plasma;
import java.util.ArrayList;
import java.util.List;
public class Quark {
private final Plasma plasma;
private /* final */ Quark parent;
private final String name;
private Object data;
Quark(Plasma plasma, Quark parent, String name) {
this.name = name;
this.plasma = plasma;
this.parent = parent;
}
public Quark getParent() {
return parent;
}
@Override
public String toString() {
// return parts.toString() + "(parent=" + parent + ")";
return getSignature().toString();
}
List<String> getSignature() {
final List<String> result = new ArrayList<>();
if (parent != null)
result.addAll(parent.getSignature());
result.add(name);
return result;
}
public boolean containsLarge(Quark other) {
final List<String> signature = this.getSignature();
final List<String> otherSignature = other.getSignature();
return otherSignature.size() > signature.size()
&& otherSignature.subList(0, signature.size()).equals(signature);
}
public String toString(String sep) {
if (sep == null)
sep = ".";
final StringBuilder sb = new StringBuilder();
for (String s : getSignature()) {
if (sb.length() > 0)
sb.append(sep);
sb.append(s);
}
return sb.toString();
}
public String getName() {
return name;
}
public String getQualifiedName() {
if (plasma.hasSeparator())
return toString(plasma.getSeparator());
return name;
}
public boolean isRoot() {
return parent == null;
}
public final Plasma getPlasma() {
return plasma;
}
public final Object getData() {
return data;
}
public final void setData(Object data) {
this.data = data;
}
public Quark childIfExists(String name) {
final List<String> sig = new ArrayList<>(getSignature());
sig.add(name);
return plasma.getIfExists(sig);
}
public Quark child(String full) {
return plasma.parse(this, full);
}
public int countChildren() {
return plasma.countChildren(this);
}
public List<Quark> getChildren() {
return plasma.getChildren(this);
}
void setParent(Quark newFather) {
this.parent = newFather;
}
}