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 29aaca85bbc5..405fdee21be3 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 @@ -93,9 +93,9 @@ private XMLToRecordConverter() {} private static final String XMLDATA = "xmldata"; private static final String COLON = ":"; - public static XMLToRecordResponse convertXMLToRecord(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean forceFormatRecordFields, - String textFieldName, boolean withNameSpaces) { + public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, + boolean forceFormatRecordFields, + String textFieldName, boolean withNameSpaces) { Map recordToTypeDescNodes = new LinkedHashMap<>(); Map recordToAnnotationNodes = new LinkedHashMap<>(); Map recordToElementNodes = new LinkedHashMap<>(); @@ -180,12 +180,12 @@ public static XMLToRecordResponse convertXMLToRecord(String xmlValue, boolean is * @param xmlValue The XML value to be converted to a record. * @param isRecordTypeDesc Whether the record is a type descriptor. * @param isClosed Whether the record is closed or not. - * @param textFieldName Whether to force format the result. + * @param forceFormatRecordFields Whether to force format the result. * @return {@link XMLToRecordResponse} The response object containing the converted record. */ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean textFieldName) { - return convertXMLToRecord(xmlValue, isRecordTypeDesc, isClosed, textFieldName, null, true); + boolean forceFormatRecordFields) { + return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true); } private static void generateRecords(Element xmlElement, boolean isClosed, 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 1aa46e494474..c0f7b3782040 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 @@ -46,8 +46,11 @@ public CompletableFuture convert(XMLToRecordRequest request boolean isRecordTypeDesc = request.getIsRecordTypeDesc(); boolean isClosed = request.getIsClosed(); boolean forceFormatRecordFields = request.getForceFormatRecordFields(); + String textFieldName = request.getTextFieldName(); + boolean withNameSpace = request.getIsWithNameSpace(); - return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields); + return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, + textFieldName, withNameSpace); }); } 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 f2b4384f0e1e..be28d7cadfb4 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 @@ -25,48 +25,44 @@ */ public class XMLToRecordRequest { - private String xmlValue; - private boolean isRecordTypeDesc; - private boolean isClosed; - private boolean forceFormatRecordFields; + private final String xmlValue; + private final boolean isRecordTypeDesc; + private final boolean isClosed; + private final boolean forceFormatRecordFields; + private final String textFieldName; + private final boolean withNameSpace; public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean forceFormatRecordFields) { + boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace) { this.xmlValue = xmlValue; this.isRecordTypeDesc = isRecordTypeDesc; this.isClosed = isClosed; this.forceFormatRecordFields = forceFormatRecordFields; + this.textFieldName = textFieldName; + this.withNameSpace = withNameSpace; } public String getXmlValue() { return xmlValue; } - public void setXmlValue(String xmlValue) { - this.xmlValue = xmlValue; - } - public boolean getIsRecordTypeDesc() { return isRecordTypeDesc; } - public void setIsRecordTypeDesc(boolean isRecordTypeDesc) { - this.isRecordTypeDesc = isRecordTypeDesc; - } - public boolean getIsClosed() { return isClosed; } - public void setIsClosed(boolean isClosed) { - this.isClosed = isClosed; - } - public boolean getForceFormatRecordFields() { return forceFormatRecordFields; } - public void setForceFormatRecordFields(boolean forceFormatRecordFields) { - this.forceFormatRecordFields = forceFormatRecordFields; + public String getTextFieldName() { + return textFieldName; + } + + public boolean getIsWithNameSpace() { + return withNameSpace; } } 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 1a713220b1fa..d85b701008a0 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 @@ -164,6 +164,11 @@ public class XMLToRecordConverterTests { private final Path sample24Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_24.bal"); + private final Path sample25XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_25.xml"); + private final Path sample25Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_25.bal"); + private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -341,7 +346,7 @@ public void testWithMultipleAttributes() throws IOException { @Test(description = "testXMLWithNamespacesWithoutNamespaceAnnotation") public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException { String xmlFileContent = Files.readString(sample19XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, "amount", false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -350,7 +355,7 @@ public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException @Test(description = "testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations") public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException { String xmlFileContent = Files.readString(sample20XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -359,7 +364,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throw @Test(description = "testXMLWithMultipleAttributesAndNamespacesWithAnnotations") public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException { String xmlFileContent = Files.readString(sample21XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, true).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -368,7 +373,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws I @Test(description = "testXMLWithoutNamespacePrefix") public void testXMLWithoutNamespacePrefix() throws IOException { String xmlFileContent = Files.readString(sample22XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, true).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -377,7 +382,7 @@ public void testXMLWithoutNamespacePrefix() throws IOException { @Test(description = "testXMLWithConflictingElementAndAttributeNames") public void testXMLWithConflictingElementAndAttributeNames() throws IOException { String xmlFileContent = Files.readString(sample23XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, true).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -386,7 +391,7 @@ public void testXMLWithConflictingElementAndAttributeNames() throws IOException @Test(description = "testXMLWithoutNamespaces") public void testXMLWithoutNamespaces() throws IOException { String xmlFileContent = Files.readString(sample24XML); - String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false, + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample24Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); @@ -397,11 +402,25 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample0XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample0Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "Test xml record request with text field name and without namespace") + public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace() + throws IOException, ExecutionException, InterruptedException { + Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); + String xmlValue = Files.readString(sample25XML); + + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false); + CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); + XMLToRecordResponse response = (XMLToRecordResponse) result.get(); + String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample25Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal new file mode 100644 index 000000000000..bcec66c509fb --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal @@ -0,0 +1,34 @@ +type Book_Title record { + string __text; + @xmldata:Attribute + string edition; + @xmldata:Attribute + string lang; +}; + +type Book_Author record { + string __text; + @xmldata:Attribute + string gender; + @xmldata:Attribute + string nationality; +}; + +type Book_Book record { + Book_Title title; + Book_Author author; + string publish_date; + string description; +}; + +type Books record { + string genre; + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + @xmldata:Attribute + string genre; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml new file mode 100644 index 000000000000..37f730ac3fe6 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml @@ -0,0 +1,11 @@ + + + Programming + + XML Developer's Guide + Gambardella, Matthew + 2000-10-01 + An in-depth look at creating applications with XML. + + +