Skip to content

Commit

Permalink
fix: improve filelist support and nwdiag
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Jun 12, 2023
1 parent 793b78a commit b32500b
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 100 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
@@ -1,4 +1,4 @@
# Warning, "version" should be the same in gradle.properties and Version.java
# Any idea anyone how to magically synchronize those :-) ?
version = 1.2023.9beta3
version = 1.2023.9beta4
org.gradle.workers.max = 3
10 changes: 5 additions & 5 deletions src/net/sourceforge/plantuml/file/AParentFolderRegular.java
Expand Up @@ -57,15 +57,15 @@ public AFile getAFile(String nameOrPath) throws IOException {
final SFile filecurrent;
// Log.info("AParentFolderRegular::looking for " + nameOrPath);
// Log.info("AParentFolderRegular::dir = " + dir);
if (dir == null) {
if (dir == null)
filecurrent = new SFile(nameOrPath);
} else {
else
filecurrent = dir.getAbsoluteFile().file(nameOrPath);
}

// Log.info("AParentFolderRegular::Filecurrent " + filecurrent);
if (filecurrent.exists()) {
if (filecurrent.exists())
return new AFileRegular(filecurrent.getCanonicalFile());
}

return null;
}

Expand Down
111 changes: 111 additions & 0 deletions src/net/sourceforge/plantuml/filesdiagram/FilesEntry.java
@@ -0,0 +1,111 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://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.filesdiagram;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.style.ISkinParam;

public class FilesEntry implements Iterable<FilesEntry> {

private final String name;
private FilesType type;
private List<FilesEntry> children = new ArrayList<>();

public FilesEntry(String name, FilesType type) {
this.name = name;
this.type = type;
}

public FilesEntry addRawEntry(String raw) {
final int x = raw.indexOf('/');
if (x == -1) {
final FilesEntry result = new FilesEntry(raw, FilesType.DATA);
children.add(result);
return result;
}
final FilesEntry folder = getOrCreateFolder(raw.substring(0, x));
final String remain = raw.substring(x + 1);
if (remain.length() == 0)
return folder;
return folder.addRawEntry(remain);
}

private FilesEntry getOrCreateFolder(String folderName) {
for (FilesEntry child : children)
if (child.type == FilesType.FOLDER && child.getName().equals(folderName))
return child;

final FilesEntry result = new FilesEntry(folderName, FilesType.FOLDER);
children.add(result);
return result;
}

@Override
public Iterator<FilesEntry> iterator() {
return Collections.unmodifiableCollection(children).iterator();
}

public String getName() {
return name;
}

public String getEmoticon() {
if (type == FilesType.FOLDER)
return "<:1f4c2:>";
// return "<:1f4c1:>";
return "<:1f4c4:>";
}

public UGraphic drawAndMove(UGraphic ug, FontConfiguration fontConfiguration, ISkinParam skinParam, double deltax) {
final Display display = Display.getWithNewlines(getEmoticon() + getName());
TextBlock result = display.create(fontConfiguration, HorizontalAlignment.LEFT, skinParam);
result.drawU(ug.apply(UTranslate.dx(deltax)));
ug = ug.apply(UTranslate.dy(result.calculateDimension(ug.getStringBounder()).getHeight() + 2));
for (FilesEntry child : children)
ug = child.drawAndMove(ug, fontConfiguration, skinParam, deltax + 21);
return ug;
}

}
20 changes: 4 additions & 16 deletions src/net/sourceforge/plantuml/filesdiagram/FilesListing.java
Expand Up @@ -34,26 +34,19 @@
*/
package net.sourceforge.plantuml.filesdiagram;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.font.UFont;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.shape.AbstractTextBlock;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.style.ISkinParam;

public class FilesListing extends AbstractTextBlock {

private final ISkinParam skinParam;
private final FontConfiguration fontConfiguration = FontConfiguration.blackBlueTrue(UFont.courier(14));
private final List<String> tmp = new ArrayList<>();
private final FilesEntry root = new FilesEntry("", FilesType.FOLDER);

public FilesListing(ISkinParam skinParam) {
this.skinParam = skinParam;
Expand All @@ -66,18 +59,13 @@ public XDimension2D calculateDimension(StringBounder stringBounder) {

@Override
public void drawU(UGraphic ug) {
for (String s : tmp) {
final Display display = Display.getWithNewlines("<:1f4c4:>" + s);
TextBlock result = display.create(fontConfiguration, HorizontalAlignment.LEFT, skinParam);
result.drawU(ug);
ug = ug.apply(UTranslate.dy(result.calculateDimension(ug.getStringBounder()).getHeight()));
}

for (FilesEntry ent : root)
ug = ent.drawAndMove(ug, fontConfiguration, skinParam, 0);
}

public void add(String line) {
if (line.startsWith("/"))
tmp.add(line.substring(1));
root.addRawEntry(line.substring(1));
}

}
40 changes: 40 additions & 0 deletions src/net/sourceforge/plantuml/filesdiagram/FilesType.java
@@ -0,0 +1,40 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://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.filesdiagram;

public enum FilesType {
FOLDER, DATA;

}
30 changes: 15 additions & 15 deletions src/net/sourceforge/plantuml/klimt/sprite/SpriteColor.java
Expand Up @@ -66,26 +66,26 @@ public SpriteColor(int width, int height) {
}

public void setGray(int x, int y, int level) {
if (x < 0 || x >= width) {
if (x < 0 || x >= width)
return;
}
if (y < 0 || y >= height) {

if (y < 0 || y >= height)
return;
}
if (level < 0 || level >= 16) {

if (level < 0 || level >= 16)
throw new IllegalArgumentException();
}

gray[y][x] = level;
color[y][x] = -1;
}

public void setColor(int x, int y, int col) {
if (x < 0 || x >= width) {
if (x < 0 || x >= width)
return;
}
if (y < 0 || y >= height) {

if (y < 0 || y >= height)
return;
}

gray[y][x] = -1;
color[y][x] = col;
}
Expand All @@ -99,14 +99,14 @@ public int getWidth() {
}

public UImage toUImage(ColorMapper colorMapper, HColor backcolor, HColor forecolor) {
final BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

if (backcolor == null) {
if (backcolor == null)
backcolor = HColors.WHITE;
}
if (forecolor == null) {

if (forecolor == null)
forecolor = HColors.BLACK;
}

final HColorGradient gradient = HColors.gradient(backcolor, forecolor, '\0');
for (int col = 0; col < width; col++) {
for (int line = 0; line < height; line++) {
Expand Down
16 changes: 16 additions & 0 deletions src/net/sourceforge/plantuml/nwdiag/NwDiagram.java
Expand Up @@ -150,11 +150,27 @@ public CommandExecutionResult openNetwork(String name) {
for (NStackable element : stack)
if (element instanceof Network)
return CommandExecutionResult.error("Cannot nest network");

if (networks.size() == 0 && groups.size() == 0)
eventuallyConnectAllStandaloneServersToHiddenNetwork();

final Network network = createNetwork(name);
stack.add(0, network);
return CommandExecutionResult.ok();
}

private void eventuallyConnectAllStandaloneServersToHiddenNetwork() {
Network first = null;
for (NServer server : servers.values())
if (server.isAlone()) {
if (first == null) {
first = createNetwork("");
first.goInvisible();
}
server.connectMeIfAlone(first);
}
}

public CommandExecutionResult closeSomething() {
if (initDone == false)
return errorNoInit();
Expand Down
2 changes: 2 additions & 0 deletions src/net/sourceforge/plantuml/nwdiag/core/NServer.java
Expand Up @@ -112,6 +112,8 @@ public void blankSomeAddress() {
}

public void learnThisAddress(String address) {
if (address == null)
address = "";
for (Entry<Network, String> ent : connections.entrySet()) {
if (ent.getValue().length() == 0) {
connections.put(ent.getKey(), address);
Expand Down
8 changes: 7 additions & 1 deletion src/net/sourceforge/plantuml/security/SURL.java
Expand Up @@ -250,7 +250,7 @@ private boolean isUrlOk() {
/**
* Regex to remove the UserInfo part from a URL.
*/
private static final Pattern PATTERN_USERINFO = Pattern.compile("(^https?://)([-_0-9a-zA-Z]+@)([^@]*)");
private static final Pattern PATTERN_USERINFO = Pattern.compile("(^https?://)([-_0-9a-zA-Z]+@)([^@]*)$");

private static final ExecutorService EXE = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(Runnable r) {
Expand Down Expand Up @@ -292,6 +292,9 @@ public String toString() {
}

private boolean forbiddenURL(String full) {
// Thanks to Agasthya Kasturi
if (full.contains("@"))
return true;
if (full.startsWith("https://") == false && full.startsWith("http://") == false)
return true;
if (full.matches("^https?://[-#.0-9:\\[\\]+]+/.*"))
Expand All @@ -305,6 +308,9 @@ private boolean forbiddenURL(String full) {

private boolean isInUrlAllowList() {
final String full = cleanPath(internal.toString());
// Thanks to Agasthya Kasturi
if (full.contains("@"))
return false;
for (String allow : getUrlAllowList())
if (full.startsWith(cleanPath(allow)))
return true;
Expand Down

1 comment on commit b32500b

@andrewshadura
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.