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() { public static List<Language> getAvailableLanguages() {
List<Language> res = new ArrayList<Language>(); List<Language> res = new ArrayList<Language>();
List<File> files = ResourceUtils.getResourceFiles(LANG_DIR); Map<String, String> langContents = ResourceUtils.getResourceContents(LANG_DIR);
try { try {
for (File file : files) { langContents.entrySet().stream()
String key = file.getName().replaceFirst("[.][^.]+$", ""); .sorted(Map.Entry.comparingByKey())
.forEach(entry -> {
String key = entry.getKey().replaceFirst("[.][^.]+$", "");
Map<String, String> details = new HashMap<String, String>(); Map<String, String> details = new HashMap<String, String>();
try (Stream<String> stream = Files.lines( try {
Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) { String content = entry.getValue();
stream.forEach(s -> { String[] lines = content.split("\n");
if (s.contains("=")) { for (String line : lines) {
String[] parts = s.split("="); if (line.contains("=")) {
String[] parts = line.split("=");
if (parts.length == 2) if (parts.length == 2)
details.put(parts[0].toLowerCase().trim(), parts[1].trim()); details.put(parts[0].toLowerCase().trim(), parts[1].trim());
} }
}); }
} catch (IOException e) { } catch (Exception e) {
LOG.error("Error loading language file", e); LOG.error("Error loading language file", e);
} }
res.add(new Language(key, details)); res.add(new Language(key, details));
} });
Collections.sort(res); Collections.sort(res);
return res; return res;
} catch (Exception e) { } catch (Exception e) {
@@ -3,36 +3,75 @@ package org.diylc.utils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.lang.LangUtil; import org.diylc.lang.LangUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.util.HashMap;
import java.nio.file.Paths; import java.util.Map;
import java.util.ArrayList; import java.util.jar.JarEntry;
import java.util.List; import java.util.jar.JarFile;
public class ResourceUtils { public class ResourceUtils {
private static final Logger LOG = Logger.getLogger(LangUtil.class); private static final Logger LOG = Logger.getLogger(ResourceUtils.class);
public static Map<String, String> getResourceContents(String dir) {
Map<String, String> contents = new HashMap<>();
public static List<File> getResourceFiles(String dir) {
List<File> files = new ArrayList<>();
URL url = LangUtil.class.getResource("/" + dir);
Path path = null;
try { try {
path = Paths.get(url.toURI()); // Get all resources in the directory
Files.walk(path, 5).forEach(p -> { URL dirUrl = ResourceUtils.class.getClassLoader().getResource(dir);
if (!p.toFile().isDirectory()) { if (dirUrl != null) {
files.add(p.toFile()); 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);
} }
}); });
} catch (URISyntaxException e) { } finally {
LOG.error("Error loading language file", e); try {
jarFile.close();
} catch (IOException e) { } catch (IOException e) {
LOG.error("Error loading language file", e); LOG.warn("Could not close JAR file", e);
} }
return files; }
} 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 (Exception e) {
LOG.error("Error loading resources from directory: " + dir, e);
}
return contents;
} }
} }
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.swing.Icon; import javax.swing.Icon;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
@@ -174,23 +175,24 @@ public class ConfigPlugin implements IPlugIn {
ActionFactory.getInstance() ActionFactory.getInstance()
.createConfigAction(plugInPort, "Sticky Points", IPlugInPort.STICKY_POINTS_KEY, true), CONFIG_MENU); .createConfigAction(plugInPort, "Sticky Points", IPlugInPort.STICKY_POINTS_KEY, true), CONFIG_MENU);
List<File> themeFiles = ResourceUtils.getResourceFiles(THEMES_DIR); Map<String, String> themeContents = ResourceUtils.getResourceContents(THEMES_DIR);
if (!themeFiles.isEmpty()) { if (!themeContents.isEmpty()) {
XStream xStream = new XStream(new DomDriver()); XStream xStream = new XStream(new DomDriver());
xStream.addPermission(AnyTypePermission.ANY); xStream.addPermission(AnyTypePermission.ANY);
swingUI.injectSubmenu(THEME_MENU, IconLoader.Pens.getIcon(), CONFIG_MENU); swingUI.injectSubmenu(THEME_MENU, IconLoader.Pens.getIcon(), CONFIG_MENU);
for (File file : themeFiles) { themeContents.entrySet().stream()
if (file.getName().toLowerCase().endsWith(".xml")) { .sorted(Map.Entry.comparingByKey())
.forEach(entry -> {
if (entry.getKey().toLowerCase().endsWith(".xml")) {
try { try {
InputStream in = new FileInputStream(file); Theme theme = (Theme) xStream.fromXML(entry.getValue());
Theme theme = (Theme) xStream.fromXML(in);
LOG.debug("Found theme: " + theme.getName()); LOG.debug("Found theme: " + theme.getName());
swingUI.injectMenuAction(ActionFactory.getInstance().createThemeAction(plugInPort, theme), THEME_MENU); swingUI.injectMenuAction(ActionFactory.getInstance().createThemeAction(plugInPort, theme), THEME_MENU);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Could not load theme file " + file.getName(), e); LOG.error("Could not load theme file " + entry.getKey(), e);
}
} }
} }
});
} }
swingUI.injectSubmenu(COMPONENT_BROWSER_MENU, IconLoader.Hammer.getIcon(), CONFIG_MENU); swingUI.injectSubmenu(COMPONENT_BROWSER_MENU, IconLoader.Hammer.getIcon(), CONFIG_MENU);