From e3829f28fa82cacbcdfd5b628787cb831789d0b1 Mon Sep 17 00:00:00 2001 From: mindula Date: Tue, 4 Jun 2024 20:06:10 +0530 Subject: [PATCH 1/6] Fix ClassCast exception --- .../io/ballerina/xmltorecordconverter/XMLToRecordConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c64ea0f2905c..b80b10b9ea4b 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 @@ -331,7 +331,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole for (int j = 0; j < xmlElement.getAttributes().getLength(); j++) { org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j); if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) { - RecordFieldNode recordField = (RecordFieldNode) getRecordField(xmlAttributeNode); + Node recordField = getRecordField(xmlAttributeNode); recordFields.add(recordField); } } From c1d32d9ee62d16da1661a55b62be479a40e87061 Mon Sep 17 00:00:00 2001 From: mindula Date: Tue, 4 Jun 2024 20:08:08 +0530 Subject: [PATCH 2/6] Add tests --- .../XMLToRecordConverterTests.java | 14 ++++++ .../test/resources/ballerina/sample_33.bal | 48 +++++++++++++++++++ .../src/test/resources/xml/sample_33.xml | 8 ++++ 3 files changed, 70 insertions(+) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_33.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 6f370d5547d4..2d42da316058 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 @@ -204,6 +204,11 @@ 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 static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -521,4 +526,13 @@ 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, + "__text", true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal new file mode 100644 index 000000000000..f0296a61fd0f --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal @@ -0,0 +1,48 @@ +@xmldata:Namespace {prefix: "n", uri: "[www.example.org/personName](http://www.example.org/personName)"} +type N_Name record { + string __text; + @xmldata:Attribute + string n = "[www.example.org/personName](http://www.example.org/personName)"; +}; + +@xmldata:Namespace {prefix: "c", uri: "[www.example.org/addressCity](http://www.example.org/addressCity)"} +type C_City record { + string __text; + @xmldata:Attribute + string c = "[www.example.org/addressCity](http://www.example.org/addressCity)"; +}; + +@xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"} +type S_State record { + string __text; + @xmldata:Attribute + string s = "[www.example.org/addressState](http://www.example.org/addressState)"; +}; + +@xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"} +type Ct_Country record { + string __text; + @xmldata:Attribute + string ct = "[www.example.org/addressCountry](http://www.example.org/addressCountry)"; +}; + +@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)"} + C_City city; + @xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"} + S_State state; + @xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"} + Ct_Country 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)"} + N_Name name; + @xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"} + A_Address address; + @xmldata:Attribute + string age; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_33.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_33.xml new file mode 100644 index 000000000000..66f81a387d28 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_33.xml @@ -0,0 +1,8 @@ + + John + + Hollywood + FL + USA + + From 19c1f86e263238f903e78b439e74751e80eb1016 Mon Sep 17 00:00:00 2001 From: mindula Date: Tue, 4 Jun 2024 20:12:32 +0530 Subject: [PATCH 3/6] Address review suggestions --- .../XMLToRecordConverter.java | 15 ++++++-- .../test/resources/ballerina/sample_33.bal | 36 +++---------------- 2 files changed, 16 insertions(+), 35 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 b80b10b9ea4b..fdf68787d5cc 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 @@ -248,7 +248,9 @@ private static List 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) { + if (!isLeafXMLElementNode || xmlElementNode.getAttributes().getLength() > 1 || + (xmlElementNode.getAttributes().getLength() == 1 + && !XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix()))) { generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace); } @@ -321,6 +323,10 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole } } if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) { + if (xmlElement.getAttributes().getLength() == 1 && xmlElement.getAttributes().item(0).getPrefix() != null && + XMLNS_PREFIX.equals(xmlElement.getAttributes().item(0).getPrefix())) { + return recordFields; + } Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue()); IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(textFieldName == null ? escapeIdentifier("#content") : textFieldName); @@ -330,7 +336,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole recordFields.add(recordFieldNode); for (int j = 0; j < xmlElement.getAttributes().getLength(); j++) { org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j); - if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) { + if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE && !XMLNS_PREFIX + .equals(xmlAttributeNode.getPrefix())) { Node recordField = getRecordField(xmlAttributeNode); recordFields.add(recordField); } @@ -461,7 +468,9 @@ 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) { + if (isLeafXMLElementNode(xmlElementNode) && (xmlElementNode.getAttributes().getLength() == 0 || + (xmlElementNode.getAttributes().getLength() == 1 && + XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix())))) { typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue()); } else { // At the moment all are considered as Objects here diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal index f0296a61fd0f..9ec5ae280a22 100644 --- a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_33.bal @@ -1,46 +1,18 @@ -@xmldata:Namespace {prefix: "n", uri: "[www.example.org/personName](http://www.example.org/personName)"} -type N_Name record { - string __text; - @xmldata:Attribute - string n = "[www.example.org/personName](http://www.example.org/personName)"; -}; - -@xmldata:Namespace {prefix: "c", uri: "[www.example.org/addressCity](http://www.example.org/addressCity)"} -type C_City record { - string __text; - @xmldata:Attribute - string c = "[www.example.org/addressCity](http://www.example.org/addressCity)"; -}; - -@xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"} -type S_State record { - string __text; - @xmldata:Attribute - string s = "[www.example.org/addressState](http://www.example.org/addressState)"; -}; - -@xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"} -type Ct_Country record { - string __text; - @xmldata:Attribute - string ct = "[www.example.org/addressCountry](http://www.example.org/addressCountry)"; -}; - @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)"} - C_City city; + string city; @xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"} - S_State state; + string state; @xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"} - Ct_Country country; + 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)"} - N_Name name; + string name; @xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"} A_Address address; @xmldata:Attribute From 27b1f920fd1bdd30a48670faf0afe59c58f3a235 Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 5 Jun 2024 09:35:06 +0530 Subject: [PATCH 4/6] Fix formatting --- .../ballerina/xmltorecordconverter/XMLToRecordConverter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 fdf68787d5cc..3438c7f0c0b2 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 @@ -323,8 +323,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole } } if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) { - if (xmlElement.getAttributes().getLength() == 1 && xmlElement.getAttributes().item(0).getPrefix() != null && - XMLNS_PREFIX.equals(xmlElement.getAttributes().item(0).getPrefix())) { + if (xmlElement.getAttributes().getLength() == 1 && xmlElement.getAttributes().item(0).getPrefix() != null + && XMLNS_PREFIX.equals(xmlElement.getAttributes().item(0).getPrefix())) { return recordFields; } Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue()); From 24a5d53a8c7510128ae0cd38dcde36d7a2b69775 Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 5 Jun 2024 09:56:46 +0530 Subject: [PATCH 5/6] Address more review comments --- .../XMLToRecordConverter.java | 12 +++---- .../XMLToRecordConverterTests.java | 20 +++++++++-- .../test/resources/ballerina/sample_34.bal | 34 +++++++++++++++++++ .../src/test/resources/xml/sample_34.xml | 8 +++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_34.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_34.xml 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 3438c7f0c0b2..1a9a9968f54e 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 @@ -248,8 +248,8 @@ private static List 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() > 1 || - (xmlElementNode.getAttributes().getLength() == 1 + if (!isLeafXMLElementNode || xmlElementNode.getAttributes().getLength() > 1 + || (xmlElementNode.getAttributes().getLength() == 1 && !XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix()))) { generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace); @@ -336,8 +336,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole recordFields.add(recordFieldNode); for (int j = 0; j < xmlElement.getAttributes().getLength(); j++) { org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j); - if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE && !XMLNS_PREFIX - .equals(xmlAttributeNode.getPrefix())) { + if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE + && !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())) { Node recordField = getRecordField(xmlAttributeNode); recordFields.add(recordField); } @@ -469,8 +469,8 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is Token semicolonToken = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN); if (isLeafXMLElementNode(xmlElementNode) && (xmlElementNode.getAttributes().getLength() == 0 || - (xmlElementNode.getAttributes().getLength() == 1 && - XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix())))) { + (xmlElementNode.getAttributes().getLength() == 1 + && XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix())))) { typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue()); } else { // At the moment all are considered as Objects here 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 2d42da316058..d4a7818a9f21 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 @@ -209,6 +209,11 @@ public class XMLToRecordConverterTests { 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"; @@ -491,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, @@ -500,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, @@ -531,8 +536,17 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept public void textXMLWithDefaultValueNode() throws IOException { String xmlFileContent = Files.readString(sample33XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + 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); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_34.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_34.bal new file mode 100644 index 000000000000..8c9b44e57979 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_34.bal @@ -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; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_34.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_34.xml new file mode 100644 index 000000000000..a2e5239cc36f --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_34.xml @@ -0,0 +1,8 @@ + + John + + Main Street + New York + + Software Engineer + From 88ce4689aa02e4b58558e45092b4c193fb200dd0 Mon Sep 17 00:00:00 2001 From: mindula Date: Thu, 13 Jun 2024 11:35:00 +0530 Subject: [PATCH 6/6] Refactor the code --- .../XMLToRecordConverter.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 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 1a9a9968f54e..91d52eb8816e 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 @@ -248,9 +248,10 @@ private static List 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() > 1 - || (xmlElementNode.getAttributes().getLength() == 1 - && !XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix()))) { + 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); } @@ -322,9 +323,11 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole } } } - if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) { - if (xmlElement.getAttributes().getLength() == 1 && xmlElement.getAttributes().item(0).getPrefix() != null - && XMLNS_PREFIX.equals(xmlElement.getAttributes().item(0).getPrefix())) { + 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()); @@ -334,7 +337,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole 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 && !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())) { @@ -468,9 +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 || - (xmlElementNode.getAttributes().getLength() == 1 - && XMLNS_PREFIX.equals(xmlElementNode.getAttributes().item(0).getPrefix())))) { + 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