From 1b3a71d4f334c21c2355972a24bac2f4a01d1cb7 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Fri, 8 Nov 2002 10:00:29 +0000 Subject: [PATCH] Little helper class with static methods to build a URL from a string which can be a URL or a filename. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_20_2-maintain@195449 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/tools/URLBuilder.java | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/org/apache/fop/tools/URLBuilder.java diff --git a/src/org/apache/fop/tools/URLBuilder.java b/src/org/apache/fop/tools/URLBuilder.java new file mode 100644 index 000000000..914d2e9d4 --- /dev/null +++ b/src/org/apache/fop/tools/URLBuilder.java @@ -0,0 +1,64 @@ +/* + * $Id$ + * Copyright (C) 2002 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + */ + +package org.apache.fop.tools; + +import java.io.File; +import java.net.URL; +import java.net.MalformedURLException; + +/** + * This utility class is used to build URLs from Strings. The String can be + * normal URLs but also just filenames. The filenames get converted to a + * file URL. + * + * @author Jeremias Maerki + */ +public class URLBuilder { + + /** + * Build an URL based on a String. The String can be a normal URL or a + * filename. Filenames get automatically converted to to URLs. + * + * @param spec A URL or a filename + * @return The requested URL + * @throws MalformedURLException If spec cannot be converted to a URL. + */ + public static URL buildURL(String spec) throws MalformedURLException { + if (spec == null) throw new NullPointerException("spec must not be null"); + File f = new File(spec); + if (f.exists()) { + return f.toURL(); + } else { + URL u1 = new URL(spec); + return u1; + } + } + + + /** + * Build an URL based on a String. The String can be a normal URL or a + * filename. Filenames get automatically converted to to URLs. + * + * @param baseURL Base URL for relative paths + * @param spec A URL or a filename + * @return The requested URL + * @throws MalformedURLException If spec cannot be converted to a URL. + */ + public static URL buildURL(URL baseURL, String spec) throws MalformedURLException { + if (spec == null) throw new NullPointerException("spec must not be null"); + try { + URL u1 = buildURL(spec); + return u1; + } catch (MalformedURLException mfue) { + if (baseURL == null) throw mfue; + URL u2 = new URL(baseURL, spec); + return u2; + } + } + +} \ No newline at end of file -- 2.39.5