11. Using DPBuddy to Transform DataPower Configuration and XML Files

There is often a need to apply environment-specific modifications to the DataPower configuration. This may include changing ports, removing or adding some configuration elements, or other changes.

DataPower offers the deployment policy-based mechanism which can be executed on the device when importing the configuration. DPBuddy fully supports deployment policies, and also provides additional XML transformation capabilities. DPBuddy supports many transformation actions that can be executed as part of the import, export, modifyConfig or copy tasks. The actions include “add”, “update”, “delete”, “setText”, “replaceText”, “include” and “exclude”. They are explained in detail later in this section.

All actions except “dpInclude”, “dpExclude” and “replaceText” rely on XPath expressions. The actions are applied to all elements or attributes that match the expression.

The simple example below changes the local port of the front-side HTTP handler to the value provided by the ws.port Ant property. The transformation is applied to the “export.xml” file located inside the zip file.

<dp:import file="${wsproxy.zip.file}" >
    <transform>
        <setText xpath="//LocalPort"  value="${ws.port}" />
    </transform>
</dp:import>

See the samples/ant-tasks/transform.xml file located in your DPBuddy distribution or online for more examples.

DPBuddy’s transformation actions have many powerful features:

  • Can be applied to any XML file, not just DataPower configuration files. You can transform any XML file included in import or copy.
  • Support Ant and Groovy/Gradle variables inside XPath or in XML fragments.
  • Reusable: you can define a reusable group of transformation actions, then reference it from any other group.
  • Can be turned on and off depending on a Boolean expression that can reference any Ant property or Groovy/Gradle variable. For example, a transformation action could be triggered only for certain environments.
  • Support Groovy expressions for complex transformations, e.g., you can apply a Groovy/Java method to an existing value.
  • Easy to debug: transformed files are saved locally and also logged using verbose/debug mode.
  • Can be developed locally without having to connect to a DataPower device. Both the import and copy tasks support “transformOnly” mode, which runs transformations but does not connect to the device.

Any transformable file could have Ant/Groovy/Gradle variables references in any text node or in any attribute. DPBuddy’s transformation logic always attempts to resolve Ant/Groovy/Gradle variables after all transformation actions have been applied. The transformation will fail if any of the variables remains unresolved.

In case you have many transformation rules, it might be beneficial to specify the transformations in a separate file and then include this file in your main DataPower build/deploy script.

11.1. transform Type

transform is the container for all transform actions, such as “add”, “setText” and so on. Transformation actions are executed in the order they are specified inside transform.

transform is supported by the following DPBuddy tasks: import, export, copy and modfyConfig.

When used with import, transform could be specified within the transformFiles element. transformFiles determines which files are going to be transformed.

For other tasks, such as copy or modifyConfig, transform is specified within the fileset or dpFileset element, so the transformations are applied to all the files that matched the “include” and “exclude” patterns of the fileset.

transform supports refid and id attributes, similar to Ant’s path element. transform with id can be defined outside of any Ant target. transform can also contain any number of nested transform actions with refid. DPBuddy will first execute actions referenced by nested transforms.

11.1.1. Attributes Common to All Transform Actions

Attribute Description Required
xpath

XPath expression the action will be applied to.

Not applicable to the “dpInclude”, “dpExclude” and “replaceText” actions.

Yes
matchRequired

Fail if xpath doesn’t match any element/attribute.

Defaults to true.

No
if

A Groovy Boolean expression (has to return true or false). The expression has access to all Ant properties as Groovy variables. In addition, there is a special dp variable containing the following properties: url, username and domain. These properties are populated with connection information for the device and domain to which the task executing this transform action is being connected. Note that all Ant property names containing dots are changed into lower camel case. E.g., “dp.env” can be referred to as “dpEnv” in Groovy.

The expression has to evaluate to true in order for the transform action to be executed.

No

11.1.2. setText Transform Action

If the action’s XPath expression matched elements, setText sets the value of the text node of these elements.

If the action’s XPath expression matched attributes, setText sets the value of these attributes.

Attribute Description Required
value Value to set matched elements or attributes to. Yes, unless expression was provided.
expression A Groovy expression-returning String. The expression has access to all Ant properties as Groovy variables. Note that all Ant property names containing dots are changed into lower camel case. E.g., “dp.env” can be referred to as “dpEnv” in Groovy. The expression can also use the special variable currentValue, which is set to the current value of the matched text node or the attribute. Yes, unless value was provided.

11.1.3. replaceText Transform Action

replaceText searches for the provided search string in all attributes and text nodes. If the string is found, it will be replaced with the value specified in the replaceWith attributes.

The search is case-sensitive.

This action does not use XPath, so all attributes and text nodes will be checked.

Attribute Description Required
textToReplace Text to replace. Yes
replaceWith Replacement string. Yes

11.1.4. add and update Transform Actions

add and update add an XML fragment nested within the action tags to the matching elements. In other words, the root element of the XML fragment becomes the child of the matched element. update removes all children of the matching element prior to adding.

add and update cannot be applied to attributes.

11.1.5. delete Transform Actions

delete simply removes all matching elements.

delete cannot be applied to attributes.

11.1.6. dpExclude and dpInclude Transform Actions

These actions don’t use XPath. Instead, they support regular expressions matching DataPower configuration classes and objects.

dpExclude removes all configuration objects matching class names/object names. Conversely, dpInclude removes all configuration objects that do not match class names/object names specified in the action.

Attribute Description Required
class Regular expression matching names of DataPower configuration classes. Yes, unless name was provided.
name Regular expression matching names of DataPower configuration objects. Yes, unless class was provided.

11.1.7. namespace Nested Element

All namespace prefixes used in XPath expressions of the transform actions have to be explicitly defined using the namespace element. Note that transforming DataPower configuration files does not require any namespace definitions.

Attribute Description Required
prefix Prefix of the XML namespace that will be used inside XPath expressions. Yes
uri XML namepsace’s URI. Yes