Skip to content

Commit

Permalink
Merge pull request ballerina-platform#43057 from mindula/remove_defau…
Browse files Browse the repository at this point in the history
…lt_namespaces_master

[Master] Remove adding default namespaces in xml to record generation
  • Loading branch information
KavinduZoysa authored Aug 13, 2024
2 parents 3ee80e7 + c29b5a7 commit 0e91626
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,18 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
if (((xmlNode.getPrefix() == null && XMLNS_PREFIX.equals(xmlNode.getLocalName())) ||
(XMLNS_PREFIX.equals(xmlNode.getPrefix()) &&
xmlNode.getLocalName().equals(xmlElement.getPrefix()))) && withNameSpace) {
String prefix = null;
String prefix = xmlElement.getPrefix();
if (xmlElement.getPrefix() != null && xmlElement.getPrefix().equals(xmlNode.getLocalName())) {
prefix = xmlNode.getLocalName();
}
AnnotationNode xmlNSNode = getXMLNamespaceNode(prefix, xmlNode.getNodeValue());
recordToAnnotationNodes.put(xmlNodeName, xmlNSNode);
} else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix())) {
} else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix())
&& !XMLNS_PREFIX.equals(xmlNode.getLocalName())) {
if (elementNames.contains(xmlNode.getNodeName())) {
continue;
}
Node recordField = getRecordField(xmlNode);
Node recordField = getRecordField(xmlNode, withNameSpace);
recordFields.add(recordField);
}
}
Expand All @@ -331,17 +332,20 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
return recordFields;
}
Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue());
TypeDescriptorNode fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(
fieldType.kind(), fieldType);
IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(textFieldName == null ?
escapeIdentifier("#content") : textFieldName);
Token semicolon = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null, fieldType,
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null, fieldTypeName,
fieldName, null, semicolon);
recordFields.add(recordFieldNode);
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())) {
Node recordField = getRecordField(xmlAttributeNode);
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getLocalName())) {
Node recordField = getRecordField(xmlAttributeNode, withNameSpace);
recordFields.add(recordField);
}
}
Expand Down Expand Up @@ -503,15 +507,20 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is
metadataNode, null, fieldTypeName, fieldName, optionalFieldToken, semicolonToken);
}

private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode) {
private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withNamespace) {
Token typeName = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD);
TypeDescriptorNode fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(typeName.kind(), typeName);
IdentifierToken fieldName =
AbstractNodeFactory.createIdentifierToken(escapeIdentifier(xmlAttributeNode.getLocalName()));
Token equalToken = AbstractNodeFactory.createToken(SyntaxKind.EQUAL_TOKEN);
Token semicolonToken = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);
NodeList<AnnotationNode> annotations = AbstractNodeFactory.createNodeList(getXMLAttributeNode());
MetadataNode metadataNode = NodeFactory.createMetadataNode(null, annotations);
List<AnnotationNode> annotations = new ArrayList<>();
if (withNamespace && xmlAttributeNode.getPrefix() != null && xmlAttributeNode.getNamespaceURI() != null) {
annotations.add(getXMLNamespaceNode(xmlAttributeNode.getPrefix(), xmlAttributeNode.getNamespaceURI()));
}
annotations.add(getXMLAttributeNode());
NodeList<AnnotationNode> annotationNodes = NodeFactory.createNodeList(annotations);
MetadataNode metadataNode = NodeFactory.createMetadataNode(null, annotationNodes);

if (xmlAttributeNode.getPrefix() != null &&
xmlAttributeNode.getPrefix().equals(XMLNS_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public class XMLToRecordConverterTests {
private final Path sample34Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_34.bal");

private final Path sample35XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_35.xml");
private final Path sample35Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_35.bal");

private final Path sample36XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_36.xml");
private final Path sample36Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_36.bal");

private final Path sample37XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_37.xml");
private final Path sample37Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_37.bal");

private static final String XMLToRecordServiceEP = "xmlToRecord/convert";


Expand Down Expand Up @@ -549,4 +564,31 @@ public void textXMLWithDefaultValueNode2() throws IOException {
String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultNamespace")
public void textXMLWithDefaultNamespace() throws IOException {
String xmlFileContent = Files.readString(sample35XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"__text", true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample35Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultNamespace2")
public void textXMLWithDefaultNamespace2() throws IOException {
String xmlFileContent = Files.readString(sample36XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"__text", true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample36Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultNamespace3")
public void textXMLWithDefaultNamespace3() throws IOException {
String xmlFileContent = Files.readString(sample37XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"__text", true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ type Customer record {
string loyalty;
@xmldata:Attribute
string optedInNewsLetter;
@xmldata:Attribute
string 'xmlns;
};

type Item record {
Expand All @@ -21,21 +19,15 @@ type Item record {

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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
type Ns2_Person record {
int __text;
@xmldata:Attribute
string age;
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
@xmldata:Attribute
string name;
};

type Data record {
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
Ns2_Person Person;
@xmldata:Namespace {
uri: "example.com"
}
string Message;
};

@xmldata:Namespace {
uri: "example.com"
}
type AQ record {
@xmldata:Namespace {
uri: "example.com"
}
Data Data;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@xmldata:Namespace {
prefix: "ns2",
uri: "example3.com"
}
type Ns2_Person record {
int __text;
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
@xmldata:Attribute
string age;
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
@xmldata:Attribute
string name;
};

type Ns1_Details record {
@xmldata:Namespace {
prefix: "ns1",
uri: "example1.com"
}
string Info;
@xmldata:Namespace {
prefix: "ns1",
uri: "example1.com"
}
string Status;
};

type Data record {
@xmldata:Namespace {
prefix: "ns2",
uri: "example2.com"
}
Ns2_Person Person;
@xmldata:Namespace {
uri: "example.com"
}
string C;
@xmldata:Namespace {
prefix: "ns1",
uri: "example1.com"
}
Ns1_Details Details;
@xmldata:Namespace {
uri: "example.com"
}
string B;
};

@xmldata:Namespace {
uri: "example.com"
}
type Test record {
@xmldata:Namespace {
uri: "example.com"
}
Data Data;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@xmldata:Namespace {
prefix: "ns1",
uri: "example3.com"
}
type Ns1_Person record {
int __text;
@xmldata:Attribute
string age;
@xmldata:Attribute
string city;
@xmldata:Namespace {
prefix: "ns1",
uri: "example1.com"
}
@xmldata:Attribute
string name;
};

type Ns2_Details record {
@xmldata:Namespace {
prefix: "ns2",
uri: "example.com"
}
string Info;
@xmldata:Namespace {
prefix: "ns2",
uri: "example.com"
}
string Status;
@xmldata:Attribute
string number;
};

type Data record {
@xmldata:Namespace {
prefix: "ns1",
uri: "example1.com"
}
Ns1_Person Person;
@xmldata:Namespace {
uri: "example2.com"
}
string Name;
@xmldata:Namespace {
prefix: "ns2",
uri: "example.com"
}
Ns2_Details Details;
@xmldata:Namespace {
uri: "example2.com"
}
string Description;
};

@xmldata:Namespace {
uri: "example2.com"
}
type Countries record {
@xmldata:Namespace {
uri: "example2.com"
}
Data Data;
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
type Ns1_Element1 record {
@xmldata:Namespace {
prefix: "ns1",
uri: "http://namespace1.com"
}
@xmldata:Attribute
string attribute1;
@xmldata:Namespace {
prefix: "ns2",
uri: "http://namespace2.com"
}
@xmldata:Attribute
string attribute2;
};

type Ns2_Null record {
@xmldata:Namespace {
prefix: "xsi",
uri: "http://www.w3.org/2001/XMLSchema-instance"
}
@xmldata:Attribute
string nil;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<AQ xmlns="example.com">
<Data xmlns:ns1="example1.com" xmlns:ns2="example2.com">
<ns2:Person xmlns="example2.com" ns2:name="John" age="25">1</ns2:Person>
<Message>Test</Message>
</Data>
</AQ>
11 changes: 11 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Test xmlns="example.com">
<Data xmlns:ns1="example1.com" xmlns:ns2="example2.com">
<ns2:Person xmlns="example3.com" ns2:name="Alice" ns2:age="30">2</ns2:Person>
<C>Example</C>
<ns1:Details>
<ns1:Info>Additional Info</ns1:Info>
<ns1:Status>Active</ns1:Status>
</ns1:Details>
<B>Sample</B>
</Data>
</Test>
11 changes: 11 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_37.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Countries xmlns="example2.com">
<Data xmlns:ns2="example.com" xmlns:ns1="example1.com">
<ns1:Person xmlns="example3.com" ns1:name="Emily" age="32" city="New York">2</ns1:Person>
<Name>Data Sample</Name>
<ns2:Details number="1234">
<ns2:Info>Additional Information</ns2:Info>
<ns2:Status>Active</ns2:Status>
</ns2:Details>
<Description>Sample Text</Description>
</Data>
</Countries>

0 comments on commit 0e91626

Please sign in to comment.