plantuml/src/net/sourceforge/plantuml/sprite/ResourcesUtils.java

127 lines
4.0 KiB
Java
Raw Normal View History

2016-01-30 12:20:07 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2016-01-30 12:20:07 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +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:
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2016-01-30 12:20:07 +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
2022-08-17 17:34:24 +00:00
*
2016-01-30 12:20:07 +00:00
*
*/
2019-08-26 17:07:21 +00:00
package net.sourceforge.plantuml.sprite;
2016-01-30 12:20:07 +00:00
import static java.nio.charset.StandardCharsets.UTF_8;
2016-01-30 12:20:07 +00:00
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2016-01-30 12:20:07 +00:00
import net.sourceforge.plantuml.version.Version;
2022-10-05 20:32:57 +00:00
public class ResourcesUtils {
2016-01-30 12:20:07 +00:00
public static Set<String> getJarFile(String path, boolean folder) throws IOException {
if (path.startsWith("/") || path.endsWith("/")) {
throw new IllegalArgumentException();
}
2017-10-07 09:46:53 +00:00
final String protocol = getProtocol();
2016-01-30 12:20:07 +00:00
if ("file".equals(protocol)) {
final URL local = Version.class.getClassLoader().getResource(path);
try {
2020-05-30 15:20:23 +00:00
return listEntry(new SFile(local.toURI()));
2016-01-30 12:20:07 +00:00
} catch (URISyntaxException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2016-01-30 12:20:07 +00:00
return null;
}
}
if ("jar".equals(protocol)) {
final String classFile = Version.class.getName().replace(".", "/") + ".class";
final URL versionURL = Version.class.getClassLoader().getResource(classFile);
final String jarPath = versionURL.getPath().substring(5, versionURL.getPath().indexOf("!"));
if (folder) {
return listFolders(new JarFile(URLDecoder.decode(jarPath, UTF_8.name())), path + "/");
2016-01-30 12:20:07 +00:00
} else {
return listFiles(new JarFile(URLDecoder.decode(jarPath, UTF_8.name())), path + "/");
2016-01-30 12:20:07 +00:00
}
}
2020-05-30 15:20:23 +00:00
return Collections.<String>emptySet();
2016-01-30 12:20:07 +00:00
}
2017-10-07 09:46:53 +00:00
private static String getProtocol() {
final URL resource = Version.class.getClassLoader().getResource("net/sourceforge/plantuml/version/logo.png");
return resource.getProtocol();
}
2016-01-30 12:20:07 +00:00
private static Set<String> listFiles(JarFile jarFile, String path) {
final Enumeration<JarEntry> entries = jarFile.entries();
2021-05-14 08:42:57 +00:00
final Set<String> result = new TreeSet<>();
2016-01-30 12:20:07 +00:00
while (entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(path)) {
result.add(name.substring(path.length()));
}
}
return result;
}
private static Set<String> listFolders(JarFile jarFile, String path) {
final Enumeration<JarEntry> entries = jarFile.entries();
2021-05-14 08:42:57 +00:00
final Set<String> result = new TreeSet<>();
2016-01-30 12:20:07 +00:00
while (entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(path)) {
final String folder = name.substring(path.length());
final int x = folder.indexOf('/');
if (x != -1) {
result.add(folder.substring(0, x));
}
}
}
return result;
}
2020-05-30 15:20:23 +00:00
private static Set<String> listEntry(SFile dir) {
2021-05-14 08:42:57 +00:00
final Set<String> result = new TreeSet<>();
2016-01-30 12:20:07 +00:00
for (String n : dir.list()) {
result.add(n);
}
return result;
}
}