Creating Java Custom Functions

You can create custom XPath functions that you can directly use in the Activity Input. These functions come very handy especially when you expect to use them repeatedly for mapping. You can do that by creating a Java Custom Function shared resource.


You will have to write a Java class that has a public and static method and returns a value. The method cannot be void or explicitly throw an exception.

Supposed you want to have a custom function that replaces all occurrences of a string with another string. This is, in my experience, a common function that is not readily available in TIBCO BW because it doesn't support XSLT 2.0; it's limited to XSLT 1.0. But it allows us to write a Java method and call it via XSLT! To demonstrate that, we can have a Java method that accepts three parameters: the input text, the regular expression or pattern, and the replacement text.

I have named this method "replaceRegex" but it could be any descriptive name. I used the StringBuilder, and the Pattern and Matcher class from java.util.regex package. StringBuilder is, you know, more efficient for string manipulation. Note that the Matcher object is continuously updated until the pattern does not find the given regular expression, and all occurrences of such in the input text have been replaced by the new text.

public static String replaceRegex(String inputText, String oldText, String newText) {
        StringBuilder sb = new StringBuilder(inputText);
        Pattern p = Pattern.compile(oldText);
        Matcher m = null;
        while((m = p.matcher(sb)).find()) {
            sb.replace(m.start(), m.end(), newText);
        }
        return sb.toString();
    }

Optionally (you can skip this though and the function will work perfectly), create a two-dimensional array constant named HELP_STRINGS. Index 0,0 is the exact name of the method, Index 0,1 is the description, Index 0,2 is the example, and Index 0,3 is the output. You only have to create one HELP_STRINGS constant.

public static final String[][] HELP_STRINGS = {        
        {    "replaceRegex",
            "Finds occurences of regex as parameter2 from input text as parameter1 and replaces them with the new text as parameter3.",         
            "replaceRegex(\"I don\'t want the spaces.\", \"\\s\", \"\")",
            "Idon'twantthespaces."
        }
    };

The help strings will appear in the XPath Formula Builder of TIBCO Designer. You can access the custom function under Functions tab inside a folder which is named based on the value of the "Suggested Prefix" field. You can then use it like a normal XSLT function.


Comments

  1. Custom function for Java programming is basically used for mapping but it can also be used for multiple purposes. This is one of the best work of blogger regarding using functions.

    ReplyDelete

Post a Comment

Popular posts from this blog

XML Schema and JSON Schema Validation in Mule 4

Parsing a JSON String and Converting it to XML in TIBCO

Using XML To Java in TIBCO BW