Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mindula committed Aug 21, 2024
2 parents 2eec876 + 477b84f commit dd9a008
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ void shrink() {
if (this.compilationUnit != null) {
this.compilationUnit.topLevelNodes.clear();
}
this.syntaxTree = null;
this.moduleLoadRequests = null;
this.content = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ private void performCodeGen(boolean shrink) {
// collect compilation diagnostics
List<Diagnostic> moduleDiagnostics = new ArrayList<>();
for (ModuleContext moduleContext : pkgResolution.topologicallySortedModuleList()) {
if (shrink) {
ModuleContext.shrinkDocuments(moduleContext);
}
if (moduleContext.moduleId().packageId().equals(packageContext.packageId())) {
if (packageCompilation.diagnosticResult().hasErrors()) {
for (Diagnostic diagnostic : moduleContext.diagnostics()) {
Expand All @@ -184,9 +187,6 @@ private void performCodeGen(boolean shrink) {
}
}

if (shrink) {
ModuleContext.shrinkDocuments(moduleContext);
}
if (moduleContext.project().kind() == ProjectKind.BALA_PROJECT) {
moduleContext.cleanBLangPackage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,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())
&& !withoutAttributes) {
&& !XMLNS_PREFIX.equals(xmlNode.getLocalName()) && !withoutAttributes) {
if (elementNames.contains(xmlNode.getNodeName())) {
continue;
}
Node recordField = getRecordField(xmlNode, withoutAttributeAnnot);
Node recordField = getRecordField(xmlNode, withNameSpace, withoutAttributeAnnot);
recordFields.add(recordField);
}
}
Expand All @@ -337,17 +337,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, withoutAttributeAnnot);
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getLocalName())) {
Node recordField = getRecordField(xmlAttributeNode, withNameSpace, withoutAttributeAnnot);
recordFields.add(recordField);
}
}
Expand Down Expand Up @@ -510,15 +513,21 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is
metadataNode, null, fieldTypeName, fieldName, optionalFieldToken, semicolonToken);
}

private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withoutAttributeAnnot) {
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 =
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 = withoutAttributeAnnot ? null : 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 = withoutAttributeAnnot ? null : 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 final Path sample38XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_38.xml");
private final Path sample39Bal = RES_DIR.resolve(BAL_DIR)
Expand Down Expand Up @@ -565,6 +580,33 @@ public void textXMLWithDefaultValueNode2() throws IOException {
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, false, false).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, false, false).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, 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);
Expand Down
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>
Loading

0 comments on commit dd9a008

Please sign in to comment.