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>com.acme.transform.MyJavaOrderItemTransformer</resource>
         </resource-config>
     </smooks-resource-list>
     
    The resource-config XML element maps to an instance of ResourceConfig. The first resource-config declares a resource of type XSL. The text content of the resource 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 second resource-config does not have a type. Without a type, Smooks will assume that the resource-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 first resource-config declares a resource that targets both message-exchange-1 and message-exchange-2 profiles. The second resource-config declares a resource that targets message-exchange-1 profile while the third resource-config declares a resource targeting message-exchange-2 profile (see Smooks.createExecutionContext(String)). It is worth noting that both the second and third resource configs inject parameters into the resources with the param element.

    XML attributes

    1. target-profile: a list of one or more ProfileTargetingExpression profile targeting expressions (supports wildcards "*").

    2. 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:
      1. 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 for Visitor type resources.
      2. "#document" is a special selector that targets a resource on the whole document.
      3. Targeting a specific SmooksXMLReader at a specific profile.
    • 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 this ResourceConfig.
        Returns:
        clone of this >ResourceConfig
      • addParameters

        void addParameters​(ResourceConfig resourceConfig)
        Shallow copies parameters from another ResourceConfig and adds them to this ResourceConfig.
        Parameters:
        resourceConfig - ResourceConfig to copy parameters from
      • setSelector

        void setSelector​(String selector,
                         Properties namespaces)
        Sets the raw selector of this ResourceConfig.
        Parameters:
        selector - an expression that defines the target of this resource. Any namespaces must be bound in the namespaces 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 this ResourceConfig. 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 this ResourceConfig 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, otherwise false
        See Also:
        setResource(String)
      • getProfile

        String getProfile()
        Gets the target profile/s of this ResourceConfig.
        Returns:
        target profile/s
        See Also:
        setProfile(String)
      • setResourceType

        void setResourceType​(String resourceType)
        Sets the resource type (e.g., "class", "xsl", "groovy", etc...)
        Parameters:
        resourceType - resource type
        See Also:
        getResourceType(), setResource(String)
      • getProfileTargetingExpressions

        ProfileTargetingExpression[] getProfileTargetingExpressions()
        Gets the profile targeting expressions of this ResourceConfig.
        Returns:
        profile targeting expressions
      • getResource

        String getResource()
        Get the resource for this ResourceConfig.
        Returns:
        resource
        See Also:
        setResource(String)
      • isSystem

        boolean isSystem()
        Evaluates whether this ResourceConfig 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 this ResourceConfig 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 this ResourceConfig is for a default resource
      • getResourceType

        String getResourceType()
        Get the resource type of this ResourceConfig.

        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 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
        3. Return the resource path file extension e.g. "xsl".
        Returns:
        resource type
        See Also:
        getResourceType()
      • setParameter

        <T> Parameter<T> setParameter​(String name,
                                      T value)
        Adds a parameter to this ResourceConfig. Multiple parameters with the same names can coexist.
        Parameters:
        name - parameter name
        value - 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 this ResourceConfig. Multiple parameters with the same names can coexist.
        Parameters:
        name - parameter name
        type - parameter type
        value - parameter value
        Returns:
        new parameter added to this ResourceConfig
      • setParameter

        <T> void setParameter​(Parameter<T> parameter)
        Adds a parameter to this ResourceConfig. Multiple parameters with the same names can coexist.
        Parameters:
        parameter - parameter to add to this ResourceConfig
      • getParameter

        <T> Parameter<T> getParameter​(String name,
                                      Class<T> valueClass)
        Gets a parameter by name from this ResourceConfig. 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 this ResourceConfig as a Map.
        Returns:
        parameters represented as a Map where the key is the parameter name and the value is either a parameter or a list of parameters. The value is a list when more than one parameter has the same name
      • getParameterValues

        List<?> getParameterValues()
        Gets all parameter values set of this ResourceConfig.
        Returns:
        list of all the parameter values
      • getParameters

        List<Parameter<?>> getParameters​(String name)
        Gets all the parameters of this ResourceConfig by name.
        Parameters:
        name - name of parameter/s to get
        Returns:
        list of all the parameters that match the given name, or null if not set
      • getParameterValue

        Object getParameterValue​(String name)
      • getParameterValue

        <T> T getParameterValue​(String name,
                                Class<T> valueClass)
        Get the named parameter from this ResourceConfig.
        Parameters:
        name - name of parameter to get from this ResourceConfig
        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 this ResourceConfig.
        Parameters:
        name - name of parameter to get.
        valueClass - expected value type of the parameter to get
        defaultValue - default value to be returned if there are no parameters in this ResourceConfig or if the parameter is not defined
        Returns:
        parameter value, or defaultValue if not defined
      • getParameterCount

        int getParameterCount()
        Get the parameter count of this ResourceConfig.
        Returns:
        no. of parameters in this ResourceConfig
      • removeParameter

        void removeParameter​(String name)
        Remove the named parameter this ResourceConfig
        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 the URIResourceLocator. That is, the path will be interpreted as a URI. 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 is null or the resource does not exist
      • isJavaResource

        boolean isJavaResource()
        Evaluates whether this ResourceConfig object references a Java class.
        Returns:
        true if this resource configuration refers to a Java Class resource, otherwise false
      • toXml

        String toXml()
        Generates an XML'ified description of this resource.
        Returns:
        XML'ified description of the resource