Skip to content
Snippets Groups Projects
Commit dca1b72b authored by Georg Graßnick's avatar Georg Graßnick :thinking:
Browse files

Distinguish between relative and absolute resource paths

Absolute paths begin with a / character and are interpreted relatively to the
ClassLoader default resource path.
parent 0a35a176
No related branches found
No related tags found
1 merge request!36Bug/file path issues
...@@ -152,15 +152,23 @@ public final class JavaPropertiesConfigurationParser extends ConfigurationParser ...@@ -152,15 +152,23 @@ public final class JavaPropertiesConfigurationParser extends ConfigurationParser
*/ */
private void includeResources(final String fileList, final URL parentUrl) throws ConfigurationParsingException, ConfigurationValidationException { private void includeResources(final String fileList, final URL parentUrl) throws ConfigurationParsingException, ConfigurationValidationException {
for (String s : fileList.split(",")) { for (String s : fileList.split(",")) {
s = s.trim();
boolean isAbsolutePath = s.startsWith("/");
URL newUrl = null; URL newUrl = null;
try { // If the value begins with a "/", treat path as absolute path in resources
newUrl = new URL(parentUrl.getProtocol(), parentUrl.getHost(), UrlHelper.getPathString(parentUrl) + "/" + s.trim() + INCLUDE_FILE_EXTENSION); if (isAbsolutePath) {
} catch (MalformedURLException e) { newUrl = getClass().getClassLoader().getResource(s.substring(1) + INCLUDE_FILE_EXTENSION);
throw new ConfigurationParsingException("Could not generate URI", e); // else treat relative
} else {
try {
newUrl = new URL(parentUrl.getProtocol(), parentUrl.getHost(), UrlHelper.getPathString(parentUrl) + "/" + s.trim() + INCLUDE_FILE_EXTENSION);
} catch (MalformedURLException e) {
throw new ConfigurationParsingException("Could not create URL to relative resource", e);
}
} }
mLogger.debug("Prepare recursive parsing of properties file in the java resources at \"{}\"", newUrl); mLogger.debug("Prepare recursive parsing of properties file in the java resources at \"{}\"", UrlHelper.getString(newUrl));
try (InputStream is = newUrl.openStream()) { try (InputStream is = newUrl.openStream()) {
parse(is, UrlHelper.getParentUrl(newUrl)); parse(is, UrlHelper.getParentUrl(newUrl));
......
...@@ -26,6 +26,10 @@ public final class UrlHelper { ...@@ -26,6 +26,10 @@ public final class UrlHelper {
return URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8); return URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8);
} }
public static String getString(final URL url) {
return URLDecoder.decode(url.toString(), StandardCharsets.UTF_8);
}
/** /**
* Returns the URL to the parent directory of a File / Resource. * Returns the URL to the parent directory of a File / Resource.
* @param resourcePath The URL to analyze. * @param resourcePath The URL to analyze.
...@@ -33,7 +37,7 @@ public final class UrlHelper { ...@@ -33,7 +37,7 @@ public final class UrlHelper {
* @throws RuntimeException if the generated URL is not a valid URL. * @throws RuntimeException if the generated URL is not a valid URL.
*/ */
public static URL getParentUrl(final URL resourcePath) throws ConfigurationParsingException { public static URL getParentUrl(final URL resourcePath) throws ConfigurationParsingException {
String fileString = getPathString(resourcePath); String fileString = getString(resourcePath);
String parentString = fileString.substring(0, fileString.lastIndexOf("/")); String parentString = fileString.substring(0, fileString.lastIndexOf("/"));
try { try {
return new URL(resourcePath.getProtocol(), resourcePath.getHost(), parentString); return new URL(resourcePath.getProtocol(), resourcePath.getHost(), parentString);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment