From 5cc18bdc030e3c481fa7b12b68c6c52583dae754 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Wed, 30 Nov 2011 09:43:11 -0500 Subject: [PATCH] handle file: URLs the same as files; support including absolute filenames --- .../com/typesafe/config/impl/ConfigUtil.java | 16 ++++++++++++ .../com/typesafe/config/impl/Parseable.java | 25 +++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigUtil.java b/config/src/main/java/com/typesafe/config/impl/ConfigUtil.java index c3f0edf5..6f7b2c5a 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigUtil.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigUtil.java @@ -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()); + } + } } diff --git a/config/src/main/java/com/typesafe/config/impl/Parseable.java b/config/src/main/java/com/typesafe/config/impl/Parseable.java index 71808188..20661f72 100644 --- a/config/src/main/java/com/typesafe/config/impl/Parseable.java +++ b/config/src/main/java/com/typesafe/config/impl/Parseable.java @@ -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; + } } }