Skip to content

Commit

Permalink
App.getResource now supports local files
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Feb 1, 2023
1 parent bff30a9 commit f00230b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.github.Osiris-Team</groupId>
<artifactId>jlib</artifactId>
<version>16.11</version>
<version>16.12</version>
</dependency>
<!-- https://github.com/jcefmaven/jcefmaven -->
<dependency>
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/osiris/desku/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,23 @@ public static Image getResourceImage(String path) throws IOException {
}

/**
* If the provided resource cannot be found it also checks these directories: <br>
* - App.workingDir <br>
* - App.workingDir + "/src/main/java" <br>
* - App.workingDir + "/src/test/java" <br>
* @param path expected relative path to a file inside the current jar. Example: help.txt or /help.txt
*/
public static InputStream getResource(String path) {
public static InputStream getResource(String path) throws IOException {
String fullPath = (path.startsWith("/") ? path : "/" + path);
return App.class.getResourceAsStream(fullPath);
InputStream in = App.class.getResourceAsStream(fullPath);
if(in != null) return in;
File f = new File(App.workingDir + fullPath);
if(f.exists()) return Files.newInputStream(f.toPath());
f = new File(App.workingDir + "/src/main/java" + fullPath);
if(f.exists()) return Files.newInputStream(f.toPath());
f = new File(App.workingDir + "/src/test/java" + fullPath); // Support JUnit tests
if(f.exists()) return Files.newInputStream(f.toPath());
return null;
}

public static void appendToGlobalStyles(String s) {
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/osiris/desku/VerticalLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.osiris.desku.ui.Component;
import com.osiris.jlib.Stream;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

/**
* Example for a custom component.
Expand All @@ -17,7 +21,7 @@ public class VerticalLayout extends Component<VerticalLayout> {
"}";
// You can also add a css file to the current classes' package/folder.
// The file below is at "com/osiris/desku/VerticalLayout.css"
//styles = Stream.toString(App.getResource(VerticalLayout.class.toString().replace(".","/") + ".css"));
//styles = Stream.toString(App.getResource(VerticalLayout.class.getName().replace(".","/") + ".css"));
App.appendToGlobalStyles(styles);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit f00230b

Please sign in to comment.