Package org.smooks.api.resource.config
Interface ResourceConfig
-
- All Known Implementing Classes:
DefaultResourceConfig
public interface ResourceConfig
Configuration of a Smooks resource.
ResourceConfig
drives 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
ResourceConfig
can 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-config
XML element maps to an instance ofResourceConfig
. The firstresource-config
declares a resource of type XSL. The text content of theresource
element 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-config
does not have a type. Without a type, Smooks will assume that theresource-config
is 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-config
declares a resource that targets bothmessage-exchange-1
andmessage-exchange-2
profiles. The secondresource-config
declares a resource that targetsmessage-exchange-1
profile while the thirdresource-config
declares a resource targetingmessage-exchange-2
profile (seeSmooks.createExecutionContext(String)
). It is worth noting that both the second and third resource configs inject parameters into the resources with theparam
element.
XML attributes
-
target-profile: a list of one or more
ProfileTargetingExpression
profile 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
Visitor
implementation, 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 forVisitor
type resources. - "#document" is a special selector that targets a resource on the whole document.
-
Targeting a specific
SmooksXMLReader
at a specific profile.
-
For a
-
-
Field Summary
Fields Modifier and Type Field Description static String
DOCUMENT_FRAGMENT_SELECTOR
A special selector for resource that targets the document as a whole (i.e., the root element).static String
SELECTOR_NONE
A special selector for a resource that does not target any event.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)
Adds the specified change listener to the list of change listeners.void
addParameters(ResourceConfig resourceConfig)
Shallow copies parameters from anotherResourceConfig
and adds them to thisResourceConfig
.ResourceConfig
copy()
Performs a shallow clone of thisResourceConfig
.byte[]
getBytes()
Returns the resource in bytes.<T> Parameter<T>
getParameter(String name, Class<T> valueClass)
Gets aparameter
by name from thisResourceConfig
.int
getParameterCount()
Get the parameter count of thisResourceConfig
.Map<String,Object>
getParameters()
Gets the parameters associated with thisResourceConfig
as aMap
.List<Parameter<?>>
getParameters(String name)
Gets all the parameters of thisResourceConfig
by name.Object
getParameterValue(String name)
<T> T
getParameterValue(String name, Class<T> valueClass)
Get the named parameter from thisResourceConfig
.<T> T
getParameterValue(String name, Class<T> valueClass, T defaultValue)
Get value of named parameter from thisResourceConfig
.List<?>
getParameterValues()
Gets allparameter
values set of thisResourceConfig
.String
getProfile()
Gets the target profile/s of thisResourceConfig
.ProfileTargetingExpression[]
getProfileTargetingExpressions()
Gets the profile targeting expressions of thisResourceConfig
.String
getResource()
Get the resource for thisResourceConfig
.String
getResourceType()
Get the resource type of thisResourceConfig
.SelectorPath
getSelectorPath()
Gets the selector in its parsed form.boolean
isInline()
Evaluates whether the resource of thisResourceConfig
is defined inline or referenced from a URI.boolean
isJavaResource()
Evaluates whether thisResourceConfig
object references a Java class.
boolean
isSystem()
Evaluates whether thisResourceConfig
is for a default resource.void
removeChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)
Removes the specified change listener from the list of change listeners.void
removeParameter(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> void
setParameter(Parameter<T> parameter)
Adds a parameter to thisResourceConfig
.void
setProfile(String profile)
Sets the target profile of thisResourceConfig
.void
setResource(String resource)
Sets the resource of thisResourceConfig
.void
setResourceType(String resourceType)
Sets the resource type (e.g., "class", "xsl", "groovy", etc...)void
setSelector(String selector, Properties namespaces)
Sets the raw selector of thisResourceConfig
.void
setSelectorPath(SelectorPath selectorPath)
Sets the selector in its parsed form.void
setSystem(boolean isSystem)
Set thisResourceConfig
as a default applied resource.Properties
toProperties()
Creates aProperties
instance from the parametersResourceConfig
object.String
toXml()
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 anotherResourceConfig
and adds them to thisResourceConfig
.- Parameters:
resourceConfig
-ResourceConfig
to 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 thenamespaces
parameter.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 thisResourceConfig
is defined inline or referenced from a URI. Note that this method also returns false if the resource is undefined (null).- Returns:
true
if 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 thisResourceConfig
is for a default resource. System resources (e.g.SystemConsumeSerializerVisitor
) are applied by default when no other resource is targeting the element.- Returns:
true
if this is a default applied resource, otherwise false- See Also:
setSystem(boolean)
-
setSystem
void setSystem(boolean isSystem)
Set thisResourceConfig
as a default applied resource. System resources (e.g.SystemConsumeSerializerVisitor
) are applied by default when no other resource is targeting the element.- Parameters:
isSystem
- whether thisResourceConfig
is 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 aparameter
by 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 thisResourceConfig
as aMap
.- Returns:
- parameters represented as a
Map
where the key is the parameter name and the value is either aparameter
or alist
ofparameters
. The value is a list when more than one parameter has the same name
-
getParameterValues
List<?> getParameterValues()
Gets allparameter
values set of thisResourceConfig
.
-
getParameters
List<Parameter<?>> getParameters(String name)
Gets all the parameters of thisResourceConfig
by name.- Parameters:
name
- name of parameter/s to get- Returns:
list
of all theparameters
that 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 thisResourceConfig
valueClass
- expected value type of the parameter to get- Returns:
- parameter value, or
null
if 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 thisResourceConfig
or if the parameter is not defined- Returns:
- parameter value, or
defaultValue
if 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
null
if resource path isnull
or the resource does not exist
-
isJavaResource
boolean isJavaResource()
Evaluates whether thisResourceConfig
object references a Java class.
- Returns:
true
if 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
-ResourceConfigChangeListener
instance to add
-
removeChangeListener
void removeChangeListener(ResourceConfigChangeListener resourceConfigChangeListener)
Removes the specified change listener from the list of change listeners.- Parameters:
resourceConfigChangeListener
-ResourceConfigChangeListener
instance to remove
-
toXml
String toXml()
Generates an XML'ified description of this resource.- Returns:
- XML'ified description of the resource
-
toProperties
Properties toProperties()
Creates aProperties
instance from the parametersResourceConfig
object.- Returns:
- resource config parameter.
-
-