How to HTML entity encode / decode in ballerina? - html-encode

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
How can we achieve the above?

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:
Create a new Ballerina project using bal new <project name> command.
Generate Ballerina bindings for StringEscapeUtils using below command.
bal bindgen -mvn org.apache.commons:commons-text:1.9 org.apache.commons.text.StringEscapeUtils
Build the project using bal build command.
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://spdx.org/licenses/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.

Related

How do I use SharePlusLinuxPlugin?

I'm trying to open an image from memory with the default image viewer on Linux.
The class is part of the share_plus package. I can't figure out how to define the required UrlLauncherPlatform property.
I want to use it like this:
SharePlusLinuxPlugin(urlLauncher).shareXFiles([XFile.fromData(img)]);
I have googled this class, didn't find any usage examples.
From the source code on Github, it looks like shareXFiles() has not been implemented on Linux. To quote:
Future<ShareResult> shareXFiles(
List<XFile> files, {
String? subject,
String? text,
Rect? sharePositionOrigin,
}) {
throw UnimplementedError(
'shareXFiles() has not been implemented on Linux.',
);
}
The same is true for the shareFiles() method.
Aside from that, there is usually no need to call SharePlusLinuxPlugin directly. The Share class is configured in such a way that it automatically detects the platform that it is running on

Create docx file from a template file in java

I need to create docx files based on a templates.
The template should contain the place holders and I should be able to fill the the place holders from java .
Is it possible to do it , If so suggest me the good and efficient way to do it .
A little late for the original question, but if anyone else needs to dynamically create docx documents from templates, you might want to have a look at the DocxStamper Java library which I created on top of docx4j.
It allows to use the Spring Expression Language in docx templates and you can create a document out of a template with a couple lines like this:
MyData data = ...; // your own POJO containing the data
InputStream template = ...; // InputStream to the template file
OutputStream out = ...; // OutputStream to the resulting document
DocxStamper stamper = new DocxStamperConfiguration()
.build();
stamper.stamp(template, context, out);
out.close();
As discussed elsewhere before, there are 3 basic approaches:
BEST: content control data binding
cheap/cheerful: Variable replacement (ie magic strings on the document surface), but brittle (the split run problem)
LEGACY: MERGEFIELD with or without other field codes.
Docx4j supports all three approaches, but we generally recommend content control databinding, since it aligns with Microsoft's direction (as best can be ascertained), and is most powerful.
You'll want to consider the technical skills of your template authors.
See https://github.com/centic9/poi-mail-merge for a simple "Variable replacement" method. It does not work if one replacement-string has multiple formats applied, but does work well for simple cases where the template is carefully crafted.
Basically it reads the template and data from CSV or an Excel file and then merges it into multiple result files, one for each line of data.
It works on the DOCX XML content, so is not fully using Apache POI XWPF support, but this way formatting and other things from the template are used as expected without the need for full support for everything in Apache POI (which has DOCX support still as part of the "scratchpad" component as support is not considered fully done yet).
You can use Word template with following syntax of LINQ Reporting to achieve your requirements using Aspose.Words for Java.
<< tag_name [expression] -switch1 -switch2 ...>>
A tag body typically consists of the following elements:
A tag name
An expression surrounded by brackets
A set of switches available for the tag, each of which is preceded by the “-“ character
Assume, that you have the Sender class defined in your application as follows:
public class Sender {
public Sender(String name, String message) {
_name = name;
_message = message;
}
public String getName() {
return _name;
}
public String getMessage() {
return _message;
}
private String _name;
private String _message;
}
To produce a report containing a message of a concrete sender on its behalf, you can use a template document with the following content.
<<[s.getName()]>> says: "<<[s.getMessage()]>>."
To build a report from the template, you can use the following source code.
Document doc = new Document(getMyDir() + "temp_HelloWorld.docx");
Sender sender = new Sender("LINQ Reporting Engine", "Hello World");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, sender, "s");
doc.save(getMyDir() + "out.docx");
I work with Aspose as Developer evangelist.

Alternative for deprecated JAXBToStringBuilder

We have an auto generated class which is importing the following classes:
import org.apache.cxf.jaxb.JAXBToStringBuilder;
import org.apache.cxf.jaxb.JAXBToStringStyle;
Both of these seem to be deprecated, I am looking for an alternative, did some search but couldn't find anything similar to use for the following method:
#Override
public String toString() {
return JAXBToStringBuilder.valueOf(this, JAXBToStringStyle.DEFAULT_STYLE);
}
The javadoc for 2.7.x says
Class was moved to project org.apache.cxf.xjc-utils:cxf-xjc-runtime so generated code does not have to depend on cxf
Depending on a current cxf-xjc-runtime and changing the imports seemed to be enough for me.
Apache Commons has a class called ToStringBuilder that you can introduce in your code to help representing objects as Strings in a human readable format.
See the doc here.
Check the constructor where you can specify the style:
ToStringBuilder(Object object, ToStringStyle style)
Constructs a builder for the specified object using the a defined output style.
There you can provide to the default style you mentioned in your question:
public String toString() {
return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).toString();
}

Access tied files through groovy in jenkins

I'm building a Jenkins plugin, and am handling the UI components using Groovy. In jelly, you can use "${it.something}" to access information in the java file tied to the jelly file, as shown here:
class:
public String getMyString() {
return "Hello Jenkins!";
}
jelly:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
${it.myString}
</j:jelly>
from https://wiki.jenkins-ci.org/display/JENKINS/Basic+guide+to+Jelly+usage+in+Jenkins.
I'd like to do the same thing in groovy, but can't seem to find an example of how it's done. Any examples?
After even more searching and some luck, I found the correct way to do this. If I was to use the class in my question but wanted to use groovy instead of jelly, the groovy code would look like this (this puts the string in the textbox):
package something.something;
import lib.JenkinsTagLib
import lib.FormTagLib
def f = namespace(lib.FormTagLib)
t=namespace(JenkinsTagLib.class)
f.entry(title:"text", field:"text") {
f.textbox(value:instance?.text)
}

how to read XML file using xml reader?

Question 1: Assume that i am reading XmlNodeType.Text and I would like to know its tag node name. How do you do that without moving cursor up or down? Also How can I know parent tag of current node tag?
Question 2: Assume that I am reading xml file and I would like to start at particular node tag. How can do that?
Question 3: if you have xsd file, is there easy way to upload xml file? I am using C# 3.5 .net and sql server 2008.
This is what i wrote so far:
XmlTextReader reader = new XmlTextReader("datafile.xml");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
Console.Write(reader.Name);
}
else if (reader.NodeType == XmlNodeType.Text)
{
Console.Write("/"+reader.Name+"/" + reader.Value+"/");
}
else
{
if (reader.NodeType == XmlNodeType.EndElement)
{
Console.WriteLine(reader.Name);
Console.ReadLine();
}
}
}
reader.Close();
Please let me know if you need more clarification
XmlReader is stateless and only retains information about the current node, so if you are reading the content of an element and wish to know the elements name you need to make sure that when you read the start element node you somehow retain the element name.
Again if you want to know the name of the parent element you need to retain this information / state yourself as you read through the xml document.
If you wish to start reading at a particular node you should go through and read the xml document node by node until you read the node you wish to start at.
Ultimately reading xml via the XmlReader class is more difficult than the alternatives, generally speaking you would only use XmlReader if the the xml document is very large, in most other cases using one of the alternatives:
Linq to XML
The XmlDocument class
Using XSD.exe to generate a .Net class from a XSD file that can be used to serialise and deserialise xml via the XmlSerializer class.
For more information see XML Serialization in the .NET Framework
If you really want to use XmlReader then you should read Using the XmlReader Class .

Resources