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