Skip to content

Commit

Permalink
Merge pull request #25 from KavinduZoysa/update-workflow
Browse files Browse the repository at this point in the history
Fix checkstyle issues
  • Loading branch information
KavinduZoysa authored Jun 10, 2024
2 parents a35d8b2 + 8b837ae commit 165aaa3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.0-20240502-141200-0a49fa42"
distribution-version = "2201.9.0"

[[package]]
org = "ballerinax"
Expand Down
32 changes: 20 additions & 12 deletions native/src/main/java/io/ballerina/stdlib/mi/BXmlConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import io.ballerina.runtime.api.values.BXml;
import io.ballerina.runtime.api.values.BXmlItem;
import io.ballerina.runtime.internal.values.XmlPi;
import org.apache.axiom.om.*;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMComment;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMText;
import org.apache.commons.lang3.tuple.Pair;

import java.util.HashMap;
Expand All @@ -45,9 +52,12 @@ static Pair<String, String> extractNamespace(String value) {
}

public static OMElement toOMElement(BXml bXml) {
BXmlItem xmlItem = (BXmlItem) bXml;
if (!(bXml instanceof BXmlItem xmlItem)) {
return null;
}

OMNamespace namespace = factory.createOMNamespace(xmlItem.getQName().getNamespaceURI(), xmlItem.getQName().getPrefix());
OMNamespace namespace = factory.createOMNamespace(xmlItem.getQName().getNamespaceURI(),
xmlItem.getQName().getPrefix());
BMap<BString, BString> bMap = xmlItem.getAttributesMap();

OMElement rootElement = factory.createOMElement(xmlItem.getQName().getLocalPart(), namespace);
Expand All @@ -68,8 +78,8 @@ public static OMElement toOMElement(BXml bXml) {
if (!attribute.getKey().getValue().startsWith(BXmlItem.XMLNS_NS_URI_PREFIX)) {
//if this is a namespace
Pair<String, String> pair = extractNamespace(attribute.getKey().getValue());
OMNamespace ns = namespaceMap.get(pair.getLeft());
OMAttribute omattribute = factory.createOMAttribute(pair.getRight(), namespaceMap.get(pair.getLeft()), attribute.getValue().getValue());
OMAttribute omattribute = factory.createOMAttribute(pair.getRight(), namespaceMap.get(pair.getLeft()),
attribute.getValue().getValue());
rootElement.addAttribute(omattribute);
//TODO: previously used OMAttribute creation method research why it was changed to attribute.
}
Expand All @@ -86,7 +96,9 @@ static void addChildrenElements(OMElement rootElement, BXml bXml) {
switch (child.getNodeType()) {
case ELEMENT:
OMElement childElement = toOMElement(child);
rootElement.addChild(childElement);
if (childElement != null) {
rootElement.addChild(childElement);
}
break;
case TEXT:
OMText omText = factory.createOMText(rootElement, child.getTextValue());
Expand All @@ -98,18 +110,14 @@ static void addChildrenElements(OMElement rootElement, BXml bXml) {
break;
case PI:
XmlPi xmlPi = (XmlPi) child;
OMProcessingInstruction omProcessingInstruction = factory.createOMProcessingInstruction(null, xmlPi.getTarget(), xmlPi.getData());
OMProcessingInstruction omProcessingInstruction = factory.createOMProcessingInstruction(null,
xmlPi.getTarget(), xmlPi.getData());
rootElement.addChild(omProcessingInstruction);
break;
default:
break;
}
}
}

private boolean isNamespace(String value) {
// starts with XmlItem.XMLNS_NS_URI_PREFIX true else false
return value.startsWith(BXmlItem.XMLNS_NS_URI_PREFIX);
}
}

11 changes: 5 additions & 6 deletions native/src/main/java/io/ballerina/stdlib/mi/Mediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@ public Mediator(ModuleInfo moduleInfo) {
}

private static String getResultProperty(SimpleMessageContext context) {
String result = context.lookupTemplateParameter(Constants.RESULT).toString();
if (result != null) {
return result;
}
return Constants.RESULT;
return context.lookupTemplateParameter(Constants.RESULT).toString();
}

public void mediate(SimpleMessageContext context) {
Callback returnCallback = new Callback() {
public void notifySuccess(Object result) {
log.info("Notify Success");
context.setProperty(getResultProperty(context), BXmlConverter.toOMElement((BXml) result));
OMElement omElement = BXmlConverter.toOMElement((BXml) result);
if (omElement != null) {
context.setProperty(getResultProperty(context), BXmlConverter.toOMElement((BXml) result));
}
}

public void notifyFailure(BError result) {
Expand Down
18 changes: 9 additions & 9 deletions native/src/main/java/io/ballerina/stdlib/mi/ModuleInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@
package io.ballerina.stdlib.mi;

public class ModuleInfo {
private static String ORG_NAME = "BALLERINA_ORG_NAME";
private static String MODULE_NAME = "BALLERINA_MODULE_NAME";
private static String MODULE_VERSION = "BALLERINA_MODULE_VERSION";
private String ballerinaOrgName = "BALLERINA_ORG_NAME";
private String ballerinaModuleName = "BALLERINA_MODULE_NAME";
private String ballerinaModuleVersion = "BALLERINA_MODULE_VERSION";

public String getOrgName() {
return ORG_NAME;
return ballerinaOrgName;
}

public String getModuleName() {
return MODULE_NAME;
return ballerinaModuleName;
}

public String getModuleVersion() {
return MODULE_VERSION;
return ballerinaModuleVersion;
}

public ModuleInfo() {}

public ModuleInfo(String orgName, String moduleName, String moduleVersion) {
ORG_NAME = orgName;
MODULE_NAME = moduleName;
MODULE_VERSION = moduleVersion;
ballerinaOrgName = orgName;
ballerinaModuleName = moduleName;
ballerinaModuleVersion = moduleVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.api.values.BXml;
import io.ballerina.runtime.api.values.BXmlItem;
import org.apache.axiom.om.*;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMComment;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMText;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.xml.namespace.QName;

public class OMElementConverter {

public static BXml toBXml(OMElement omElement) {
Expand Down Expand Up @@ -64,7 +71,8 @@ private static BXml toBXml(OMNode omNode) {


private static QName getQNameOMElement(OMElement omElement) {
return new QName(omElement.getQName().getNamespaceURI(), omElement.getQName().getLocalPart(), omElement.getQName().getPrefix());
return new QName(omElement.getQName().getNamespaceURI(), omElement.getQName().getLocalPart(),
omElement.getQName().getPrefix());
}

private static QName getQNameOMAttribute(OMAttribute omAttribute) {
Expand All @@ -78,7 +86,8 @@ private static QName getQNameOMAttribute(OMAttribute omAttribute) {
}

private static void addAttributes(OMElement omElement, BXmlItem xmlItem) {
// NOTE: Extracted the idea from bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/XmlTreeBuilder.java
// NOTE: Extracted the idea from
// bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/XmlTreeBuilder.java
Iterator attributes = omElement.getAllAttributes();
BMap<BString, BString> attributesMap = xmlItem.getAttributesMap();
Set<QName> usedNS = new HashSet<>();
Expand All @@ -88,7 +97,8 @@ private static void addAttributes(OMElement omElement, BXmlItem xmlItem) {
OMAttribute attribute = (OMAttribute) attributes.next();
QName qName = getQNameOMAttribute(attribute);
//CHECK: Good to put break point here and check the values
attributesMap.put(StringUtils.fromString(qName.toString()), StringUtils.fromString(attribute.getAttributeValue()));
attributesMap.put(StringUtils.fromString(qName.toString()),
StringUtils.fromString(attribute.getAttributeValue()));
if (attribute.getPrefix() != null) {
if (!attribute.getPrefix().isEmpty()) {
usedNS.add(qName);
Expand Down
2 changes: 1 addition & 1 deletion tool/tool-mi/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.0-20240502-141200-0a49fa42"
distribution-version = "2201.9.0"

[[package]]
org = "ballerinax"
Expand Down

0 comments on commit 165aaa3

Please sign in to comment.