camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1432435 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/xml/ main/java/org/apache/camel/component/xslt/ test/java/org/apache/camel/component/xslt/
Date Sat, 12 Jan 2013 14:32:53 GMT
Author: davsclaus
Date: Sat Jan 12 14:32:52 2013
New Revision: 1432435

URL: http://svn.apache.org/viewvc?rev=1432435&view=rev
Log:
CAMEL-5942: Fixed issue with XSLT was not using custom assigned UriResolver to load the initial
xslt resource, but only for other included resources.

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/component/xslt/XsltEndpoint.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.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=1432435&r1=1432434&r2=1432435&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
Sat Jan 12 14:32:52 2013
@@ -82,6 +82,13 @@ public class XsltUriResolver implements 
             return new StreamSource(file);
         }
 
+        // 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;
+            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)) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java?rev=1432435&r1=1432434&r2=1432435&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java
Sat Jan 12 14:32:52 2013
@@ -17,9 +17,8 @@
 package org.apache.camel.component.xslt;
 
 import java.io.IOException;
-import java.net.URL;
-
-import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
 
 import org.apache.camel.Component;
 import org.apache.camel.Exchange;
@@ -28,7 +27,6 @@ import org.apache.camel.api.management.M
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.builder.xml.XsltBuilder;
 import org.apache.camel.impl.ProcessorEndpoint;
-import org.apache.camel.util.ResourceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,7 +46,6 @@ public class XsltEndpoint extends Proces
         this.xslt = xslt;
         this.resourceUri = resourceUri;
         this.cacheStylesheet = cacheStylesheet;
-        loadResource(xslt, resourceUri);
     }
 
     @ManagedOperation(description = "Clears the cached XSLT stylesheet, forcing to re-load
the stylesheet on next request")
@@ -61,21 +58,12 @@ public class XsltEndpoint extends Proces
         return cacheStylesheet;
     }
 
-    private synchronized void loadResource(XsltBuilder xslt, String resourceUri) throws TransformerConfigurationException,
IOException {
-        LOG.trace("{} loading schema resource: {}", this, resourceUri);
-        // prefer to use URL over InputStream as it loads better with http
-        URL url = ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(),
resourceUri);
-        xslt.setTransformerURL(url);
-        // now loaded so clear flag
-        cacheCleared = false;
-    }
-
     public XsltEndpoint findOrCreateEndpoint(String uri, String newResourceUri) {
         String newUri = uri.replace(resourceUri, newResourceUri);
         LOG.trace("Getting endpoint with URI: {}", newUri);
         return getCamelContext().getEndpoint(newUri, XsltEndpoint.class);
     }
-    
+
     @Override
     protected void onExchange(Exchange exchange) throws Exception {
         String newResourceUri = exchange.getIn().getHeader(XsltConstants.XSLT_RESOURCE_URI,
String.class);
@@ -86,12 +74,42 @@ public class XsltEndpoint extends Proces
             XsltEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
             newEndpoint.onExchange(exchange);
             return;
-        } else {            
+        } else {
             if (!cacheStylesheet || cacheCleared) {
-                loadResource(xslt, resourceUri);
-            }    
+                loadResource(resourceUri);
+            }
             super.onExchange(exchange);
         }
     }
 
+    /**
+     * Loads the resource.
+     *
+     * @param resourceUri  the resource to load
+     *
+     * @throws TransformerException is thrown if error loading resource
+     * @throws IOException is thrown if error loading resource
+     */
+    protected void loadResource(String resourceUri) throws TransformerException, IOException
{
+        LOG.trace("{} loading schema resource: {}", this, resourceUri);
+        Source source = xslt.getUriResolver().resolve(resourceUri, null);
+        if (source == null) {
+            throw new IOException("Cannot load schema resource " + resourceUri);
+        } else {
+            xslt.setTransformerSource(source);
+        }
+        // now loaded so clear flag
+        cacheCleared = false;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        loadResource(resourceUri);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+    }
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java?rev=1432435&r1=1432434&r2=1432435&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java
Sat Jan 12 14:32:52 2013
@@ -17,7 +17,7 @@
 package org.apache.camel.component.xslt;
 
 import java.io.ByteArrayInputStream;
-
+import java.io.InputStream;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.URIResolver;
@@ -27,6 +27,7 @@ import org.apache.camel.ContextTestSuppo
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.util.ResourceHelper;
 
 /**
  *
@@ -61,6 +62,15 @@ public class XsltCustomizeURIResolverTes
 
             @Override
             public Source resolve(String href, String base) throws TransformerException {
+                if (href.equals("org/apache/camel/component/xslt/include_not_existing_resource.xsl"))
{
+                    try {
+                        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(),
href);
+                        return new StreamSource(is);
+                    } catch (Exception e) {
+                        throw new TransformerException(e);
+                    }
+                }
+
                 Source constantResult = new StreamSource(new ByteArrayInputStream(EXPECTED_XML_CONSTANT.getBytes()));
                 return constantResult;
             }

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=1432435&r1=1432434&r2=1432435&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
Sat Jan 12 14:32:52 2013
@@ -16,7 +16,8 @@
  */
 package org.apache.camel.component.xslt;
 
-import java.io.FileNotFoundException;
+import javax.xml.transform.TransformerException;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.FailedToCreateRouteException;
 import org.apache.camel.ResolveEndpointFailedException;
@@ -38,9 +39,9 @@ public class XsltFileNotFoundTest extend
 
             fail("Should have thrown an exception due XSLT file not found");
         } catch (FailedToCreateRouteException e) {
-            // expected
             assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
-            assertIsInstanceOf(FileNotFoundException.class, e.getCause().getCause());
+            assertIsInstanceOf(TransformerException.class, e.getCause().getCause());
+            assertEquals("Cannot find org/apache/camel/component/xslt/notfound.xsl in classpath",
e.getCause().getCause().getMessage());
         }
     }
 



Mime
View raw message