handle file: URLs the same as files; support including absolute filenames

This commit is contained in:
Havoc Pennington 2011-11-30 09:43:11 -05:00
parent 9689abd781
commit 5cc18bdc03
2 changed files with 34 additions and 7 deletions

View File

@ -3,6 +3,10 @@
*/
package com.typesafe.config.impl;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import com.typesafe.config.ConfigException;
@ -130,4 +134,16 @@ final public class ConfigUtil {
throw e;
}
}
static File urlToFile(URL url) {
// this isn't really right, clearly, but not sure what to do.
try {
// this will properly handle hex escapes, etc.
return new File(url.toURI());
} catch (URISyntaxException e) {
// this handles some stuff like file:///c:/Whatever/
// apparently but mangles handling of hex escapes
return new File(url.getPath());
}
}
}

View File

@ -338,7 +338,13 @@ public abstract class Parseable implements ConfigParseable {
}
public static Parseable newURL(URL input, ConfigParseOptions options) {
return new ParseableURL(input, options);
// we want file: URLs and files to always behave the same, so switch
// to a file if it's a file: URL
if (input.getProtocol().equals("file")) {
return newFile(ConfigUtil.urlToFile(input), options);
} else {
return new ParseableURL(input, options);
}
}
private final static class ParseableFile extends Parseable {
@ -362,13 +368,18 @@ public abstract class Parseable implements ConfigParseable {
@Override
ConfigParseable relativeTo(String filename) {
try {
URL url = relativeTo(input.toURI().toURL(), filename);
if (url == null)
File f = new File(filename);
if (f.isAbsolute()) {
return newFile(f, options().setOriginDescription(null));
} else {
try {
URL url = relativeTo(input.toURI().toURL(), filename);
if (url == null)
return null;
return newURL(url, options().setOriginDescription(null));
} catch (MalformedURLException e) {
return null;
return newURL(url, options().setOriginDescription(null));
} catch (MalformedURLException e) {
return null;
}
}
}