org.milyn.cdr
Class SmooksResourceConfiguration

java.lang.Object
  extended by org.milyn.cdr.SmooksResourceConfiguration

public class SmooksResourceConfiguration
extends Object

Smooks Resource Targeting Configuration.

A Content Delivery Resource is anything that can be used by Smooks in the process of analysing or transforming a data stream. They could be pieces of Java logic (DOMElementVisitor, SerializationUnit), some text or script resource, or perhaps simply a configuration parameter (see ParameterAccessor).

What is Resource Targeting?

Smooks works by "targeting" resources at message transformation/analysis processes. It targets resources at message profiles, and then message fragments (or other aspects of the transformation/analysis process) within that message profile. This typically means targeting a piece of tranformation logic (XSLT, Java, Groovy etc) at a specific type of message ("base profile"), and then at a specific fragment of that message. The fragment may include as much or as little of the document as required. Smooks also allows you to target multilpe resources at the same fragment (see SmooksResourceConfigurationSortComparator).

Note you don't have to use message profiling. You can simply create a set of configurations that are only targeted at message fragments (no profiling info), supply them to a Smooks instance and then use an ExecutionContext instance that's not based on a profile (see Smooks.createExecutionContext()). This is definitely the easiest way to start using Smooks.

Resource Targeting Configurations

Smooks can be manually configured (through code), but the easiest way of working is through XML. The follwoing are a few sample configurations. Explanations follow the samples.

A basic sample. Note that it is not using any profiling. The resource-config element maps directly to an instance of this class.

 <?xml version='1.0'?>
 <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
      <resource-config selector="order/order-header">
          <resource type="xsl">/com/acme/transform/OrderHeaderTransformer.xsl</resource>
      </resource-config>
      <resource-config selector="order-items/order-item">
          <resource>com.acme.transform.MyJavaOrderItemTransformer</resource>
      </resource-config>
 </smooks-resource-list>

A more complex sample, using profiling. So resource 1 is targeted at both "message-exchange-1" and "message-exchange-2", whereas resource 2 is only targeted at "message-exchange-1" and resource 3 at "message-exchange-2" (see Smooks.createExecutionContext(String)).

 <?xml version='1.0'?>
 <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
      <profiles>
          <profile base-profile="message-exchange-1" sub-profiles="message-producer-A, message-consumer-B" />
          <profile base-profile="message-exchange-2" sub-profiles="message-producer-A, message-consumer-C" />
      </profiles>
 (1)  <resource-config selector="order/order-header" target-profile="message-producer-A">
          <resource>com.acme.transform.AddIdentityInfo</resource>
      </resource-config>
 (2)  <resource-config selector="order-items/order-item" target-profile="message-consumer-B">
          <resource>com.acme.transform.MyJavaOrderItemTransformer</resource>
          <param name="execution-param-X">param-value-forB</param>
      </resource-config>
 (3)  <resource-config selector="order-items/order-item" target-profile="message-consumer-C">
          <resource>com.acme.transform.MyJavaOrderItemTransformer</resource>
          <param name="execution-param-X">param-value-forC</param>
      </resource-config>
 </smooks-resource-list>

Attribute Definitions

Resource Targeting Configurations

Author:
tom.fennelly@gmail.com
See Also:
SmooksResourceConfigurationSortComparator

Field Summary
static String DOCUMENT_FRAGMENT_SELECTOR
          A special selector for resource targeted at the document as a whole (the roor element).
static String DOCUMENT_VOID_SELECTOR
          A special selector for resource targeted at the document as a whole (the roor element).
static String LEGACY_DOCUMENT_FRAGMENT_SELECTOR
           
static String PARAM_RESDATA
          Deprecated. Resource now specified on <resource> element. Since Configuration DTD v2.0.
static String PARAM_RESTYPE
          Deprecated. Resource type now specified on "type" attribute of <resource> element. Since Configuration DTD v2.0.
static String XML_DEF_PREFIX
          XML selector type definition prefix
 
Constructor Summary
SmooksResourceConfiguration()
          Public default constructor.
SmooksResourceConfiguration(String selector)
          Public constructor.
SmooksResourceConfiguration(String selector, String resource)
          Public constructor.
SmooksResourceConfiguration(String selector, String targetProfile, String resource)
          Public constructor.
SmooksResourceConfiguration(String selector, String selectorNamespaceURI, String targetProfile, String resource)
          Public constructor.
 
Method Summary
 void addParmeters(SmooksResourceConfiguration config)
           
 void attachGlobalParameters(ApplicationContext appContext)
           
 Object clone()
          Perform a shallow clone of this configuration.
static String extractTargetAttribute(String[] selectorTokens)
           
 boolean getBoolParameter(String name, boolean defaultVal)
          Get the named SmooksResourceConfiguration parameter as a boolean.
 byte[] getBytes()
          Get the resource as a byte array.
 ExpressionEvaluator getConditionEvaluator()
          Get the condition evaluator used in targeting of this resource.
 String[] getContextualSelector()
          Get the contextual selector definition for this SmooksResourceConfiguration.
 Parameter getParameter(String name)
          Get the named SmooksResourceConfiguration parameter.
 int getParameterCount()
          Get the SmooksResourceConfiguration parameter count.
 List getParameterList()
          Get all parameter values set on this configuration.
 Map<String,Object> getParameters()
          Get the param map associated with this configuration.
 List getParameters(String name)
          Get the named SmooksResourceConfiguration parameter List.
 ProfileTargetingExpression[] getProfileTargetingExpressions()
          Get the profile targeting expressions for this SmooksResourceConfiguration.
 String getResource()
          Get the resource for this SmooksResourceConfiguration.
 String getResourceType()
          Get the resource "type" for this resource.
 String getSelector()
          Get the selector definition for this SmooksResourceConfiguration.
 String getSelectorNamespaceURI()
          The the selector namespace URI.
 String getStringParameter(String name)
          Get the named SmooksResourceConfiguration parameter.
 String getStringParameter(String name, String defaultVal)
          Get the named SmooksResourceConfiguration parameter.
 String getTargetAttribute()
          Get the name of the attribute specified on the selector, if one was specified.
 String getTargetElement()
          Get the name of the target element where the selector is targeting the resource at an XML element.
 String getTargetProfile()
          Get the target profile string as set in the configuration.
 boolean isDefaultResource()
          Is this resource config a default applied resource.
 boolean isInline()
          Is this resource defined inline in the configuration, or is it referenced through a URI.
 boolean isJavaContentHandler()
          Is this resource a Java ContentHandler resource.
 boolean isJavaResource()
          Does this resource configuration refer to a Java Class resource.
 boolean isSelectorContextual()
          Is the resource selector contextual.
 boolean isTargetedAtElement(Element element)
          Is this configuration targeted at the supplied DOM element.
 boolean isTargetedAtElement(SAXElement element)
          Is this configuration targeted at the supplied SAX element.
 boolean isTargetedAtElementContext(Element element)
          Is this resource configuration targeted at the specified DOM element in context.
 boolean isTargetedAtElementContext(SAXElement element)
          Is this resource configuration targeted at the specified SAX element in context.
 boolean isTargetedAtNamespace(String namespace)
          Is this resource configuration targets at the same namespace as the specified elemnt.
 boolean isXmlDef()
          Is this selector defininition an XML based definition.
 SmooksResourceConfiguration merge(SmooksResourceConfiguration config)
           
static String[] parseSelector(String selector)
           
 void removeParameter(String name)
          Remove the named parameter.
 void setConditionEvaluator(ExpressionEvaluator expressionEvaluator)
          Set the condition evaluator to be used in targeting of this resource.
 void setDefaultResource(boolean defaultResource)
          Set this resource config as a default applied resource.
 void setParameter(Parameter parameter)
           
 Parameter setParameter(String name, String value)
          Set the named SmooksResourceConfiguration parameter value (default type - String).
 Parameter setParameter(String name, String type, String value)
          Set the named SmooksResourceConfiguration parameter value (with type).
 void setResource(String resource)
          Set the configs "resource".
 void setResourceType(String resourceType)
          Explicitly set the resource type.
 void setSelector(String selector)
          Set the config selector.
 void setSelectorNamespaceURI(String namespaceURI)
          Set the namespace URI to which the selector is associated.
 void setTargetProfile(String targetProfile)
          Set the configs "target profile".
 Class toJavaResource()
          Returns the resource as a Java Class instance.
 Properties toProperties()
          Create a Properties instance from this supplied SmooksResourceConfiguration
 String toString()
           
 String toXML()
          Generate an XML'ified description of this resource.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PARAM_RESTYPE

public static final String PARAM_RESTYPE
Deprecated. Resource type now specified on "type" attribute of <resource> element. Since Configuration DTD v2.0.
The resource type can be specified as a resource parameter. This constant defines that parameter name.

See Also:
Constant Field Values

PARAM_RESDATA

public static final String PARAM_RESDATA
Deprecated. Resource now specified on <resource> element. Since Configuration DTD v2.0.
The resource data can be specified as a resource parameter. This constant defines that parameter name.

See Also:
Constant Field Values

XML_DEF_PREFIX

public static final String XML_DEF_PREFIX
XML selector type definition prefix


DOCUMENT_FRAGMENT_SELECTOR

public static final String DOCUMENT_FRAGMENT_SELECTOR
A special selector for resource targeted at the document as a whole (the roor element).

See Also:
Constant Field Values

LEGACY_DOCUMENT_FRAGMENT_SELECTOR

public static final String LEGACY_DOCUMENT_FRAGMENT_SELECTOR
See Also:
Constant Field Values

DOCUMENT_VOID_SELECTOR

public static final String DOCUMENT_VOID_SELECTOR
A special selector for resource targeted at the document as a whole (the roor element).

See Also:
Constant Field Values
Constructor Detail

SmooksResourceConfiguration

public SmooksResourceConfiguration()
Public default constructor.

See Also:
setSelector(String), setSelectorNamespaceURI(String), setTargetProfile(String), setResource(String), setResourceType(String), setParameter(String, String)

SmooksResourceConfiguration

public SmooksResourceConfiguration(String selector)
Public constructor.

Parameters:
selector - The selector definition.
See Also:
setSelectorNamespaceURI(String), setTargetProfile(String), setResource(String), setResourceType(String), setParameter(String, String)

SmooksResourceConfiguration

public SmooksResourceConfiguration(String selector,
                                   String resource)
Public constructor.

Parameters:
selector - The selector definition.
resource - The resource.
See Also:
setSelectorNamespaceURI(String), setTargetProfile(String), setResourceType(String), setParameter(String, String)

SmooksResourceConfiguration

public SmooksResourceConfiguration(String selector,
                                   String targetProfile,
                                   String resource)
Public constructor.

Parameters:
selector - The selector definition.
targetProfile - Target Profile(s). Comma separated list of ProfileTargetingExpressions.
resource - The resource.
See Also:
setSelectorNamespaceURI(String), setResourceType(String), setParameter(String, String)

SmooksResourceConfiguration

public SmooksResourceConfiguration(String selector,
                                   String selectorNamespaceURI,
                                   String targetProfile,
                                   String resource)
Public constructor.

Parameters:
selector - The selector definition.
selectorNamespaceURI - The selector namespace URI.
targetProfile - Target Profile(s). Comma separated list of ProfileTargetingExpressions.
resource - The resource.
See Also:
setResourceType(String), setParameter(String, String)
Method Detail

clone

public Object clone()
Perform a shallow clone of this configuration.

Overrides:
clone in class Object
Returns:
Configuration clone.

attachGlobalParameters

public void attachGlobalParameters(ApplicationContext appContext)

merge

public SmooksResourceConfiguration merge(SmooksResourceConfiguration config)

addParmeters

public void addParmeters(SmooksResourceConfiguration config)

setSelector

public void setSelector(String selector)
Set the config selector.

Parameters:
selector - The selector definition.

parseSelector

public static String[] parseSelector(String selector)

setSelectorNamespaceURI

public void setSelectorNamespaceURI(String namespaceURI)
Set the namespace URI to which the selector is associated.

Parameters:
namespaceURI - Selector namespace.

setResource

public void setResource(String resource)
Set the configs "resource".

Parameters:
resource - The resource.

isInline

public boolean isInline()
Is this resource defined inline in the configuration, or is it referenced through a URI.

Note that this method also returns false if the resource is undefined (null).

Returns:
True if the resource is defined inline, otherwise false.

getTargetProfile

public String getTargetProfile()
Get the target profile string as set in the configuration.

Returns:
The target profile.

setTargetProfile

public void setTargetProfile(String targetProfile)
Set the configs "target profile".

Parameters:
targetProfile - Target Profile(s). Comma separated list of ProfileTargetingExpressions.

setResourceType

public void setResourceType(String resourceType)
Explicitly set the resource type.

E.g. "class", "xsl", "groovy" etc.

Parameters:
resourceType - The resource type.

getSelector

public String getSelector()
Get the selector definition for this SmooksResourceConfiguration.

Returns:
The selector definition.

getContextualSelector

public String[] getContextualSelector()
Get the contextual selector definition for this SmooksResourceConfiguration.

See details about the "selector" attribute in the Attribute Definitions section.

Returns:
The contxtual selector definition.

getTargetElement

public String getTargetElement()
Get the name of the target element where the selector is targeting the resource at an XML element.

Accomodates the fact that element based selectors can be contextual. This method is not relevant where the selector is not targeting an XML element.

See details about the "selector" attribute in the Attribute Definitions section.

Returns:
The target XML element name.

getTargetAttribute

public String getTargetAttribute()
Get the name of the attribute specified on the selector, if one was specified.

Returns:
An attribute name, if one was specified on the selector, otherwise null.

getSelectorNamespaceURI

public String getSelectorNamespaceURI()
The the selector namespace URI.

Returns:
The XML namespace URI of the element to which this configuration applies, or null if not namespaced.

getProfileTargetingExpressions

public ProfileTargetingExpression[] getProfileTargetingExpressions()
Get the profile targeting expressions for this SmooksResourceConfiguration.

Returns:
The profile targeting expressions.

getResource

public String getResource()
Get the resource for this SmooksResourceConfiguration.

Returns:
The cdrar path.

setConditionEvaluator

public void setConditionEvaluator(ExpressionEvaluator expressionEvaluator)
Set the condition evaluator to be used in targeting of this resource.

Parameters:
expressionEvaluator - The ExpressionEvaluator, or null if no condition is to be used.

getConditionEvaluator

public ExpressionEvaluator getConditionEvaluator()
Get the condition evaluator used in targeting of this resource.

Returns:
The ExpressionEvaluator, or null if no condition is specified.

isDefaultResource

public boolean isDefaultResource()
Is this resource config a default applied resource.

Some resources (e.g. DefaultSerializationUnit or DefaultSAXElementSerializer) are applied by default when no other resources are targeted at an element.

Returns:
True if this is a default applied resource, otherwise false.

setDefaultResource

public void setDefaultResource(boolean defaultResource)
Set this resource config as a default applied resource.

Some resources (e.g. DefaultSerializationUnit or DefaultSAXElementSerializer) are applied by default when no other resources are targeted at an element.

Parameters:
defaultResource - True if this is a default applied resource, otherwise false.

getResourceType

public String getResourceType()
Get the resource "type" for this resource.

Determines the type through the following checks (in order):

  1. Is it a Java resource. See isJavaResource(). If it is, return "class".
  2. Is the "restype" resource paramater specified. If it is, return it's value. Ala DTD v1.0
  3. Is the resource type explicitly set on this configuration. If it is, return it's value. Ala the "type" attribute on the resource element on DTD v2.0
  4. Return the resource path file extension e.g. "xsl".

Returns:

setParameter

public Parameter setParameter(String name,
                              String value)
Set the named SmooksResourceConfiguration parameter value (default type - String).

Overwrites previous value of the same name.

Parameters:
name - Parameter name.
value - Parameter value.
Returns:
The parameter instance.

setParameter

public Parameter setParameter(String name,
                              String type,
                              String value)
Set the named SmooksResourceConfiguration parameter value (with type).

Overwrites previous value of the same name.

Parameters:
name - Parameter name.
type - Parameter type.
value - Parameter value.
Returns:
The parameter instance.

setParameter

public void setParameter(Parameter parameter)

getParameter

public Parameter getParameter(String name)
Get the named SmooksResourceConfiguration parameter.

If there is more than one of the named parameters defined, the first defined value is returned.

Parameters:
name - Name of parameter to get.
Returns:
Parameter value, or null if not set.

getParameters

public Map<String,Object> getParameters()
Get the param map associated with this configuration.

Returns:
The configuration parameters. The Map value is either a Parameter or parameter list (List<Parameter>).

getParameterList

public List getParameterList()
Get all parameter values set on this configuration.

Returns:
Parameter value List, or null if not set.

getParameters

public List getParameters(String name)
Get the named SmooksResourceConfiguration parameter List.

Parameters:
name - Name of parameter to get.
Returns:
Parameter value List, or null if not set.

getStringParameter

public String getStringParameter(String name)
Get the named SmooksResourceConfiguration parameter.

Parameters:
name - Name of parameter to get.
Returns:
Parameter value, or null if not set.

getStringParameter

public String getStringParameter(String name,
                                 String defaultVal)
Get the named SmooksResourceConfiguration parameter.

Parameters:
name - Name of parameter to get.
defaultVal - The default value to be returned if there are no parameters on the this SmooksResourceConfiguration instance, or the parameter is not defined.
Returns:
Parameter value, or defaultVal if not defined.

getBoolParameter

public boolean getBoolParameter(String name,
                                boolean defaultVal)
Get the named SmooksResourceConfiguration parameter as a boolean.

Parameters:
name - Name of parameter to get.
defaultVal - The default value to be returned if there are no parameters on the this SmooksResourceConfiguration instance, or the parameter is not defined.
Returns:
true if the parameter is set to true, defaultVal if not defined, otherwise false.

getParameterCount

public int getParameterCount()
Get the SmooksResourceConfiguration parameter count.

Returns:
Number of parameters defined on this SmooksResourceConfiguration.

removeParameter

public void removeParameter(String name)
Remove the named parameter.

Parameters:
name - The name of the parameter to be removed.

isXmlDef

public boolean isXmlDef()
Is this selector defininition an XML based definition.

I.e. is the selector attribute value prefixed with "xmldef:".

Returns:
True if this selector defininition is an XML based definition, otherwise false.

toString

public String toString()
Overrides:
toString in class Object

getBytes

public byte[] getBytes()
Get the resource as a byte array.

If the resource data is not inlined in the configuration (in a "resdata" param), it will be resolved using the URIResourceLocator i.e. the path will be enterpretted as a URI. If the resource doesn't resolve to a stream producing URI, the resource string will be converted to bytes and returned.

Returns:
The resource as a byte array, or null if resource path is null or the resource doesn't exist.

toJavaResource

public Class toJavaResource()
Returns the resource as a Java Class instance.

Returns:
The Java Class instance refered to be this resource configuration, or null if the resource doesn't refer to a Java Class.

isJavaResource

public boolean isJavaResource()
Does this resource configuration refer to a Java Class resource.

Returns:
True if this resource configuration refers to a Java Class resource, otherwise false.

isJavaContentHandler

public boolean isJavaContentHandler()
Is this resource a Java ContentHandler resource.

Returns:
True if this resource refers to an instance of the ContentHandler class, otherwise false.

isTargetedAtNamespace

public boolean isTargetedAtNamespace(String namespace)
Is this resource configuration targets at the same namespace as the specified elemnt.

Parameters:
namespace - The element to check against.
Returns:
True if this resource config is targeted at the element namespace, or if the resource is not targeted at any namespace (i.e. not specified), otherwise false.

isSelectorContextual

public boolean isSelectorContextual()
Is the resource selector contextual.

See details about the "selector" attribute in the Attribute Definitions section.

Returns:
True if the selector is contextual, otherwise false.

isTargetedAtElementContext

public boolean isTargetedAtElementContext(Element element)
Is this resource configuration targeted at the specified DOM element in context.

See details about the "selector" attribute in the Attribute Definitions section.

Note this doesn't perform any namespace checking.

Parameters:
element - The element to check against.
Returns:
True if this resource configuration is targeted at the specified element in context, otherwise false.

isTargetedAtElementContext

public boolean isTargetedAtElementContext(SAXElement element)
Is this resource configuration targeted at the specified SAX element in context.

See details about the "selector" attribute in the Attribute Definitions section.

Note this doesn't perform any namespace checking.

Parameters:
element - The element to check against.
Returns:
True if this resource configuration is targeted at the specified element in context, otherwise false.

isTargetedAtElement

public boolean isTargetedAtElement(Element element)
Is this configuration targeted at the supplied DOM element.

Checks that the element is in the correct namespace and is a contextual match for the configuration.

Parameters:
element - The element to be checked.
Returns:
True if this configuration is targeted at the supplied element, otherwise false.

isTargetedAtElement

public boolean isTargetedAtElement(SAXElement element)
Is this configuration targeted at the supplied SAX element.

Checks that the element is in the correct namespace and is a contextual match for the configuration.

Parameters:
element - The element to be checked.
Returns:
True if this configuration is targeted at the supplied element, otherwise false.

toXML

public String toXML()
Generate an XML'ified description of this resource.

Returns:
XML'ified description of the resource.

toProperties

public Properties toProperties()
Create a Properties instance from this supplied SmooksResourceConfiguration

Returns:
The resource parameters as a Properties instance.

extractTargetAttribute

public static String extractTargetAttribute(String[] selectorTokens)


Copyright © 2009. All Rights Reserved.