Skip to content

Commit

Permalink
Merge pull request #42486 from mindula/xmlToRecord
Browse files Browse the repository at this point in the history
[Master] Add new options in xml conversion service
  • Loading branch information
LakshanWeerasinghe committed Apr 8, 2024
2 parents 55f7dc6 + bc0c5f8 commit 4196e06
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, NonTerminalNode> recordToTypeDescNodes = new LinkedHashMap<>();
Map<String, AnnotationNode> recordToAnnotationNodes = new LinkedHashMap<>();
Map<String, Element> recordToElementNodes = new LinkedHashMap<>();
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public CompletableFuture<XMLToRecordResponse> 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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";


Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
};
11 changes: 11 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library xmlns:book="http://example.com/books" genre="genre">
<books>
<genre>Programming</genre>
<book:book>
<book:title lang="en" edition="1st">XML Developer's Guide</book:title>
<book:author gender="male" nationality="American">Gambardella, Matthew</book:author>
<book:publish_date>2000-10-01</book:publish_date>
<book:description>An in-depth look at creating applications with XML.</book:description>
</book:book>
</books>
</library>

0 comments on commit 4196e06

Please sign in to comment.