aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/java/org/apache/fop/apps/FOURIResolver.java39
-rw-r--r--test/java/org/apache/fop/config/FOURIResolverTestCase.java56
-rw-r--r--test/java/org/apache/fop/config/FontBaseBadTestCase.java11
3 files changed, 85 insertions, 21 deletions
diff --git a/src/java/org/apache/fop/apps/FOURIResolver.java b/src/java/org/apache/fop/apps/FOURIResolver.java
index 58f527abe..f96711d31 100644
--- a/src/java/org/apache/fop/apps/FOURIResolver.java
+++ b/src/java/org/apache/fop/apps/FOURIResolver.java
@@ -24,6 +24,8 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
@@ -32,8 +34,10 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
import org.apache.xmlgraphics.util.io.Base64EncodeStream;
import org.apache.xmlgraphics.util.uri.CommonURIResolver;
@@ -74,20 +78,31 @@ public class FOURIResolver implements javax.xml.transform.URIResolver {
base += "/";
}
File dir = new File(base);
- try {
- base = (dir.isDirectory() ? dir.toURI().toURL() : new URL(base)).toExternalForm();
- } catch (MalformedURLException mfue) {
- String message = mfue.getMessage();
- if (!dir.isDirectory()) {
- message = "base " + base + " is not a directory and not a valid URL: " + message;
- mfue = new MalformedURLException(message);
+ if (dir.isDirectory()) {
+ return dir.toURI().toASCIIString();
+ } else {
+ URI baseURI;
+ try {
+ baseURI = new URI(base);
+ String scheme = baseURI.getScheme();
+ boolean directoryExists = true;
+ if ("file".equals(scheme)) {
+ dir = FileUtils.toFile(baseURI.toURL());
+ directoryExists = dir.isDirectory();
+ }
+ if (scheme == null || !directoryExists) {
+ String message = "base " + base + " is not a valid directory";
+ if (throwExceptions) {
+ throw new MalformedURLException(message);
+ }
+ log.error(message);
+ }
+ return baseURI.toASCIIString();
+ } catch (URISyntaxException e) {
+ //TODO not ideal: our base URLs are actually base URIs.
+ throw new MalformedURLException(e.getMessage());
}
- if (throwExceptions) {
- throw mfue;
- }
- log.error(message);
}
- return base;
}
/**
diff --git a/test/java/org/apache/fop/config/FOURIResolverTestCase.java b/test/java/org/apache/fop/config/FOURIResolverTestCase.java
new file mode 100644
index 000000000..e6f8db712
--- /dev/null
+++ b/test/java/org/apache/fop/config/FOURIResolverTestCase.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.config;
+
+import java.net.MalformedURLException;
+
+import junit.framework.TestCase;
+
+import org.apache.fop.apps.FOURIResolver;
+
+/**
+ * This tests some aspects of the {@link FOURIResolver} class.
+ */
+public class FOURIResolverTestCase extends TestCase {
+
+ /**
+ * Checks the {@link FOURIResolver#checkBaseURL(String)} method.
+ * @throws Exception if an error occurs
+ */
+ public void testCheckBaseURI() throws Exception {
+ FOURIResolver resolver = new FOURIResolver(true);
+ System.out.println(resolver.checkBaseURL("./test/config"));
+ System.out.println(resolver.checkBaseURL("file:test/config"));
+ System.out.println(resolver.checkBaseURL("fantasy:myconfig"));
+ try {
+ resolver.checkBaseURL("./doesnotexist");
+ fail("Expected an exception for a inexistent base directory");
+ } catch (MalformedURLException mfue) {
+ //expected
+ }
+ try {
+ resolver.checkBaseURL("file:doesnotexist");
+ fail("Expected an exception for a inexistent base URI");
+ } catch (MalformedURLException mfue) {
+ //expected
+ }
+ }
+
+}
diff --git a/test/java/org/apache/fop/config/FontBaseBadTestCase.java b/test/java/org/apache/fop/config/FontBaseBadTestCase.java
index b22d0f4f3..792acf59a 100644
--- a/test/java/org/apache/fop/config/FontBaseBadTestCase.java
+++ b/test/java/org/apache/fop/config/FontBaseBadTestCase.java
@@ -20,7 +20,7 @@
package org.apache.fop.config;
/*
- * this font base does not exist and a relative font path is used
+ * This font base does not exist and a relative font path is used.
*/
public class FontBaseBadTestCase extends BaseDestructiveUserConfigTestCase {
@@ -28,14 +28,7 @@ public class FontBaseBadTestCase extends BaseDestructiveUserConfigTestCase {
super(name);
}
- public void testUserConfig() throws Exception {
- // Override this method from the super-class and do nothing as this test doesn't pass ATM
- // TODO re-enable later
- }
-
- /**
- * @see org.apache.fop.config.BaseUserConfigTestCase#getUserConfigFilename()
- */
+ /** {@inheritDoc} */
public String getUserConfigFilename() {
return "test_fontbase_bad.xconf";
}