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 380d2b1c2bf4..5e3dd99c406e 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 @@ -97,7 +97,8 @@ private XMLToRecordConverter() {} public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, boolean forceFormatRecordFields, - String textFieldName, boolean withNameSpaces) { + String textFieldName, boolean withNameSpaces, boolean withoutAttributes, + boolean withoutAttributeAnnot) { Map recordToTypeDescNodes = new LinkedHashMap<>(); Map recordToAnnotationNodes = new LinkedHashMap<>(); Map recordToElementNodes = new LinkedHashMap<>(); @@ -116,7 +117,8 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD Element rootElement = doc.getDocumentElement(); generateRecords(rootElement, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, - recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces); + recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces, withoutAttributes, + withoutAttributeAnnot); } catch (ParserConfigurationException parserConfigurationException) { DiagnosticMessage message = DiagnosticMessage.xmlToRecordConverter100(null); diagnosticMessages.add(message); @@ -187,7 +189,7 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD */ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, boolean forceFormatRecordFields) { - return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true); + return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true, false, false); } private static void generateRecords(Element xmlElement, boolean isClosed, @@ -195,7 +197,8 @@ private static void generateRecords(Element xmlElement, boolean isClosed, Map recordToAnnotationsNodes, Map recordToElementNodes, List diagnosticMessages, - String textFieldName, boolean withNameSpace) { + String textFieldName, boolean withNameSpace, boolean withoutAttributes, + boolean withoutAttributeAnnot) { Token recordKeyWord = AbstractNodeFactory.createToken(SyntaxKind.RECORD_KEYWORD); Token bodyStartDelimiter = AbstractNodeFactory.createToken(isClosed ? SyntaxKind.OPEN_BRACE_PIPE_TOKEN : SyntaxKind.OPEN_BRACE_TOKEN); @@ -204,7 +207,7 @@ private static void generateRecords(Element xmlElement, boolean isClosed, List recordFields = getRecordFieldsForXMLElement(xmlElement, isClosed, recordToTypeDescNodes, recordToAnnotationsNodes, recordToElementNodes, diagnosticMessages, textFieldName, - withNameSpace); + withNameSpace, withoutAttributes, withoutAttributeAnnot); if (recordToTypeDescNodes.containsKey(xmlNodeName)) { RecordTypeDescriptorNode previousRecordTypeDescriptorNode = (RecordTypeDescriptorNode) recordToTypeDescNodes.get(xmlNodeName); @@ -237,7 +240,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole Map recordToAnnotationNodes, Map recordToElementNodes, List diagnosticMessages, - String textFieldName, boolean withNameSpace) { + String textFieldName, boolean withNameSpace, + boolean withoutAttributes, boolean withoutAttributeAnnot) { List recordFields = new ArrayList<>(); String xmlNodeName = xmlElement.getNodeName(); @@ -249,16 +253,17 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole Element xmlElementNode = (Element) xmlNode; boolean isLeafXMLElementNode = isLeafXMLElementNode(xmlElementNode); NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes(); - if (!isLeafXMLElementNode || xmlAttributesMap.getLength() > 1 + if (!isLeafXMLElementNode || (!withoutAttributes && (xmlAttributesMap.getLength() > 1 || (xmlAttributesMap.getLength() == 1 - && !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) { + && !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))))) { generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, - recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace); + recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace, withoutAttributes, + withoutAttributeAnnot); } Map prefixMap = hasMultipleFieldsWithSameName(xmlNodeList, xmlElementNode.getLocalName()); RecordFieldNode recordField = getRecordField(xmlElementNode, false, withNameSpace, - prefixMap.size() > 1); + prefixMap.size() > 1, withoutAttributes); if (withNameSpace && xmlElementNode.getPrefix() != null) { int indexOfRecordFieldNode = IntStream.range(0, recordFields.size()) @@ -315,18 +320,18 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole AnnotationNode xmlNSNode = getXMLNamespaceNode(prefix, xmlNode.getNodeValue()); recordToAnnotationNodes.put(xmlNodeName, xmlNSNode); } else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix()) - && !XMLNS_PREFIX.equals(xmlNode.getLocalName())) { + && !XMLNS_PREFIX.equals(xmlNode.getLocalName()) && !withoutAttributes) { if (elementNames.contains(xmlNode.getNodeName())) { continue; } - Node recordField = getRecordField(xmlNode, withNameSpace); + Node recordField = getRecordField(xmlNode, withNameSpace, withoutAttributeAnnot); recordFields.add(recordField); } } } int attributeLength = xmlElement.getAttributes().getLength(); org.w3c.dom.Node attributeItem = xmlElement.getAttributes().item(0); - if (isLeafXMLElementNode(xmlElement) && attributeLength > 0) { + if (isLeafXMLElementNode(xmlElement) && attributeLength > 0 && !withoutAttributes) { if (attributeLength == 1 && attributeItem.getPrefix() != null && XMLNS_PREFIX.equals(attributeItem.getPrefix())) { return recordFields; @@ -345,7 +350,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE && !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix()) && !XMLNS_PREFIX.equals(xmlAttributeNode.getLocalName())) { - Node recordField = getRecordField(xmlAttributeNode, withNameSpace); + Node recordField = getRecordField(xmlAttributeNode, withNameSpace, withoutAttributeAnnot); recordFields.add(recordField); } } @@ -467,7 +472,8 @@ private static List updateRecordFields(Map previo } private static RecordFieldNode getRecordField(Element xmlElementNode, boolean isOptionalField, - boolean withNameSpace, boolean sameFieldExists) { + boolean withNameSpace, boolean sameFieldExists, + boolean withoutAttributes) { Token typeName; Token questionMarkToken = AbstractNodeFactory.createToken(SyntaxKind.QUESTION_MARK_TOKEN); IdentifierToken fieldName = @@ -478,7 +484,7 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes(); if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 || (xmlAttributesMap.getLength() == 1 - && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())))) { + && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())) || withoutAttributes)) { typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue()); } else { // At the moment all are considered as Objects here @@ -507,7 +513,8 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is metadataNode, null, fieldTypeName, fieldName, optionalFieldToken, semicolonToken); } - private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withNamespace) { + private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withNamespace, + boolean withoutAttributeAnnot) { Token typeName = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD); TypeDescriptorNode fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(typeName.kind(), typeName); IdentifierToken fieldName = @@ -520,7 +527,8 @@ private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean wi } annotations.add(getXMLAttributeNode()); NodeList annotationNodes = NodeFactory.createNodeList(annotations); - MetadataNode metadataNode = NodeFactory.createMetadataNode(null, annotationNodes); + MetadataNode metadataNode = withoutAttributeAnnot ? null : + NodeFactory.createMetadataNode(null, annotationNodes); if (xmlAttributeNode.getPrefix() != null && xmlAttributeNode.getPrefix().equals(XMLNS_PREFIX)) { diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java index c0f7b3782040..8e0b5252eff9 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java @@ -48,9 +48,11 @@ public CompletableFuture convert(XMLToRecordRequest request boolean forceFormatRecordFields = request.getForceFormatRecordFields(); String textFieldName = request.getTextFieldName(); boolean withNameSpace = request.getIsWithNameSpace(); + boolean withoutAttributes = request.getWithoutAttributes(); + boolean withoutAttributeAnnot = request.getWithoutAttributeAnnot(); return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, - textFieldName, withNameSpace); + textFieldName, withNameSpace, withoutAttributes, withoutAttributeAnnot); }); } diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java index be28d7cadfb4..46b0607c2a6c 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java @@ -31,15 +31,20 @@ public class XMLToRecordRequest { private final boolean forceFormatRecordFields; private final String textFieldName; private final boolean withNameSpace; + private final boolean withoutAttributes; + private final boolean withoutAttributeAnnot; public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace) { + boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace, + boolean withoutAttributes, boolean withoutAttributeAnnot) { this.xmlValue = xmlValue; this.isRecordTypeDesc = isRecordTypeDesc; this.isClosed = isClosed; this.forceFormatRecordFields = forceFormatRecordFields; this.textFieldName = textFieldName; this.withNameSpace = withNameSpace; + this.withoutAttributes = withoutAttributes; + this.withoutAttributeAnnot = withoutAttributeAnnot; } public String getXmlValue() { @@ -65,4 +70,12 @@ public String getTextFieldName() { public boolean getIsWithNameSpace() { return withNameSpace; } + + public boolean getWithoutAttributes() { + return withoutAttributes; + } + + public boolean getWithoutAttributeAnnot() { + return withoutAttributeAnnot; + } } 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 70b9a80c073c..f94e22944551 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 @@ -229,6 +229,20 @@ public class XMLToRecordConverterTests { private final Path sample37Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_37.bal"); + private final Path sample38XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_38.xml"); + private final Path sample39Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_39.bal"); + private final Path sample40Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_40.bal"); + + private final Path sample39XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_39.xml"); + private final Path sample41Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_41.bal"); + private final Path sample42Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_42.bal"); + private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -407,7 +421,7 @@ public void testWithMultipleAttributes() throws IOException { public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException { String xmlFileContent = Files.readString(sample19XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "amount", false).getCodeBlock().replaceAll("\\s+", ""); + "amount", false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -416,7 +430,7 @@ public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException { String xmlFileContent = Files.readString(sample20XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -425,7 +439,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throw public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException { String xmlFileContent = Files.readString(sample21XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -434,7 +448,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws I public void testXMLWithoutNamespacePrefix() throws IOException { String xmlFileContent = Files.readString(sample22XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -443,7 +457,7 @@ public void testXMLWithoutNamespacePrefix() throws IOException { public void testXMLWithConflictingElementAndAttributeNames() throws IOException { String xmlFileContent = Files.readString(sample23XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -452,7 +466,7 @@ public void testXMLWithConflictingElementAndAttributeNames() throws IOException public void testXMLWithoutNamespaces() throws IOException { String xmlFileContent = Files.readString(sample24XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample24Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -462,7 +476,7 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample0XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -476,7 +490,8 @@ public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace() Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample25XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", + false, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -497,7 +512,7 @@ public void testXMLWithMultipleAttributes() throws IOException { public void testXMLWithoutNamespaceAnnotations() throws IOException { String xmlFileContent = Files.readString(sample27XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample27Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -506,7 +521,7 @@ public void testXMLWithoutNamespaceAnnotations() throws IOException { public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { String xmlFileContent = Files.readString(sample28XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample28Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -515,7 +530,7 @@ public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { public void testXMLWithMultipleNamespaces2() throws IOException { String xmlFileContent = Files.readString(sample29XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample29Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -524,7 +539,7 @@ public void testXMLWithMultipleNamespaces2() throws IOException { public void testXMLWithMultipleNamespaces3() throws IOException { String xmlFileContent = Files.readString(sample30XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample30Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -533,7 +548,7 @@ public void testXMLWithMultipleNamespaces3() throws IOException { public void testXMLWithoutNamespaceAnnotation() throws IOException { String xmlFileContent = Files.readString(sample31XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample31Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -542,7 +557,7 @@ public void testXMLWithoutNamespaceAnnotation() throws IOException { public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOException { String xmlFileContent = Files.readString(sample32XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample32Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -551,7 +566,7 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept public void textXMLWithDefaultValueNode() throws IOException { String xmlFileContent = Files.readString(sample33XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -560,7 +575,7 @@ public void textXMLWithDefaultValueNode() throws IOException { public void textXMLWithDefaultValueNode2() throws IOException { String xmlFileContent = Files.readString(sample34XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -569,7 +584,7 @@ public void textXMLWithDefaultValueNode2() throws IOException { public void textXMLWithDefaultNamespace() throws IOException { String xmlFileContent = Files.readString(sample35XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample35Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -578,7 +593,7 @@ public void textXMLWithDefaultNamespace() throws IOException { public void textXMLWithDefaultNamespace2() throws IOException { String xmlFileContent = Files.readString(sample36XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample36Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -587,8 +602,44 @@ public void textXMLWithDefaultNamespace2() throws IOException { public void textXMLWithDefaultNamespace3() throws IOException { String xmlFileContent = Files.readString(sample37XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "textXMLWithoutAttributes") + public void textXMLWithoutAttributes() throws IOException { + String xmlFileContent = Files.readString(sample38XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + null, true, true, false).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample39Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutAttributeAnnotation") + public void textXMLWithoutAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample38XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample40Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutMultipleAttributeAnnotation") + public void textXMLWithoutMultipleAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample39XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample41Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithNamespacesAndWithoutAttributeAnnotation") + public void textXMLWithNamespacesAndWithoutAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample39XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", true, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample42Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_39.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_39.bal new file mode 100644 index 000000000000..7e40d68e6076 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_39.bal @@ -0,0 +1,11 @@ +type Book record { + string title; + string author; + int year; + decimal price; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_40.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_40.bal new file mode 100644 index 000000000000..30d9306ba750 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_40.bal @@ -0,0 +1,22 @@ +type Title record { + string __text; + string lang; +}; + +type Price record { + decimal __text; + string currency; +}; + +type Book record { + Title title; + string author; + int year; + Price price; + string category; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_41.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_41.bal new file mode 100644 index 000000000000..de7054486b68 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_41.bal @@ -0,0 +1,37 @@ +type Book_Title record { + string __text; + string edition; + string format; + string lang; + string pages; +}; + +type Book_Price record { + decimal __text; + string currency; +}; + +type Book_Book record { + Book_Title title; + string author; + string publisher; + string publish_date; + int isbn; + Book_Price price; + string available; + string id; + string shelf; +}; + +type Books record { + string genre; + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + string established; + string genre; + string location; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_42.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_42.bal new file mode 100644 index 000000000000..87853261f06e --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_42.bal @@ -0,0 +1,44 @@ +type Book_Title record { + string __text; + string edition; + string format; + string lang; + string pages; +}; + +type Book_Price record { + decimal __text; + string currency; +}; + +type Book_Book record { + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Title title; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string author; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string publisher; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string publish_date; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + int isbn; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Price price; + string available; + string id; + string shelf; +}; + +type Books record { + string genre; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + string established; + string genre; + string location; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_38.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_38.xml new file mode 100644 index 000000000000..fb2f40571d64 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_38.xml @@ -0,0 +1,8 @@ + + + Harry Potter + J.K. Rowling + 2005 + 29.99 + + diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_39.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_39.xml new file mode 100644 index 000000000000..c0f3d434cd84 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_39.xml @@ -0,0 +1,13 @@ + + + Programming + + XML Developer's Guide + John Doe + Tech Press + 2000-10-01 + 1234567890 + 49.99 + + +