Author: davsclaus
Date: Sun Jan 13 15:14:00 2013
New Revision: 1432629

URL: http://svn.apache.org/viewvc?rev=1432629&view=rev
Log:
CAMEL-5962: When using XSLT includes will use same file or classpath loading as 
endpoint configuration, if not explicit configured.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java
      - copied, changed from r1432601, 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeTest.java
    camel/trunk/camel-core/src/test/resources/xslt/
    camel/trunk/camel-core/src/test/resources/xslt/common/
    camel/trunk/camel-core/src/test/resources/xslt/common/staff_template.xsl
      - copied unchanged from r1432601, 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/staff_template.xsl
    camel/trunk/camel-core/src/test/resources/xslt/staff/
    camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xsl
      - copied, changed from r1432601, 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/staff_include_relative.xsl
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java?rev=1432629&r1=1432628&r2=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
 Sun Jan 13 15:14:00 2013
@@ -16,7 +16,7 @@
  */
 package org.apache.camel.builder.xml;
 
-import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
@@ -26,6 +26,7 @@ import javax.xml.transform.stream.Stream
 import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,10 +48,17 @@ public class XsltUriResolver implements 
 
     private final ClassResolver resolver;
     private final String location;
+    private final String baseScheme;
 
     public XsltUriResolver(ClassResolver resolver, String location) {
         this.resolver = resolver;
         this.location = location;
+        if (ResourceHelper.hasScheme(location)) {
+            baseScheme = ResourceHelper.getScheme(location);
+        } else {
+            // default to use classpath
+            baseScheme = "classpath:";
+        }
     }
 
     public Source resolve(String href, String base) throws 
TransformerException {
@@ -60,44 +68,41 @@ public class XsltUriResolver implements 
 
         LOG.trace("Resolving URI with href: {} and base: {}", href, base);
 
-        if (href.startsWith("classpath:")) {
-            LOG.debug("Resolving URI from classpath: {}", href);
-
-            String name = ObjectHelper.after(href, ":");
-            InputStream is = resolver.loadResourceAsStream(name);
-            if (is == null) {
-                throw new TransformerException("Cannot find " + name + " in 
classpath");
-            }
-            return new StreamSource(is);
+        String scheme = ResourceHelper.getScheme(href);
+        if (scheme != null && "file:".equals(scheme)) {
+            // need to compact paths for file as it can be relative paths 
using .. to go backwards
+            href = FileUtil.compactPath(href, '/');
         }
 
-        if (href.startsWith("file:")) {
-            LOG.debug("Resolving URI from file: {}", href);
+        if (scheme != null) {
+            LOG.debug("Resolving URI from {}: {}", scheme, href);
 
-            String name = ObjectHelper.after(href, ":");
-            File file = new File(name);
-            if (!file.exists()) {
-                throw new TransformerException("Cannot find " + name + " in 
the file system");
+            InputStream is;
+            try {
+                is = 
ResourceHelper.resolveMandatoryResourceAsInputStream(resolver, href);
+            } catch (IOException e) {
+                throw new TransformerException(e);
             }
-            return new StreamSource(file);
+            return new StreamSource(is);
         }
 
         // if href and location is the same, then its the initial resolve
         if (href.equals(location)) {
-            // default to use classpath: location
-            String path = "classpath:" + href;
+            String path = baseScheme + href;
             return resolve(path, base);
         }
 
         // okay then its relative to the starting location from the XSLT 
component
         String path = FileUtil.onlyPath(location);
         if (ObjectHelper.isEmpty(path)) {
-            // default to use classpath: location
-            path = "classpath:" + href;
+            path = baseScheme + href;
             return resolve(path, base);
         } else {
-            // default to use classpath: location
-            path = "classpath:" + path + "/" + href;
+            if (ResourceHelper.hasScheme(path)) {
+                path = path + "/" + href;
+            } else {
+                path = baseScheme + path + "/" + href;
+            }
             return resolve(path, base);
         }
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java?rev=1432629&r1=1432628&r2=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java 
Sun Jan 13 15:14:00 2013
@@ -55,6 +55,20 @@ public final class ResourceHelper {
     }
 
     /**
+     * Gets the scheme from the URI (e.g. file:, classpath: or http:)
+     *
+     * @param uri  the uri
+     * @return the scheme, or <tt>null</tt> if no scheme
+     */
+    public static String getScheme(String uri) {
+        if (hasScheme(uri)) {
+            return uri.substring(0, uri.indexOf(":") + 1);
+        } else {
+            return null;
+        }
+    }
+
+    /**
      * Resolves the mandatory resource.
      * <p/>
      * If possible recommended to use {@link 
#resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java?rev=1432629&r1=1432628&r2=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
 Sun Jan 13 15:14:00 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.xslt;
 
+import java.io.FileNotFoundException;
 import javax.xml.transform.TransformerException;
 
 import org.apache.camel.CamelContext;
@@ -41,7 +42,7 @@ public class XsltFileNotFoundTest extend
         } catch (FailedToCreateRouteException e) {
             assertIsInstanceOf(ResolveEndpointFailedException.class, 
e.getCause());
             assertIsInstanceOf(TransformerException.class, 
e.getCause().getCause());
-            assertEquals("Cannot find 
org/apache/camel/component/xslt/notfound.xsl in classpath", 
e.getCause().getCause().getMessage());
+            assertIsInstanceOf(FileNotFoundException.class, 
e.getCause().getCause().getCause());
         }
     }
 

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java
 (from r1432601, 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeTest.java&r1=1432601&r2=1432629&rev=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java
 Sun Jan 13 15:14:00 2013
@@ -23,7 +23,7 @@ import org.apache.camel.component.mock.M
 /**
  *
  */
-public class XsltIncludeRelativeTest extends ContextTestSupport {
+public class XsltIncludeRelativeFileSchemeTest extends ContextTestSupport {
 
     public void testXsltIncludeRelative() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
@@ -40,7 +40,7 @@ public class XsltIncludeRelativeTest ext
             @Override
             public void configure() throws Exception {
                 from("file:src/test/data/?fileName=staff.xml&noop=true")
-                    
.to("xslt:org/apache/camel/component/xslt/staff_include_relative.xsl")
+                    .to("xslt:file:src/test/resources/xslt/staff/staff.xsl")
                     .to("log:foo")
                     .to("mock:result");
             }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java?rev=1432629&r1=1432628&r2=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
 Sun Jan 13 15:14:00 2013
@@ -141,6 +141,14 @@ public class ResourceHelperTest extends 
         assertTrue(ResourceHelper.isHttpUri("https://camel.apache.org";));
     }
 
+    public void testGetScheme() throws Exception {
+        assertEquals("file:", ResourceHelper.getScheme("file:myfile.txt"));
+        assertEquals("classpath:", 
ResourceHelper.getScheme("classpath:myfile.txt"));
+        assertEquals("http:", ResourceHelper.getScheme("http:www.foo.com"));
+        assertEquals(null, ResourceHelper.getScheme("www.foo.com"));
+        assertEquals(null, ResourceHelper.getScheme("myfile.txt"));
+    }
+
     public void testAppendParameters() throws Exception {
         Map<String, Object> params = new LinkedHashMap<String, Object>();
         params.put("foo", 123);

Copied: camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xsl (from 
r1432601, 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/staff_include_relative.xsl)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xsl?p2=camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xsl&p1=camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/staff_include_relative.xsl&r1=1432601&r2=1432629&rev=1432629&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/xslt/staff_include_relative.xsl
 (original)
+++ camel/trunk/camel-core/src/test/resources/xslt/staff/staff.xsl Sun Jan 13 
15:14:00 2013
@@ -17,7 +17,7 @@
 -->
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
 
-    <xsl:include href="staff_template.xsl"/>
+    <xsl:include href="../common/staff_template.xsl"/>
 
     <xsl:template match="staff/programmer">
         <html>


Reply via email to