fix resource loading

This commit is contained in:
Bane Stojkovic
2025-03-24 11:11:39 +01:00
parent b41a073044
commit 3440a48dba
3 changed files with 96 additions and 52 deletions
@@ -125,25 +125,28 @@ public class LangUtil {
public static List<Language> getAvailableLanguages() {
List<Language> res = new ArrayList<Language>();
List<File> files = ResourceUtils.getResourceFiles(LANG_DIR);
Map<String, String> langContents = ResourceUtils.getResourceContents(LANG_DIR);
try {
for (File file : files) {
String key = file.getName().replaceFirst("[.][^.]+$", "");
Map<String, String> details = new HashMap<String, String>();
try (Stream<String> stream = Files.lines(
Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) {
stream.forEach(s -> {
if (s.contains("=")) {
String[] parts = s.split("=");
if (parts.length == 2)
details.put(parts[0].toLowerCase().trim(), parts[1].trim());
langContents.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> {
String key = entry.getKey().replaceFirst("[.][^.]+$", "");
Map<String, String> details = new HashMap<String, String>();
try {
String content = entry.getValue();
String[] lines = content.split("\n");
for (String line : lines) {
if (line.contains("=")) {
String[] parts = line.split("=");
if (parts.length == 2)
details.put(parts[0].toLowerCase().trim(), parts[1].trim());
}
}
} catch (Exception e) {
LOG.error("Error loading language file", e);
}
res.add(new Language(key, details));
});
} catch (IOException e) {
LOG.error("Error loading language file", e);
}
res.add(new Language(key, details));
}
Collections.sort(res);
return res;
} catch (Exception e) {
@@ -3,36 +3,75 @@ package org.diylc.utils;
import org.apache.log4j.Logger;
import org.diylc.lang.LangUtil;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ResourceUtils {
private static final Logger LOG = Logger.getLogger(LangUtil.class);
private static final Logger LOG = Logger.getLogger(ResourceUtils.class);
public static List<File> getResourceFiles(String dir) {
List<File> files = new ArrayList<>();
URL url = LangUtil.class.getResource("/" + dir);
Path path = null;
public static Map<String, String> getResourceContents(String dir) {
Map<String, String> contents = new HashMap<>();
try {
path = Paths.get(url.toURI());
Files.walk(path, 5).forEach(p -> {
if (!p.toFile().isDirectory()) {
files.add(p.toFile());
// Get all resources in the directory
URL dirUrl = ResourceUtils.class.getClassLoader().getResource(dir);
if (dirUrl != null) {
if (dirUrl.getProtocol().equals("jar")) {
// Running from JAR
String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
JarFile jarFile = new JarFile(jarPath);
try {
jarFile.stream()
.filter(entry -> entry.getName().startsWith(dir + "/") && !entry.isDirectory())
.forEach(entry -> {
try {
String relativePath = entry.getName().substring(dir.length() + 1);
// Read the entire file content as string
String content = new String(jarFile.getInputStream(entry).readAllBytes(), StandardCharsets.UTF_8);
contents.put(relativePath, content);
} catch (IOException e) {
LOG.warn("Could not read resource: " + entry.getName(), e);
}
});
} finally {
try {
jarFile.close();
} catch (IOException e) {
LOG.warn("Could not close JAR file", e);
}
}
} else {
// Running from filesystem
java.io.File dirFile = new java.io.File(dirUrl.toURI());
if (dirFile.exists() && dirFile.isDirectory()) {
java.io.File[] files = dirFile.listFiles();
if (files != null) {
for (java.io.File file : files) {
if (file.isFile()) {
try {
String relativePath = file.getName();
String content = new String(java.nio.file.Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
contents.put(relativePath, content);
} catch (IOException e) {
LOG.warn("Could not read file: " + file, e);
}
}
}
}
}
}
});
} catch (URISyntaxException e) {
LOG.error("Error loading language file", e);
} catch (IOException e) {
LOG.error("Error loading language file", e);
}
} catch (Exception e) {
LOG.error("Error loading resources from directory: " + dir, e);
}
return files;
return contents;
}
}