mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
fix resource loading
This commit is contained in:
@@ -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())
|
||||||
Map<String, String> details = new HashMap<String, String>();
|
.forEach(entry -> {
|
||||||
try (Stream<String> stream = Files.lines(
|
String key = entry.getKey().replaceFirst("[.][^.]+$", "");
|
||||||
Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) {
|
Map<String, String> details = new HashMap<String, String>();
|
||||||
stream.forEach(s -> {
|
try {
|
||||||
if (s.contains("=")) {
|
String content = entry.getValue();
|
||||||
String[] parts = s.split("=");
|
String[] lines = content.split("\n");
|
||||||
if (parts.length == 2)
|
for (String line : lines) {
|
||||||
details.put(parts[0].toLowerCase().trim(), parts[1].trim());
|
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);
|
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 List<File> getResourceFiles(String dir) {
|
public static Map<String, String> getResourceContents(String dir) {
|
||||||
List<File> files = new ArrayList<>();
|
Map<String, String> contents = new HashMap<>();
|
||||||
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} 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) {
|
} catch (Exception e) {
|
||||||
LOG.error("Error loading language file", e);
|
LOG.error("Error loading resources from directory: " + dir, e);
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.error("Error loading language file", e);
|
|
||||||
}
|
}
|
||||||
return files;
|
|
||||||
|
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())
|
||||||
try {
|
.forEach(entry -> {
|
||||||
InputStream in = new FileInputStream(file);
|
if (entry.getKey().toLowerCase().endsWith(".xml")) {
|
||||||
Theme theme = (Theme) xStream.fromXML(in);
|
try {
|
||||||
LOG.debug("Found theme: " + theme.getName());
|
Theme theme = (Theme) xStream.fromXML(entry.getValue());
|
||||||
swingUI.injectMenuAction(ActionFactory.getInstance().createThemeAction(plugInPort, theme), THEME_MENU);
|
LOG.debug("Found theme: " + theme.getName());
|
||||||
} catch (Exception e) {
|
swingUI.injectMenuAction(ActionFactory.getInstance().createThemeAction(plugInPort, theme), THEME_MENU);
|
||||||
LOG.error("Could not load theme file " + file.getName(), e);
|
} catch (Exception 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user