[FIX] resource Uri finder

This commit is contained in:
Edouard DUPIN 2024-06-10 22:08:16 +02:00
parent 5a5e7de99d
commit 0067726c33

View File

@ -14,30 +14,32 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class Uri { public class Uri {
final static Logger LOGGER = LoggerFactory.getLogger(Uri.class); final static Logger LOGGER = LoggerFactory.getLogger(Uri.class);
private record LibraryElement( private record LibraryElement(
Class<?> klass, Class<?> klass,
String basePath) {} String basePath) {}
private static Map<String, String> genericMap = new HashMap<>(); private static Map<String, String> genericMap = new HashMap<>();
private static Map<String, LibraryElement> libraries = new HashMap<>(); private static Map<String, LibraryElement> libraries = new HashMap<>();
private static Class<?> applicationClass = null; private static Class<?> applicationClass = null;
private static String applicationBasePath = ""; private static String applicationBasePath = "";
static { static {
Uri.genericMap.put("DATA", "data/"); Uri.genericMap.put("DATA", "data/");
Uri.genericMap.put("THEME", "theme/"); Uri.genericMap.put("THEME", "theme/");
Uri.genericMap.put("FONTS", "fonts/"); Uri.genericMap.put("FONTS", "fonts/");
Uri.genericMap.put("TRANSLATE", "translate/"); Uri.genericMap.put("TRANSLATE", "translate/");
} }
public static void addLibrary(final String libName, final Class<?> classHandle, String basePath) { public static void addLibrary(final String libName, final Class<?> classHandle, String basePath) {
LOGGER.trace("Add library reference: lib={} ==> {} base path={}", libName, classHandle.getCanonicalName(), basePath); LOGGER.trace("Add library reference: lib={} ==> {} base path={}", libName, classHandle.getCanonicalName(),
basePath);
if (basePath == null || basePath.isEmpty()) { if (basePath == null || basePath.isEmpty()) {
basePath = "/"; basePath = "/";
} }
@ -49,7 +51,7 @@ public class Uri {
} }
Uri.libraries.put(libName.toLowerCase(), new LibraryElement(classHandle, basePath)); Uri.libraries.put(libName.toLowerCase(), new LibraryElement(classHandle, basePath));
} }
public static byte[] getAllData(final Uri resourceName) { public static byte[] getAllData(final Uri resourceName) {
final InputStream out = Uri.getStream(resourceName); final InputStream out = Uri.getStream(resourceName);
if (out == null) { if (out == null) {
@ -64,43 +66,12 @@ public class Uri {
} }
return data; return data;
} }
// public static Stream<Path> getResources(final URL element) {
// try {
// final URI uri = element.toURI();
// FileSystem fs;
// Path path;
// if (uri.getScheme().contentEquals("jar")) {
// try {
// fs = FileSystems.getFileSystem(uri);
// } catch (final FileSystemNotFoundException e) {
// fs = FileSystems.newFileSystem(uri, Collections.<String, String> emptyMap());
// }
// String pathInJar = "/";
// final String tmpPath = element.getPath();
// final int idSeparate = tmpPath.indexOf('!');
// if (idSeparate != -1) {
// pathInJar = tmpPath.substring(idSeparate + 1);
// while (pathInJar.startsWith("/")) {
// pathInJar = pathInJar.substring(1);
// }
// }
// path = fs.getPath(pathInJar);
// } else {
// fs = FileSystems.getDefault();
// path = Paths.get(uri);
// }
// return Files.walk(path, 1);
// } catch (URISyntaxException | IOException e) {
// e.printStackTrace();
// return Stream.of();
// }
// }
private static List<String> getResourceFiles(final Class<?> clazz, final String path) throws IOException { private static List<String> getResourceFiles(final Class<?> clazz, final String path) throws IOException {
final List<String> filenames = new ArrayList<>(); final List<String> filenames = new ArrayList<>();
try (InputStream in = clazz.getResourceAsStream(path); BufferedReader br = new BufferedReader(new InputStreamReader(in))) { try (InputStream in = clazz.getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
String resource; String resource;
while ((resource = br.readLine()) != null) { while ((resource = br.readLine()) != null) {
filenames.add(resource); filenames.add(resource);
@ -108,8 +79,9 @@ public class Uri {
} }
return filenames; return filenames;
} }
public static InputStream getStream(final Uri uri) { public static InputStream getStream(final Uri uri) {
LOGGER.trace("????????????????????????????????????????????");
LOGGER.trace("Load resource: {}", uri); LOGGER.trace("Load resource: {}", uri);
String offsetGroup = ""; String offsetGroup = "";
if (uri.group != null) { if (uri.group != null) {
@ -134,25 +106,31 @@ public class Uri {
if (Uri.applicationClass == null) { if (Uri.applicationClass == null) {
LOGGER.trace(" !! Application data class is not defined ..."); LOGGER.trace(" !! Application data class is not defined ...");
} else { } else {
String tmpPath = Uri.applicationBasePath + offsetGroup + uri.path; String tmpPath = "/" + Uri.applicationBasePath + offsetGroup + uri.path;
tmpPath = tmpPath.replace("//", "/"); tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());// + " ==> " + applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath()); LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());
final URL realFileName = Uri.applicationClass.getClassLoader().getResource(tmpPath); final URL realFileName = Uri.applicationClass.getClassLoader().getResource(tmpPath);
if (realFileName != null) { if (realFileName != null) {
LOGGER.trace("(appl) >>> {}", realFileName.getFile()); LOGGER.trace("(appl) >>> {}", realFileName.getFile());
} else { } else {
LOGGER.trace("(appl) ??? base folder: {}", Uri.applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath() + Uri.applicationBasePath + offsetGroup + uri.path); LOGGER.trace("(appl) ??? base folder: {}",
Uri.applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath() + tmpPath);
} }
out = Uri.applicationClass.getResourceAsStream(tmpPath); LOGGER.trace("(appl) {} getResourceAsStream({})", Uri.applicationClass.getCanonicalName(), tmpPath);
out = Uri.applicationClass.getResourceAsStream("/" + tmpPath);
if (out == null) { if (out == null) {
LOGGER.trace("(appl) ==> element does not exist ..."); LOGGER.error("(appl) ==> element does not exist ... {}", uri);
// try { /*
// LOGGER.warn("elements: " + getResourceFiles(applicationClass, applicationBasePath + offsetGroup + "/*.*")); try {
// } catch (IOException e) { LOGGER.warn("elements: " + getResourceFiles(applicationClass,
// // TODO Auto-generated catch block BASE_RESOURCE_FOLDER + applicationBasePath + offsetGroup + "/*.*"));
// e.printStackTrace(); } catch (final IOException e) {
// } // TODO Auto-generated catch block
e.printStackTrace();
}
*/
} }
} }
if (out == null) { if (out == null) {
@ -172,21 +150,23 @@ public class Uri {
// // TODO Auto-generated catch block // // TODO Auto-generated catch block
// e.printStackTrace(); // e.printStackTrace();
// } // }
String tmpPath = libraryElement.basePath + offsetGroup + uri.path; String tmpPath = "/" + libraryElement.basePath + offsetGroup + uri.path;
tmpPath = tmpPath.replace("//", "/"); tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
;
LOGGER.trace("(lib) Try to load '{}' in {}", tmpPath, libraryElement.klass.getCanonicalName()); LOGGER.trace("(lib) Try to load '{}' in {}", tmpPath, libraryElement.klass.getCanonicalName());
final URL realFileName = libraryElement.klass.getClassLoader().getResource(tmpPath); final URL realFileName = libraryElement.klass.getClassLoader().getResource(tmpPath);
if (realFileName != null) { if (realFileName != null) {
LOGGER.trace("(lib) >>> {}", realFileName.getFile()); LOGGER.trace("(lib) >>> {}", realFileName.getFile());
} else { } else {
LOGGER.trace("(lib) ??? base folder: {}", libraryElement.klass.getProtectionDomain().getCodeSource().getLocation().getPath() + libraryElement.basePath + offsetGroup + uri.path); LOGGER.trace("(lib) ??? base folder: {}",
libraryElement.klass.getProtectionDomain().getCodeSource().getLocation().getPath() + tmpPath);
} }
out = libraryElement.klass.getResourceAsStream(tmpPath); out = libraryElement.klass.getResourceAsStream("/" + tmpPath);
if (out == null) { if (out == null) {
LOGGER.trace("(lib) ==> element does not exist ..."); LOGGER.trace("(lib) ==> element does not exist ...");
} }
} }
if (out == null) { if (out == null) {
LOGGER.error("Can not load resource: '" + uri + "'"); LOGGER.error("Can not load resource: '" + uri + "'");
} else { } else {
@ -194,19 +174,19 @@ public class Uri {
} }
return out; return out;
} }
public static List<Uri> listRecursive(final Uri uri) { public static List<Uri> listRecursive(final Uri uri) {
final List<Uri> out = new ArrayList<>(); final List<Uri> out = new ArrayList<>();
LOGGER.error("TODO: not implemented function ..."); LOGGER.error("TODO: not implemented function ...");
return out; return out;
} }
public static void setApplication(final Class<?> classHandle) { public static void setApplication(final Class<?> classHandle) {
Uri.setApplication(classHandle, ""); Uri.setApplication(classHandle, "");
} }
public static void setApplication(final Class<?> classHandle, String basePath) { public static void setApplication(final Class<?> classHandle, String basePath) {
LOGGER.info ("Set application reference : {} base path={}", classHandle.getCanonicalName(), basePath); LOGGER.info("Set application reference : {} base path={}", classHandle.getCanonicalName(), basePath);
Uri.applicationClass = classHandle; Uri.applicationClass = classHandle;
if (basePath == null || basePath.isEmpty()) { if (basePath == null || basePath.isEmpty()) {
basePath = "/"; basePath = "/";
@ -219,9 +199,9 @@ public class Uri {
} }
Uri.applicationBasePath = basePath; Uri.applicationBasePath = basePath;
} }
public static void setGroup(final String groupName, String basePath) { public static void setGroup(final String groupName, String basePath) {
LOGGER.info ("Set Group : {} base path={}", groupName, basePath); LOGGER.info("Set Group : {} base path={}", groupName, basePath);
if (basePath == null || basePath.isEmpty()) { if (basePath == null || basePath.isEmpty()) {
basePath = "/"; basePath = "/";
} }
@ -230,7 +210,7 @@ public class Uri {
} }
Uri.genericMap.put(groupName.toUpperCase(), basePath); Uri.genericMap.put(groupName.toUpperCase(), basePath);
} }
public static Uri valueOf(String value) { public static Uri valueOf(String value) {
String group = null; String group = null;
String path = null; String path = null;
@ -246,16 +226,14 @@ public class Uri {
if (index != -1) { if (index != -1) {
final String valuesMetadata = value.substring(index + 1); final String valuesMetadata = value.substring(index + 1);
final String[] elementMetaData = valuesMetadata.split("&"); final String[] elementMetaData = valuesMetadata.split("&");
for (int iii = 0; iii < elementMetaData.length; iii++) { for (final String element : elementMetaData) {
final String[] keyVal = elementMetaData[iii].split("=", 2); final String[] keyVal = element.split("=", 2);
if (keyVal.length == 1) { if (keyVal.length == 1) {
prop.put(keyVal[0], ""); prop.put(keyVal[0], "");
} else if (keyVal[0].equals("lib")) {
prop.put(keyVal[0], keyVal[1].toLowerCase());
} else { } else {
if (keyVal[0].equals("lib")) { prop.put(keyVal[0], keyVal[1]);
prop.put(keyVal[0], keyVal[1].toLowerCase());
} else {
prop.put(keyVal[0], keyVal[1]);
}
} }
} }
path = value.substring(0, index); path = value.substring(0, index);
@ -264,7 +242,7 @@ public class Uri {
} }
return new Uri(group, path, prop); return new Uri(group, path, prop);
} }
public static void writeAll(final Uri uri, final String data) throws IOException { public static void writeAll(final Uri uri, final String data) throws IOException {
BufferedWriter out = null; BufferedWriter out = null;
try { try {
@ -286,7 +264,7 @@ public class Uri {
} }
} }
} }
public static void writeAllAppend(final Uri uri, final String data) { public static void writeAllAppend(final Uri uri, final String data) {
BufferedWriter out = null; BufferedWriter out = null;
try { try {
@ -306,17 +284,17 @@ public class Uri {
} }
} }
} }
private final String group; private final String group;
private final String path; private final String path;
private final Map<String, String> properties; private final Map<String, String> properties;
public Uri(final String path) { public Uri(final String path) {
this(null, path); this(null, path);
} }
public Uri(final String group, final String path) { public Uri(final String group, final String path) {
if (group == null) { if (group == null) {
this.group = null; this.group = null;
@ -326,7 +304,7 @@ public class Uri {
this.path = path; this.path = path;
this.properties = new HashMap<>(); this.properties = new HashMap<>();
} }
public Uri(final String group, final String path, final Map<String, String> properties) { public Uri(final String group, final String path, final Map<String, String> properties) {
if (group == null) { if (group == null) {
this.group = null; this.group = null;
@ -336,19 +314,19 @@ public class Uri {
this.path = path; this.path = path;
this.properties = new HashMap<>(properties); this.properties = new HashMap<>(properties);
} }
public Uri(final String group, final String path, final String lib) { public Uri(final String group, final String path, final String lib) {
this(group, path); this(group, path);
if (lib != null) { if (lib != null) {
this.properties.put("lib", lib.toLowerCase()); this.properties.put("lib", lib.toLowerCase());
} }
} }
@Override @Override
public Uri clone() { public Uri clone() {
return new Uri(this.group, this.path, new HashMap<>(this.properties)); return new Uri(this.group, this.path, new HashMap<>(this.properties));
} }
public boolean exist() { public boolean exist() {
final InputStream stream = Uri.getStream(this); final InputStream stream = Uri.getStream(this);
if (stream == null) { if (stream == null) {
@ -362,16 +340,16 @@ public class Uri {
} }
return true; return true;
} }
public String get() { public String get() {
return getPath(); return getPath();
} }
public String getExtention() { public String getExtention() {
final String[] ret = this.path.split("\\."); final String[] ret = this.path.split("\\.");
return ret[ret.length - 1]; return ret[ret.length - 1];
} }
/** /**
* Get the filename of the URI with the extension "plop.txt" * Get the filename of the URI with the extension "plop.txt"
* @return simple filename * @return simple filename
@ -379,7 +357,7 @@ public class Uri {
public String getFileName() { public String getFileName() {
return this.path.substring(this.path.lastIndexOf("/") + 1); return this.path.substring(this.path.lastIndexOf("/") + 1);
} }
/** /**
* Get the filename of the URI without the extension "plop" * Get the filename of the URI without the extension "plop"
* @return simple filename * @return simple filename
@ -388,51 +366,51 @@ public class Uri {
final String ext = getExtention(); final String ext = getExtention();
return this.path.substring(this.path.lastIndexOf("/") + 1, this.path.length() - ext.length() + 1); return this.path.substring(this.path.lastIndexOf("/") + 1, this.path.length() - ext.length() + 1);
} }
public String getGroup() { public String getGroup() {
return this.group; return this.group;
} }
public Uri getParent() { public Uri getParent() {
final String path = this.path.substring(0, this.path.lastIndexOf("/")); final String path = this.path.substring(0, this.path.lastIndexOf("/"));
return new Uri(getGroup(), path, this.properties); return new Uri(getGroup(), path, this.properties);
} }
public String getPath() { public String getPath() {
return this.path; return this.path;
} }
public Map<String, String> getproperties() { public Map<String, String> getproperties() {
return this.properties; return this.properties;
} }
public String getProperty(final String key) { public String getProperty(final String key) {
return this.properties.get(key); return this.properties.get(key);
} }
public String getValue() { public String getValue() {
return toString(); return toString();
} }
public boolean hasProperty(final String key) { public boolean hasProperty(final String key) {
return this.properties.containsKey(key); return this.properties.containsKey(key);
} }
public boolean isEmpty() { public boolean isEmpty() {
return this.path == null || this.path.isEmpty(); return this.path == null || this.path.isEmpty();
} }
public Uri pathAdd(final String value) { public Uri pathAdd(final String value) {
if (this.path.charAt(this.path.length() - 1) == '/') { if (this.path.charAt(this.path.length() - 1) == '/') {
return withPath(this.path + value); return withPath(this.path + value);
} }
return withPath(this.path + "/" + value); return withPath(this.path + "/" + value);
} }
public void setProperty(final String key, final String value) { public void setProperty(final String key, final String value) {
this.properties.put(key, value); this.properties.put(key, value);
} }
@Override @Override
public String toString() { public String toString() {
final StringBuilder out = new StringBuilder(); final StringBuilder out = new StringBuilder();
@ -457,18 +435,18 @@ public class Uri {
} }
return out.toString(); return out.toString();
} }
// Format : DATA:jlfqkjsdflkjqs/sqldkhjflqksdjf/lll.png?lib=ewol // Format : DATA:jlfqkjsdflkjqs/sqldkhjflqksdjf/lll.png?lib=ewol
public Uri withGroup(final String group) { public Uri withGroup(final String group) {
return new Uri(group, this.path, new HashMap<>(this.properties)); return new Uri(group, this.path, new HashMap<>(this.properties));
} }
public Uri withLib(final String lib) { public Uri withLib(final String lib) {
final Map<String, String> tmp = new HashMap<>(this.properties); final Map<String, String> tmp = new HashMap<>(this.properties);
tmp.put("lib", lib); tmp.put("lib", lib);
return new Uri(this.group, this.path, tmp); return new Uri(this.group, this.path, tmp);
} }
public Uri withPath(final String path) { public Uri withPath(final String path) {
return new Uri(this.group, path, new HashMap<>(this.properties)); return new Uri(this.group, path, new HashMap<>(this.properties));
} }