How to HTML entity encode / decode in ballerina?

74 Views Asked by At

Currently I'm having a requirement of HTML entity encode / decode using Ballerina language. However I couldn't find any references for that.

If I have a string as following,

https://spdx.org/licenses/Apache-2.0.html

Expected result would be,

https://spdx.org/licenses/Apache-2.0.html

You can refer to the following screenshot where I have done the same thing using an online tool : https://mothereff.in/html-entities

html entity encode / decode sample

How can we achieve the above?

1

There are 1 best solutions below

0
Anupama Pathirage On

There is no direct Ballerina module that provides this functionality. But it can be done using the Java interoperability feature in the Ballerina language.

The StringEscapeUtils class of Apache common-text library can be used for this.

Steps:

  1. Create a new Ballerina project using bal new <project name> command.
  2. Generate Ballerina bindings for StringEscapeUtils using below command.

bal bindgen -mvn org.apache.commons:commons-text:1.9 org.apache.commons.text.StringEscapeUtils

  1. Build the project using bal build command.

  2. Write the Ballerina code for encode and decode.

import ballerina/io;
import htmlTest.org.apache.commons.text as text;

public function main() {
    string? decodedText = text:StringEscapeUtils_unescapeHtml3("https&#x3a;&#x2f;&#x2f;spdx.org&#x2f;licenses&#x2f;Apache-2.0.html");
    if decodedText is string {
        io:println(decodedText); //Output : https://spdx.org/licenses/Apache-2.0.html
    } else {
        io:println("Error occured during conversion");
    }
}

For more details refer to the Java interoperability guide on Ballerina.