From 5c208ffd14e72ed940017985b19f3911d785bdff Mon Sep 17 00:00:00 2001 From: mindula Date: Thu, 2 May 2024 10:10:06 +0530 Subject: [PATCH 1/2] Fix adding namespaces issue --- .../xmltorecordconverter/XMLToRecordConverter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java index 405fdee21be3..1e37315a68ff 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java @@ -277,9 +277,9 @@ private static List 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(); @@ -296,7 +296,7 @@ private static List 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); From da9fee71c8893b0cab653c549bc8bcb1a0cedefb Mon Sep 17 00:00:00 2001 From: mindula Date: Thu, 2 May 2024 10:38:45 +0530 Subject: [PATCH 2/2] Add tests --- .../XMLToRecordConverterTests.java | 28 ++++++++ .../test/resources/ballerina/sample_20.bal | 2 +- .../test/resources/ballerina/sample_26.bal | 22 ++++++ .../test/resources/ballerina/sample_27.bal | 41 +++++++++++ .../src/test/resources/xml/sample_26.xml | 12 ++++ .../src/test/resources/xml/sample_27.xml | 68 +++++++++++++++++++ 6 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_26.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_27.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_26.xml create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_27.xml diff --git a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java index d85b701008a0..7a3d4b0d8346 100644 --- a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java +++ b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java @@ -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"; @@ -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); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_20.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_20.bal index 487e0af8daf8..400da59eea37 100644 --- a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_20.bal +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_20.bal @@ -23,7 +23,7 @@ type Genre_Genre record { }; type Currency_Price record { - string \#content; + decimal \#content; @xmldata:Attribute string currency; @xmldata:Attribute diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_26.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_26.bal new file mode 100644 index 000000000000..fbda74d6e036 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_26.bal @@ -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; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_27.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_27.bal new file mode 100644 index 000000000000..88eae813663e --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_27.bal @@ -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; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_26.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_26.xml new file mode 100644 index 000000000000..6d381a7007a9 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_26.xml @@ -0,0 +1,12 @@ + + 123456 + The Great Gatsby + F. Scott Fitzgerald + Fiction + 10.99 + 1925-04-10 + + A classic novel depicting the roaring twenties in America, + exploring themes of love, wealth, and the American Dream. + + diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_27.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_27.xml new file mode 100644 index 000000000000..33cdda2a15b1 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_27.xml @@ -0,0 +1,68 @@ + + + 001 + + John + Doe + john.doe@example.com + 1001 + + + + Widget A + 2 + 15.00 + + + Gadget B + 1 + 25.00 + + + 55.00 + + + 002 + + Jane + Smith + jane.smith@example.com + 1002 + + + + Tool C + 3 + 10.00 + + + Device D + 1 + 40.00 + + + 70.00 + + + 003 + + Alice + Brown + alice.brown@example.com + 1003 + + + + Accessory E + 2 + 5.00 + + + Supplement F + 4 + 8.00 + + + 54.00 + +