Skip to content

Commit

Permalink
Merge pull request #42677 from mindula/fix-namespaces-issue-master
Browse files Browse the repository at this point in the history
[Master] Fix adding namespaces when the option is not selected
  • Loading branch information
KavinduZoysa authored May 7, 2024
2 parents adec063 + da9fee7 commit b2284b5
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
for (int i = 0; i < xmlAttributesMap.getLength(); i++) {
org.w3c.dom.Node xmlNode = xmlAttributesMap.item(i);
if (xmlNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) {
if ((xmlNode.getPrefix() == null &&
XMLNS_PREFIX.equals(xmlNode.getLocalName())) || (XMLNS_PREFIX.equals(xmlNode.getPrefix()) &&
xmlNode.getLocalName().equals(xmlElement.getPrefix())) && withNameSpace) {
if (((xmlNode.getPrefix() == null && XMLNS_PREFIX.equals(xmlNode.getLocalName())) ||
(XMLNS_PREFIX.equals(xmlNode.getPrefix()) &&
xmlNode.getLocalName().equals(xmlElement.getPrefix()))) && withNameSpace) {
String prefix = null;
if (xmlElement.getPrefix() != null && xmlElement.getPrefix().equals(xmlNode.getLocalName())) {
prefix = xmlNode.getLocalName();
Expand All @@ -296,7 +296,7 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
}
}
if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) {
Token fieldType = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD);
Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue());
IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(textFieldName == null ?
escapeIdentifier("#content") : textFieldName);
Token semicolon = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public class XMLToRecordConverterTests {
private final Path sample25Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_25.bal");

private final Path sample26XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_26.xml");
private final Path sample26Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_26.bal");

private final Path sample27XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_27.xml");
private final Path sample27Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_27.bal");

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


Expand Down Expand Up @@ -423,4 +433,22 @@ public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace()
String expectedCodeBlock = Files.readString(sample25Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

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

@Test(description = "testXMLWithoutNamespaceAnnotations")
public void testXMLWithoutNamespaceAnnotations() throws IOException {
String xmlFileContent = Files.readString(sample27XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample27Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Genre_Genre record {
};

type Currency_Price record {
string \#content;
decimal \#content;
@xmldata:Attribute
string currency;
@xmldata:Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Title record {
string \#content;
@xmldata:Attribute
string lang;
};

type Price record {
decimal \#content;
@xmldata:Attribute
string currency;
};

@xmldata:Name {value: "book"}
type Book record {
int id;
Title title;
string author;
string genre;
Price price;
string publication_date;
string description;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type Customer record {
string firstName;
string lastName;
string email;
int id;
@xmldata:Attribute
string loyalty;
@xmldata:Attribute
string optedInNewsLetter;
@xmldata:Attribute
string 'xmlns;
};

type Item record {
string name;
int quantity;
decimal price;
@xmldata:Attribute
string id;
};

type Items record {
Item[] item;
@xmldata:Attribute
string 'xmlns;
};

type Order record {
int id;
Customer customer;
Items items;
decimal total;
@xmldata:Attribute
string 'xmlns;
};

type Orders record {
Order[] 'order;
@xmldata:Attribute
string 'xmlns;
};
12 changes: 12 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_26.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<book>
<id>123456</id>
<title lang="en">The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<genre>Fiction</genre>
<price currency="USD">10.99</price>
<publication_date>1925-04-10</publication_date>
<description>
A classic novel depicting the roaring twenties in America,
exploring themes of love, wealth, and the American Dream.
</description>
</book>
68 changes: 68 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_27.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<Orders xmlns="www.example.com/orders">
<order xmlns="www.example.com/order">
<id>001</id>
<customer xmlns="www.example.com/customer" loyalty="premium" optedInNewsLetter="true">
<firstName>John</firstName>
<lastName>Doe</lastName>
<email>[email protected]</email>
<id>1001</id>
</customer>
<items xmlns="www.example.com/items">
<item id="a1">
<name>Widget A</name>
<quantity>2</quantity>
<price>15.00</price>
</item>
<item id="b2">
<name>Gadget B</name>
<quantity>1</quantity>
<price>25.00</price>
</item>
</items>
<total>55.00</total>
</order>
<order xmlns="www.example.com/order">
<id>002</id>
<customer xmlns="www.example.com/customer" loyalty="regular" optedInNewsLetter="false">
<firstName>Jane</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<id>1002</id>
</customer>
<items xmlns="www.example.com/items">
<item id="c3">
<name>Tool C</name>
<quantity>3</quantity>
<price>10.00</price>
</item>
<item id="d4">
<name>Device D</name>
<quantity>1</quantity>
<price>40.00</price>
</item>
</items>
<total>70.00</total>
</order>
<order xmlns="www.example.com/order">
<id>003</id>
<customer xmlns="www.example.com/customer" loyalty="new" optedInNewsLetter="true">
<firstName>Alice</firstName>
<lastName>Brown</lastName>
<email>[email protected]</email>
<id>1003</id>
</customer>
<items xmlns="www.example.com/items">
<item id="e5">
<name>Accessory E</name>
<quantity>2</quantity>
<price>5.00</price>
</item>
<item id="f6">
<name>Supplement F</name>
<quantity>4</quantity>
<price>8.00</price>
</item>
</items>
<total>54.00</total>
</order>
</Orders>

0 comments on commit b2284b5

Please sign in to comment.