Package org.smooks.api.resource.config
Interface ResourceConfig
-
- All Known Implementing Classes:
DefaultResourceConfig
public interface ResourceConfigConfiguration of a Smooks resource.
ResourceConfigdrives the behaviour of a Smooks resource. It defines:- which event/s from the event stream target the resource
- the parameters injected into the resource
- the type of resource that is instantiated
A
ResourceConfigcan be assembled with Java code but it is more convenient to configure one through XML. The following are some resource configurations described in XML.
Basic Sample
<?xml version='1.0'?> <smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"> (1) <resource-config selector="order/order-header"> <resource type="xsl">/com/acme/transform/OrderHeaderTransformer.xsl</resource> </resource-config> (2) <resource-config selector="order-items/order-item"> <resource>
Thecom.acme.transform.MyJavaOrderItemTransformer</resource> </resource-config> </smooks-resource-list>resource-configXML element maps to an instance ofResourceConfig. The firstresource-configdeclares a resource of type XSL. The text content of theresourceelement references the resource itself which could be a path, an identifier, etc.... In this case, the resource is a path to an XSL script. The resource will be executed when the selector "order/order-header" evaluates to true.
The secondresource-configdoes not have a type. Without a type, Smooks will assume that theresource-configis for a Java resource. The resource itself is a qualified class name. If the resource is valid (e.g., the class exists), the instantiated class will be invoked when the selector "order-items/order-item" evaluates to true.
More Complex Sample with Profiling
<?xml version='1.0'?> <smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.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>
The firstresource-configdeclares a resource that targets bothmessage-exchange-1andmessage-exchange-2profiles. The secondresource-configdeclares a resource that targetsmessage-exchange-1profile while the thirdresource-configdeclares a resource targetingmessage-exchange-2profile (seeSmooks.createExecutionContext(String)). It is worth noting that both the second and third resource configs inject parameters into the resources with theparamelement.
XML attributes
-
target-profile: a list of one or more
ProfileTargetingExpressionprofile targeting expressions (supports wildcards "*"). -
selector: a string specifying when the resource should be activated. The selector
is used by the Smooks engine to look-up a resource config. This is typically the name of a fragment (partial XPath support).
This attribute supports a list of comma-separated selectors, allowing you to target a single resource with multiple
selectors (e.g. fragments). When the resource is a
Visitorimplementation, the selector is treated as an XPath expression (full XPath spec not supported), otherwise the selector value is treated as an opaque value.
Example selectors:-
For a
Visitor, use the target fragment name e.g. "order", "address", "address/name", "item[2]/price[text() = 99.99]" etc. Also supports wildcard based fragment selection (i.e., "*"). See the User Guide for more details on setting selectors forVisitortype resources. - "#document" is a special selector that targets a resource on the whole document.
-
Targeting a specific
SmooksXMLReaderat a specific profile.
-
For a
-
-
Field Summary
Fields Modifier and Type Field Description static StringDOCUMENT_FRAGMENT_SELECTORA special selector for resource that targets the document as a whole (i.e., the root element).static StringSELECTOR_NONEA special selector for a resource that does not target any event.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)Adds the specified change listener to the list of change listeners.voidaddParameters(ResourceConfig resourceConfig)Shallow copies parameters from anotherResourceConfigand adds them to thisResourceConfig.ResourceConfigcopy()Performs a shallow clone of thisResourceConfig.byte[]getBytes()Returns the resource in bytes.<T> Parameter<T>getParameter(String name, Class<T> valueClass)Gets aparameterby name from thisResourceConfig.intgetParameterCount()Get the parameter count of thisResourceConfig.Map<String,Object>getParameters()Gets the parameters associated with thisResourceConfigas aMap.List<Parameter<?>>getParameters(String name)Gets all the parameters of thisResourceConfigby name.ObjectgetParameterValue(String name)<T> TgetParameterValue(String name, Class<T> valueClass)Get the named parameter from thisResourceConfig.<T> TgetParameterValue(String name, Class<T> valueClass, T defaultValue)Get value of named parameter from thisResourceConfig.List<?>getParameterValues()Gets allparametervalues set of thisResourceConfig.StringgetProfile()Gets the target profile/s of thisResourceConfig.ProfileTargetingExpression[]getProfileTargetingExpressions()Gets the profile targeting expressions of thisResourceConfig.StringgetResource()Get the resource for thisResourceConfig.StringgetResourceType()Get the resource type of thisResourceConfig.SelectorPathgetSelectorPath()Gets the selector in its parsed form.booleanisInline()Evaluates whether the resource of thisResourceConfigis defined inline or referenced from a URI.booleanisJavaResource()Evaluates whether thisResourceConfigobject references a Java class.booleanisSystem()Evaluates whether thisResourceConfigis for a default resource.voidremoveChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)Removes the specified change listener from the list of change listeners.voidremoveParameter(String name)Remove the named parameter thisResourceConfig<T> Parameter<T>setParameter(String name, String type, T value)Adds a parameter with a specified type to thisResourceConfig.<T> Parameter<T>setParameter(String name, T value)Adds a parameter to thisResourceConfig.<T> voidsetParameter(Parameter<T> parameter)Adds a parameter to thisResourceConfig.voidsetProfile(String profile)Sets the target profile of thisResourceConfig.voidsetResource(String resource)Sets the resource of thisResourceConfig.voidsetResourceType(String resourceType)Sets the resource type (e.g., "class", "xsl", "groovy", etc...)voidsetSelector(String selector, Properties namespaces)Sets the raw selector of thisResourceConfig.voidsetSelectorPath(SelectorPath selectorPath)Sets the selector in its parsed form.voidsetSystem(boolean isSystem)Set thisResourceConfigas a default applied resource.PropertiestoProperties()Creates aPropertiesinstance from the parametersResourceConfigobject.StringtoXml()Generates an XML'ified description of this resource.
-
-
-
Field Detail
-
DOCUMENT_FRAGMENT_SELECTOR
static final String DOCUMENT_FRAGMENT_SELECTOR
A special selector for resource that targets the document as a whole (i.e., the root element). This selector is especially useful when the name of the first event is not known ahead of time.- See Also:
- Constant Field Values
-
SELECTOR_NONE
static final String SELECTOR_NONE
A special selector for a resource that does not target any event. Such a selector is useful when the resource is not actively participating in the event processing (e.g., a resource shared between other resources for looking up values).- See Also:
- Constant Field Values
-
-
Method Detail
-
copy
ResourceConfig copy()
Performs a shallow clone of thisResourceConfig.- Returns:
- clone of this
>ResourceConfig
-
addParameters
void addParameters(ResourceConfig resourceConfig)
Shallow copies parameters from anotherResourceConfigand adds them to thisResourceConfig.- Parameters:
resourceConfig-ResourceConfigto copy parameters from
-
setSelector
void setSelector(String selector, Properties namespaces)
Sets the raw selector of thisResourceConfig.- Parameters:
selector- an expression that defines the target of this resource. Any namespaces must be bound in thenamespacesparameter.namespaces- namespaces bound to prefixes. The property key is the prefix while the value is the namespace.
-
setResource
void setResource(String resource)
Sets the resource of thisResourceConfig. Unless the resource type is set, the resource is assumed to be a Java resource.- Parameters:
resource- an identifier for the resource. This could be anything like a path, a name, etc... The meaning of this value is driven by the resource type.- See Also:
setResourceType(String)
-
isInline
boolean isInline()
Evaluates whether the resource of thisResourceConfigis defined inline or referenced from a URI. Note that this method also returns false if the resource is undefined (null).- Returns:
trueif the resource is defined inline, otherwisefalse- See Also:
setResource(String)
-
getProfile
String getProfile()
Gets the target profile/s of thisResourceConfig.- Returns:
- target profile/s
- See Also:
setProfile(String)
-
setProfile
void setProfile(String profile)
Sets the target profile of thisResourceConfig.- Parameters:
profile- comma-separated list ofProfileTargetingExpressions- See Also:
getProfile()
-
setResourceType
void setResourceType(String resourceType)
Sets the resource type (e.g., "class", "xsl", "groovy", etc...)- Parameters:
resourceType- resource type- See Also:
getResourceType(),setResource(String)
-
setSelectorPath
void setSelectorPath(SelectorPath selectorPath)
Sets the selector in its parsed form.- Parameters:
selectorPath- selector steps- See Also:
setSelector(String, Properties),getSelectorPath()
-
getSelectorPath
SelectorPath getSelectorPath()
Gets the selector in its parsed form.- Returns:
- selector steps
- See Also:
setSelector(String, Properties),getSelectorPath()
-
getProfileTargetingExpressions
ProfileTargetingExpression[] getProfileTargetingExpressions()
Gets the profile targeting expressions of thisResourceConfig.- Returns:
- profile targeting expressions
-
getResource
String getResource()
Get the resource for thisResourceConfig.- Returns:
- resource
- See Also:
setResource(String)
-
isSystem
boolean isSystem()
Evaluates whether thisResourceConfigis for a default resource. System resources (e.g.SystemConsumeSerializerVisitor) are applied by default when no other resource is targeting the element.- Returns:
trueif this is a default applied resource, otherwise false- See Also:
setSystem(boolean)
-
setSystem
void setSystem(boolean isSystem)
Set thisResourceConfigas a default applied resource. System resources (e.g.SystemConsumeSerializerVisitor) are applied by default when no other resource is targeting the element.- Parameters:
isSystem- whether thisResourceConfigis for a default resource
-
getResourceType
String getResourceType()
Get the resource type of thisResourceConfig. Determines the type through the following checks (in order):- Is it a Java resource. See
isJavaResource(). If it is, return "class". - Is the resource type explicitly set on this configuration. If it is, return the value. Ala the "type" attribute on the resource element on DTD v2.0
- Return the resource path file extension e.g. "xsl".
- Returns:
- resource type
- See Also:
getResourceType()
- Is it a Java resource. See
-
setParameter
<T> Parameter<T> setParameter(String name, T value)
Adds a parameter to thisResourceConfig. Multiple parameters with the same names can coexist.- Parameters:
name- parameter namevalue- parameter value- Returns:
- new parameter added to this
ResourceConfig
-
setParameter
<T> Parameter<T> setParameter(String name, String type, T value)
Adds a parameter with a specified type to thisResourceConfig. Multiple parameters with the same names can coexist.- Parameters:
name- parameter nametype- parameter typevalue- parameter value- Returns:
- new parameter added to this
ResourceConfig
-
setParameter
<T> void setParameter(Parameter<T> parameter)
Adds a parameter to thisResourceConfig. Multiple parameters with the same names can coexist.- Parameters:
parameter- parameter to add to thisResourceConfig
-
getParameter
<T> Parameter<T> getParameter(String name, Class<T> valueClass)
Gets aparameterby name from thisResourceConfig. If more than one parameter match the name, the first parameter is returned.- Parameters:
name- name of parameter to get- Returns:
- parameter reference, or null if not parameter does not exist
-
getParameters
Map<String,Object> getParameters()
Gets the parameters associated with thisResourceConfigas aMap.- Returns:
- parameters represented as a
Mapwhere the key is the parameter name and the value is either aparameteror alistofparameters. The value is a list when more than one parameter has the same name
-
getParameterValues
List<?> getParameterValues()
Gets allparametervalues set of thisResourceConfig.
-
getParameters
List<Parameter<?>> getParameters(String name)
Gets all the parameters of thisResourceConfigby name.- Parameters:
name- name of parameter/s to get- Returns:
listof all theparametersthat match the given name, or null if not set
-
getParameterValue
<T> T getParameterValue(String name, Class<T> valueClass)
Get the named parameter from thisResourceConfig.- Parameters:
name- name of parameter to get from thisResourceConfigvalueClass- expected value type of the parameter to get- Returns:
- parameter value, or
nullif not set
-
getParameterValue
<T> T getParameterValue(String name, Class<T> valueClass, T defaultValue)
Get value of named parameter from thisResourceConfig.- Parameters:
name- name of parameter to get.valueClass- expected value type of the parameter to getdefaultValue- default value to be returned if there are no parameters in thisResourceConfigor if the parameter is not defined- Returns:
- parameter value, or
defaultValueif not defined
-
getParameterCount
int getParameterCount()
Get the parameter count of thisResourceConfig.- Returns:
- no. of parameters in this
ResourceConfig
-
removeParameter
void removeParameter(String name)
Remove the named parameter thisResourceConfig- Parameters:
name- name of the parameter to be removed
-
getBytes
byte[] getBytes()
Returns the resource in bytes. If the resource content is not inlined in the configuration, it will be resolved using theURIResourceLocator. That is, the path will be interpreted as aURI. If the resource does not resolve to a stream producing URI, the resource string will be converted to bytes and returned.- Returns:
- resource as a byte array, or
nullif resource path isnullor the resource does not exist
-
isJavaResource
boolean isJavaResource()
Evaluates whether thisResourceConfigobject references a Java class.- Returns:
trueif this resource configuration refers to a Java Class resource, otherwisefalse
-
addChangeListener
void addChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)
Adds the specified change listener to the list of change listeners.- Parameters:
resourceConfigChangeListener-ResourceConfigChangeListenerinstance to add
-
removeChangeListener
void removeChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)
Removes the specified change listener from the list of change listeners.- Parameters:
resourceConfigChangeListener-ResourceConfigChangeListenerinstance to remove
-
toXml
String toXml()
Generates an XML'ified description of this resource.- Returns:
- XML'ified description of the resource
-
toProperties
Properties toProperties()
Creates aPropertiesinstance from the parametersResourceConfigobject.- Returns:
- resource config parameter.
-
-