All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.templating.xslt.XslContentHandlerFactory Maven / Gradle / Ivy

The newest version!
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU Lesser General Public License for more details:
	http://www.gnu.org/licenses/lgpl.txt
*/

package org.milyn.templating.xslt;

import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.annotation.AppContext;
import org.milyn.cdr.annotation.Configurator;
import org.milyn.container.ApplicationContext;
import org.milyn.delivery.ContentHandler;
import org.milyn.delivery.ContentHandlerFactory;
import org.milyn.delivery.annotation.Resource;
import org.milyn.javabean.context.BeanContext;


/**
 * XSL {@link org.milyn.delivery.dom.DOMElementVisitor} Creator class.
 *
 * Creates {@link org.milyn.delivery.dom.DOMElementVisitor} instances for performing node/element level
 * XSL templating (aka XSLT).
 *
 * Template application can be done in a synchronized or unsynchronized fashion by setting
 * the system property "org.milyn.templating.xslt.synchronized".  According to the spec,
 * this should not be necessary.  However, Xalan 2.7.0 (for one) has a bug which results in
 * unsynchronized template application causing invalid transforms.
 *
 * 

Targeting "xsl" Templates

* The following is the basic configuration specification for XSL resources: *
 * <resource-config selector="target-element">
 *     <resource>XSL Resource - Inline or {@link org.milyn.resource.URIResourceLocator URI}</resource>
 *
 *     <!-- (Optional) The action to be applied on the template content. Should the content
 *          generated by the template:
 *          1. replace ("replace") the target element, or
 *          2. be added to ("addto") the target element, or
 *          3. be inserted before ("insertbefore") the target element, or
 *          4. be inserted after ("insertafter") the target element.
 *          5. be bound to ("bindto") a {@link BeanContext} variable named by the "bindId" param.
 *          Default "replace".-->
 *     <param name="action">replace/addto/insertbefore/insertafter/bindto</param>
 *
 *     <!-- (Optional) Is this XSL template resource a complete XSL template, or just a "Templatelet".
 *          Only relevant for inlined XSL resources.  URI based resource are always assumed to NOT be templatelets.
 *          Default "false" (for inline resources).-->
 *     <param name="is-xslt-templatelet">true/false</param>
 *
 *     <!-- (Optional) Should the template be applied before (true) or
 *             after (false) Smooks visits the child elements of the target element.
 *             Default "false".-->
 *     <param name="applyTemplateBefore">true/false</param>
 *
 *     <!-- (Optional) The name of the {@link org.milyn.io.AbstractOutputStreamResource OutputStreamResource}
 *             to which the result should be written. If set, the "action" param is ignored. -->
 *     <param name="outputStreamResource">xyzResource</param>
 *
 *     <!-- (Optional) Template encoding.
 *          Default "UTF-8".-->
 *     <param name="encoding">encoding</param>
 *
 *     <!-- (Optional) bindId when "action" is "bindto".
 *     <param name="bindId">xxxx</param>
 *
 *     <!-- (Optional) Fail on XSL Transformer Warning.
 *          Default "true".-->
 *     <param name="failOnWarning">false</param> <!-- Default "true" -->
 *
 * </resource-config>
 * 
* * Example - URI based XSLT spec: *
 * <resource-config selector="target-element">
 *     <!-- 1. See {@link org.milyn.resource.URIResourceLocator} -->
 *     <resource>/com/acme/order-transform.xsl</resource>
 * </resource-config>
 * 
* * Example - Inlined XSLT spec: *
 * <resource-config selector="target-element">
 *     <!-- 1. Note how we have to specify the resource type when it's inlined. -->
 *     <!-- 2. Note how the inlined XSLT is wrapped as an XML Comment. CDATA Section wrapping also works. -->
 *     <!-- 3. Note if the inlined XSLT is a templatelet, is-xslt-templatelet=true must be specified. -->
 *     <resource type="xsl">
 *         <!--
 *            Inline XSLT....
 *         -->
 *     </resource>
 *     <param name="is-xslt-templatelet">true</param>
 * </resource-config>
 * 
* *

Templatelets

* Templatelets are a convenient way of specifying an XSL Stylesheet. When using "Templatelets", you simply specify the * body of an XSL template. This creator then wraps that body to make a complete XSL Stylesheet with a single template matched to the * element targeted by the Smooks resource configuration in question. This feature only applies * to inlined XSL resources and in this case, it's OFF by default. To use this feature, * you must specify the "is-xslt-templatelet" parameter with a value of "true". * * This feature will not work in all situations since you'll often need to specify a full stylesheet in order to * specify namespaces etc. It's just here for convenience. * * See the template used to wrap the templatelet. * *

JavaBean Support

* Support for injection of JavaBean values populated by the * Smooks JavaBean Cartridge is supported through the * Xalan extension {@link org.milyn.templating.xslt.XalanJavabeanExtension}. * * @author [email protected] */ @Resource(type = "xsl") public class XslContentHandlerFactory implements ContentHandlerFactory { /** * Parameter name for templating feature. */ public static final String IS_XSLT_TEMPLATELET = "is-xslt-templatelet"; /** * Synchonized template application system property key. */ public static final String ORG_MILYN_TEMPLATING_XSLT_SYNCHRONIZED = "org.milyn.templating.xslt.synchronized"; @AppContext private ApplicationContext applicationContext; /** * Create an XSL based ContentHandler instance ie from an XSL byte streamResult. * * @param resourceConfig The SmooksResourceConfiguration for the XSL {@link org.milyn.delivery.ContentHandler} * to be created. * @return XSL {@link org.milyn.delivery.ContentHandler} instance. * @see org.milyn.delivery.JavaContentHandlerFactory */ public synchronized ContentHandler create(SmooksResourceConfiguration resourceConfig) throws SmooksConfigurationException, InstantiationException { try { return Configurator.configure(new XslTemplateProcessor(), resourceConfig, applicationContext); } catch(SmooksConfigurationException e) { throw e; } catch (Exception e) { InstantiationException instanceException = new InstantiationException("XSL ProcessingUnit resource [" + resourceConfig.getResource() + "] not loadable."); instanceException.initCause(e); throw instanceException; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy