Skip to content

Commit

Permalink
Fix parsing xml attribute with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
KavinduZoysa committed Aug 2, 2024
1 parent 1644d20 commit e3e5efa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ private static QName getQNameOMElement(OMElement omElement) {
}

private static QName getQNameOMAttribute(OMAttribute omAttribute) {
// If prefix is not provided test case will fail
if (omAttribute.getPrefix() == null) {
return new QName(omAttribute.getNamespaceURI(), omAttribute.getLocalName());
} else {
return new QName(omAttribute.getNamespaceURI(), omAttribute.getLocalName(), omAttribute.getPrefix());
}

return omAttribute.getQName();
}

private static void addAttributes(OMElement omElement, BXmlItem xmlItem) {
Expand All @@ -99,10 +93,8 @@ private static void addAttributes(OMElement omElement, BXmlItem xmlItem) {
//CHECK: Good to put break point here and check the values
attributesMap.put(StringUtils.fromString(qName.toString()),
StringUtils.fromString(attribute.getAttributeValue()));
if (attribute.getPrefix() != null) {
if (!attribute.getPrefix().isEmpty()) {
usedNS.add(qName);
}
if (!qName.getPrefix().isEmpty()) {
usedNS.add(qName);
}
}
if (omElement.getQName().getPrefix() != null) {
Expand Down
72 changes: 72 additions & 0 deletions tests/src/test/java/org/ballerina/test/TestTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,78 @@ public void testJsonTransform1() {
"{\"name\":\"John\",\"age\":23,\"city\":\"Colombo\",\"country\":\"Sri Lanka\"}");
}

@Test
public void testXmlReturn1() throws XMLStreamException {
String project = "project3";
ModuleInfo moduleInfo = new ModuleInfo("testOrg", project, "1");
Mediator mediator = new Mediator(moduleInfo);

List<Property> properties = new ArrayList<>() {{
add(new Property("paramFunctionName", "testXmlReturn"));
add(new Property("paramSize", 1));
add(new Property("param0", "x"));
add(new Property("paramType0", "xml"));
add(new Property("returnType", "xml"));
}};
TestMessageContext context = createMessageContext(properties);

Stack<TemplateContext> stack = new Stack<>();
TemplateContext templateContext = new TemplateContext("testTemplateFunc", new ArrayList<>());
stack.push(templateContext);

HashMap<Object, Object> map = new HashMap<>();
map.put("x", AXIOMUtil.stringToOM("<Document><FIToFICstmrCdtTrf><GrpHdr><MsgId>123489</MsgId><CreDtTm" +
">0111522180</CreDtTm><NbOfTxs>1</NbOfTxs><SttlmInf><SttlmMtd>INDA</SttlmMtd></SttlmInf></GrpHdr" +
"><CdtTrfTxInf><PmtId><EndToEndId>12348912a456789123</EndToEndId></PmtId><IntrBkSttlmAmt " +
"Ccy=\"USD\">500000</IntrBkSttlmAmt><ChrgBr>DEBT</ChrgBr><Dbtr><number>1</number></Dbtr><CdtrAgt" +
"><FinInstnId>100009</FinInstnId></CdtrAgt></CdtTrfTxInf></FIToFICstmrCdtTrf></Document>"));
map.put("Result", "r");
templateContext.setMappedValues(map);

context.setProperty("_SYNAPSE_FUNCTION_STACK", stack);
mediator.mediate(context);
Assert.assertEquals(context.getProperty("r").toString(),
"<Document><FIToFICstmrCdtTrf><GrpHdr><MsgId>123489</MsgId><CreDtTm>0111522180</CreDtTm><NbOfTxs>1" +
"</NbOfTxs><SttlmInf><SttlmMtd>INDA</SttlmMtd></SttlmInf></GrpHdr><CdtTrfTxInf><PmtId" +
"><EndToEndId>12348912a456789123</EndToEndId></PmtId><IntrBkSttlmAmt " +
"Ccy=\"USD\">500000</IntrBkSttlmAmt><ChrgBr>DEBT</ChrgBr><Dbtr><number>1</number></Dbtr" +
"><CdtrAgt><FinInstnId>100009</FinInstnId></CdtrAgt></CdtTrfTxInf></FIToFICstmrCdtTrf" +
"></Document>");
}

@Test
public void testXmlReturn2() throws XMLStreamException {
String project = "project3";
ModuleInfo moduleInfo = new ModuleInfo("testOrg", project, "1");
Mediator mediator = new Mediator(moduleInfo);

List<Property> properties = new ArrayList<>() {{
add(new Property("paramFunctionName", "testXmlReturn"));
add(new Property("paramSize", 1));
add(new Property("param0", "x"));
add(new Property("paramType0", "xml"));
add(new Property("returnType", "xml"));
}};
TestMessageContext context = createMessageContext(properties);

Stack<TemplateContext> stack = new Stack<>();
TemplateContext templateContext = new TemplateContext("testTemplateFunc", new ArrayList<>());
stack.push(templateContext);

HashMap<Object, Object> map = new HashMap<>();
map.put("x", AXIOMUtil.stringToOM("<library xmlns:bk=\"http://example.com/book\" xmlns:auth=\"http://example" +
".com/author\"><bk:Book bk:id=\"001\"><bk:Title>1984</bk:Title><bk:Author " +
"auth:country=\"UK\"><auth:Name>George Orwell</auth:Name></bk:Author></bk:Book></library>"));
map.put("Result", "r");
templateContext.setMappedValues(map);

context.setProperty("_SYNAPSE_FUNCTION_STACK", stack);
mediator.mediate(context);
Assert.assertEquals(context.getProperty("r").toString(),
"<library><bk:Book xmlns:bk=\"http://example.com/book\" " +
"bk:id=\"001\"><bk:Title>1984</bk:Title><bk:Author xmlns:auth=\"http://example.com/author\" " +
"auth:country=\"UK\"><auth:Name>George Orwell</auth:Name></bk:Author></bk:Book></library>");
}

private TestMessageContext createMessageContext(List<Property> properties) {
TestMessageContext context = new TestMessageContext();
Expand Down
4 changes: 4 additions & 0 deletions tests/src/test/resources/ballerina/project3/main.bal
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public function test(xml xmlA, xml xmlB, xml xmlC, xml xmlD) returns xml {
return xml `<may22>${xmlA}${xmlB}${xmlC}${xmlD}</may22>`;
}

public function testXmlReturn(xml x) returns xml {
return x;
}

0 comments on commit e3e5efa

Please sign in to comment.