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

Move getParentUrl() to static util class

parent 618aaad5
No related branches found
No related tags found
1 merge request!36Bug/file path issues
...@@ -173,7 +173,7 @@ public abstract class ConfigurationParser { ...@@ -173,7 +173,7 @@ public abstract class ConfigurationParser {
mLogger.debug("Starting parsing properties file from java resources: \"{}\"", resource); mLogger.debug("Starting parsing properties file from java resources: \"{}\"", resource);
try { try {
parseConfigFile(resource.openStream(), getParentUrl(resource), assertCompleteness); parseConfigFile(resource.openStream(), UrlHelper.getParentUrl(resource), assertCompleteness);
} catch (IOException e) { } catch (IOException e) {
throw new ConfigurationParsingException("Could not open resource at \"" + resource.toString() + "\"", e); throw new ConfigurationParsingException("Could not open resource at \"" + resource.toString() + "\"", e);
} }
...@@ -212,7 +212,7 @@ public abstract class ConfigurationParser { ...@@ -212,7 +212,7 @@ public abstract class ConfigurationParser {
// reset internal property buffer // reset internal property buffer
mPrinterProperties.clear(); mPrinterProperties.clear();
mFormatProperties.clear(); mFormatProperties.clear();
mValidator.setSearchPath(getPath(path)); mValidator.setSearchPath(getPathNoFilePrefix(path));
// load and parse file // load and parse file
parse(config, path); parse(config, path);
// build printer object from added properties // build printer object from added properties
...@@ -241,29 +241,13 @@ public abstract class ConfigurationParser { ...@@ -241,29 +241,13 @@ public abstract class ConfigurationParser {
} }
} }
/**
* Returns the URL to the parent directory of a File / Resource.
* @param resourcePath The URL to analyze.
* @return The URL to the parent directory of the specified URL.
* @throws ConfigurationParsingException if the generated URL is not a valid URL.
*/
private static URL getParentUrl(final URL resourcePath) throws ConfigurationParsingException {
String fileString = resourcePath.getPath();
String parentString = fileString.substring(0, fileString.lastIndexOf("/"));
try {
return new URL(resourcePath.getProtocol(), resourcePath.getHost(), parentString);
} catch (MalformedURLException e) {
throw new ConfigurationParsingException("Could not create URL to parent path", e);
}
}
/** /**
* Return a String representation of the path of a {@link URL}. * Return a String representation of the path of a {@link URL}.
* Strips the {@literal "}file:{@literal "} prefix from an URL, if it exist. * Strips the {@literal "}file:{@literal "} prefix from an URL, if it exist.
* @param url The URL that needs to be stripped * @param url The URL that needs to be stripped
* @return The String representation of the path of a URL where the leading {@literal "}file:{@literal "} prefix is stripped. * @return The String representation of the path of a URL where the leading {@literal "}file:{@literal "} prefix is stripped.
*/ */
private static String getPath(final URL url) throws ConfigurationParsingException { private static String getPathNoFilePrefix(final URL url) throws ConfigurationParsingException {
String urlString = UrlHelper.getPathString(url); String urlString = UrlHelper.getPathString(url);
return urlString.replaceAll("^file:", ""); return urlString.replaceAll("^file:", "");
} }
......
package de.tudresden.inf.mci.brailleplot.util; package de.tudresden.inf.mci.brailleplot.util;
import de.tudresden.inf.mci.brailleplot.configparser.ConfigurationParsingException;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
...@@ -22,4 +25,20 @@ public final class UrlHelper { ...@@ -22,4 +25,20 @@ public final class UrlHelper {
public static String getPathString(final URL url) { public static String getPathString(final URL url) {
return URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8); return URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8);
} }
/**
* Returns the URL to the parent directory of a File / Resource.
* @param resourcePath The URL to analyze.
* @return The URL to the parent directory of the specified URL.
* @throws RuntimeException if the generated URL is not a valid URL.
*/
public static URL getParentUrl(final URL resourcePath) throws ConfigurationParsingException {
String fileString = getPathString(resourcePath);
String parentString = fileString.substring(0, fileString.lastIndexOf("/"));
try {
return new URL(resourcePath.getProtocol(), resourcePath.getHost(), parentString);
} catch (MalformedURLException e) {
throw new RuntimeException("Could not create URL to parent path", e);
}
}
} }
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