You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Will Holcomb <wh...@gmail.com> on 2008/02/28 19:38:21 UTC

chaining XMLFilters and TransformerHanders

I have an application where I would like to have a dynamic list of
XMLFilters (because they're easy to write) some of which are XSLT
stylesheets where the stylesheets have parameters. I have read that it it
not possible to specify parameters on XSLT stylesheets that are entered as:

StreamSource xsltSource = new StreamSource(xsltFilename)
XMLFilter filter = SAXTransformerFactory.newXMLFilter(xsltSource);

I attempted to get around this by doing:

Templates templates = transFactory.newTemplates(xsltSource);
Transformer transform = templates.newTransformer();
for(Map.Entry<String,String> param : templateParams.entrySet()) {
     transform.setParameter(param.getKey(), param.getValue());
}
XMLFilter filter = transFactory.newXMLFilter(templates);

The documentation says newTransform() returns "a transformation context,"
but apparently that context doesn't actually modify the original
Templatesobject since the parameter values don't seem to be set in the
output.

If I knew that all of the custom XMLFilters would come before the Transforms
then I think I could build two separate chains. One of XMLFilters that I use
with a SAXSource and another of TransformHandlers that I terminate with a
Result. I don't want to make that ordering assumption though.

I've tried to find a way to operate entirely using TransformHandlers, but I
can't figure out how to convert an XMLFilter into one.

Are there any suggestions for a method of accomplishing the goal of having a
chain of XMLFilters which include XSLT stylesheets with parameters
specified?

Will

Re: chaining XMLFilters and TransformerHanders

Posted by Will Holcomb <wh...@gmail.com>.
I ended up using the fact that the actual implementing class for XMLFilter
in the 1.5 jdk is
com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilterwhich has a
getTransformer method. I don't really like the code because a change in jdk
could break it. Is there a reason that at the least getTransformer isn't
exposed through getProperty? It seems like at the least it could make it
easy for me to avoid the reflection.

public void addTransform(String xsltFilename, Map<String,String>
templateParams)
  throws TransformerConfigurationException {
   XMLFilter filter = transFactory.newXMLFilter(new
StreamSource(xsltFilename));
   if(templateParams != null && templateParams.size() > 0) {
      try {
         Method getTransformer = filter.getClass
().getMethod("getTransformer");
         Transformer transformer =
(Transformer)getTransformer.invoke(filter);
         for(Map.Entry<String,String> param : templateParams.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
         }
      } catch(NoSuchMethodException nsme) {
         System.err.println("No Method getTransformer(): Cannot set
parameters for: " + xsltFilename);
      } catch(InvocationTargetException ite) {
         System.err.println("Invocation Exception on getTransformer():
Cannot set parameters for: " + xsltFilename);
      } catch (IllegalAccessException iae) {
         System.err.println("Illegal Access: Cannot set parameters for: " +
xsltFilename);
      }
   }
   addTransform(filter);
}

On Mon, Mar 3, 2008 at 8:49 AM, Eric J. Schwarzenbach <
Eric.Schwarzenbach@wrycan.com> wrote:

> You could make the data you'd have passed as a parameter available to
> your xslt code via an extension function.
> Or, you could wrap your xml in some additional xml containing the data.
> You could have additional filters in your chain that do the wrapping, as
> well as unwrapping it again later.
>
> Eric
>
> Will Holcomb wrote:
> > I have an application where I would like to have a dynamic list of
> > XMLFilters (because they're easy to write) some of which are XSLT
> > stylesheets where the stylesheets have parameters. I have read that it
> > it not possible to specify parameters on XSLT stylesheets that are
> > entered as:
> >
> > StreamSource xsltSource = new StreamSource(xsltFilename)
> > XMLFilter filter = SAXTransformerFactory.newXMLFilter(xsltSource);
> >
> > I attempted to get around this by doing:
> >
> > Templates templates = transFactory.newTemplates(xsltSource);
> > Transformer transform = templates.newTransformer();
> > for(Map.Entry<String,String> param : templateParams.entrySet()) {
> >      transform.setParameter(param.getKey(), param.getValue());
> > }
> > XMLFilter filter = transFactory.newXMLFilter(templates);
> >
> > The documentation says newTransform() returns "a transformation
> > context," but apparently that context doesn't actually modify the
> > original Templates object since the parameter values don't seem to be
> > set in the output.
> >
> > If I knew that all of the custom XMLFilters would come before the
> > Transforms then I think I could build two separate chains. One of
> > XMLFilters that I use with a SAXSource and another of
> > TransformHandlers that I terminate with a Result. I don't want to make
> > that ordering assumption though.
> >
> > I've tried to find a way to operate entirely using TransformHandlers,
> > but I can't figure out how to convert an XMLFilter into one.
> >
> > Are there any suggestions for a method of accomplishing the goal of
> > having a chain of XMLFilters which include XSLT stylesheets with
> > parameters specified?
> >
> > Will
>

Re: chaining XMLFilters and TransformerHanders

Posted by "Eric J. Schwarzenbach" <Er...@wrycan.com>.
You could make the data you'd have passed as a parameter available to 
your xslt code via an extension function.
Or, you could wrap your xml in some additional xml containing the data. 
You could have additional filters in your chain that do the wrapping, as 
well as unwrapping it again later.

Eric

Will Holcomb wrote:
> I have an application where I would like to have a dynamic list of 
> XMLFilters (because they're easy to write) some of which are XSLT 
> stylesheets where the stylesheets have parameters. I have read that it 
> it not possible to specify parameters on XSLT stylesheets that are 
> entered as:
>
> StreamSource xsltSource = new StreamSource(xsltFilename)
> XMLFilter filter = SAXTransformerFactory.newXMLFilter(xsltSource);
>
> I attempted to get around this by doing:
>
> Templates templates = transFactory.newTemplates(xsltSource);
> Transformer transform = templates.newTransformer();
> for(Map.Entry<String,String> param : templateParams.entrySet()) {
>      transform.setParameter(param.getKey(), param.getValue());
> }
> XMLFilter filter = transFactory.newXMLFilter(templates);
>
> The documentation says newTransform() returns "a transformation 
> context," but apparently that context doesn't actually modify the 
> original Templates object since the parameter values don't seem to be 
> set in the output.
>
> If I knew that all of the custom XMLFilters would come before the 
> Transforms then I think I could build two separate chains. One of 
> XMLFilters that I use with a SAXSource and another of 
> TransformHandlers that I terminate with a Result. I don't want to make 
> that ordering assumption though.
>
> I've tried to find a way to operate entirely using TransformHandlers, 
> but I can't figure out how to convert an XMLFilter into one.
>
> Are there any suggestions for a method of accomplishing the goal of 
> having a chain of XMLFilters which include XSLT stylesheets with 
> parameters specified?
>
> Will