Skip to content

Commit

Permalink
Merge pull request #42699 from mindula/fix-42698
Browse files Browse the repository at this point in the history
[Master] Fix xml to record conversion for default value node
  • Loading branch information
KavinduZoysa authored Jun 17, 2024
2 parents c5ff52c + 88ce468 commit 8e81723
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
if (xmlNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
Element xmlElementNode = (Element) xmlNode;
boolean isLeafXMLElementNode = isLeafXMLElementNode(xmlElementNode);
if (!isLeafXMLElementNode || xmlElementNode.getAttributes().getLength() > 0) {
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (!isLeafXMLElementNode || xmlAttributesMap.getLength() > 1
|| (xmlAttributesMap.getLength() == 1
&& !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) {
generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes,
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace);
}
Expand Down Expand Up @@ -320,18 +323,25 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
}
}
}
if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) {
int attributeLength = xmlElement.getAttributes().getLength();
org.w3c.dom.Node attributeItem = xmlElement.getAttributes().item(0);
if (isLeafXMLElementNode(xmlElement) && attributeLength > 0) {
if (attributeLength == 1 && attributeItem.getPrefix() != null
&& XMLNS_PREFIX.equals(attributeItem.getPrefix())) {
return recordFields;
}
Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue());
IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(textFieldName == null ?
escapeIdentifier("#content") : textFieldName);
Token semicolon = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null, fieldType,
fieldName, null, semicolon);
recordFields.add(recordFieldNode);
for (int j = 0; j < xmlElement.getAttributes().getLength(); j++) {
for (int j = 0; j < attributeLength; j++) {
org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j);
if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) {
RecordFieldNode recordField = (RecordFieldNode) getRecordField(xmlAttributeNode);
if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())) {
Node recordField = getRecordField(xmlAttributeNode);
recordFields.add(recordField);
}
}
Expand Down Expand Up @@ -461,7 +471,10 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is
Token optionalFieldToken = isOptionalField ? questionMarkToken : null;
Token semicolonToken = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);

if (isLeafXMLElementNode(xmlElementNode) && xmlElementNode.getAttributes().getLength() == 0) {
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 ||
(xmlAttributesMap.getLength() == 1
&& XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())))) {
typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue());
} else {
// At the moment all are considered as Objects here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public class XMLToRecordConverterTests {
private final Path sample32Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_32.bal");

private final Path sample33XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_33.xml");
private final Path sample33Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_33.bal");

private final Path sample34XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_34.xml");
private final Path sample34Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_34.bal");

private static final String XMLToRecordServiceEP = "xmlToRecord/convert";


Expand Down Expand Up @@ -486,7 +496,7 @@ public void testXMLWithMultipleNamespacesAndSameElement() throws IOException {
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleNamespaces2")
@Test(description = "testXMLWithMultipleNamespaces")
public void testXMLWithMultipleNamespaces2() throws IOException {
String xmlFileContent = Files.readString(sample29XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
Expand All @@ -495,7 +505,7 @@ public void testXMLWithMultipleNamespaces2() throws IOException {
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleNamespaces3")
@Test(description = "testXMLWithMultipleNamespaces")
public void testXMLWithMultipleNamespaces3() throws IOException {
String xmlFileContent = Files.readString(sample30XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
Expand All @@ -521,4 +531,22 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept
String expectedCodeBlock = Files.readString(sample32Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultValueNode")
public void textXMLWithDefaultValueNode() throws IOException {
String xmlFileContent = Files.readString(sample33XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultValueNode")
public void textXMLWithDefaultValueNode2() throws IOException {
String xmlFileContent = Files.readString(sample34XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"__text", true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"}
type A_Address record {
@xmldata:Namespace {prefix: "c", uri: "[www.example.org/addressCity](http://www.example.org/addressCity)"}
string city;
@xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"}
string state;
@xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"}
string country;
};

@xmldata:Name {value: "person"}
@xmldata:Namespace {prefix: "p", uri: "[www.example.org/personData](http://www.example.org/personData)"}
type P_Person record {
@xmldata:Namespace {prefix: "n", uri: "[www.example.org/personName](http://www.example.org/personName)"}
string name;
@xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"}
A_Address address;
@xmldata:Attribute
string age;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@xmldata:Namespace {prefix: "n", uri: "http://www.example.org/personName"}
type N_Name record {
string __text;
@xmldata:Attribute
string 'type;
};

@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Address record {
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
string street;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
string city;
};

@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Occupation record {
string __text;
@xmldata:Attribute
string country;
};

@xmldata:Name {value: "person"}
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Person record {
@xmldata:Namespace {prefix: "n", uri: "http://www.example.org/personName"}
N_Name name;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
P_Address address;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
P_Occupation occupation;
@xmldata:Attribute
string age;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p:person xmlns:p="[www.example.org/personData](http://www.example.org/personData)" age="30">
<n:name xmlns:n="[www.example.org/personName](http://www.example.org/personName)">John</n:name>
<a:address xmlns:a="[www.example.org/personAddress](http://www.example.org/personAddress)">
<c:city xmlns:c="[www.example.org/addressCity](http://www.example.org/addressCity)">Hollywood</c:city>
<s:state xmlns:s="[www.example.org/addressState](http://www.example.org/addressState)">FL</s:state>
<ct:country xmlns:ct="[www.example.org/addressCountry](http://www.example.org/addressCountry)">USA</ct:country>
</a:address>
</p:person>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p:person xmlns:p="http://www.example.org/personData" age="30">
<n:name xmlns:n="http://www.example.org/personName" type="first">John</n:name>
<p:address xmlns:p="http://www.example.org/personData">
<p:street>Main Street</p:street>
<p:city>New York</p:city>
</p:address>
<p:occupation xmlns:p="http://www.example.org/personData" country="USA">Software Engineer</p:occupation>
</p:person>

0 comments on commit 8e81723

Please sign in to comment.