You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/01/13 16:15:34 UTC

svn commit: r1432630 - in /camel/branches/camel-2.10.x: ./ camel-core/src/main/java/org/apache/camel/builder/xml/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/component/xslt/ camel-core/src/test/java/org/apa...

Author: davsclaus
Date: Sun Jan 13 15:15:33 2013
New Revision: 1432630

URL: http://svn.apache.org/viewvc?rev=1432630&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/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java
      - copied unchanged from r1432629, camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltIncludeRelativeFileSchemeTest.java
    camel/branches/camel-2.10.x/camel-core/src/test/resources/xslt/
      - copied from r1432629, camel/trunk/camel-core/src/test/resources/xslt/
Modified:
    camel/branches/camel-2.10.x/   (props changed)
    camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
    camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
    camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
    camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1432629

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java?rev=1432630&r1=1432629&r2=1432630&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java Sun Jan 13 15:15:33 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/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java?rev=1432630&r1=1432629&r2=1432630&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java Sun Jan 13 15:15:33 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/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java?rev=1432630&r1=1432629&r2=1432630&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java Sun Jan 13 15:15:33 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());
         }
     }
 

Modified: camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java?rev=1432630&r1=1432629&r2=1432630&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/util/ResourceHelperTest.java Sun Jan 13 15:15:33 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);