Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option to remove attributes and attribute annotation in XML to record conversion #42989

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why choose the name withoutAttributes instead of withAttributes ?

Copy link
Contributor

@prakanth97 prakanth97 Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like, we are loosing consistency with withNameSpaces parameter name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added withoutAttributes because the default behaviour includes attributes, and the checkbox we will show in the UI will be labeled Without Attributes or Remove Attributes. I thought changing the option to withAttributes might confuse the tooling team.

boolean withoutAttributeAnnot) {
Map<String, NonTerminalNode> recordToTypeDescNodes = new LinkedHashMap<>();
Map<String, AnnotationNode> recordToAnnotationNodes = new LinkedHashMap<>();
Map<String, Element> recordToElementNodes = new LinkedHashMap<>();
Expand All @@ -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);
Expand Down Expand Up @@ -187,15 +189,16 @@ 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,
Map<String, NonTerminalNode> recordToTypeDescNodes,
Map<String, AnnotationNode> recordToAnnotationsNodes,
Map<String, Element> recordToElementNodes,
List<DiagnosticMessage> 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);
Expand All @@ -204,7 +207,7 @@ private static void generateRecords(Element xmlElement, boolean isClosed,

List<Node> recordFields = getRecordFieldsForXMLElement(xmlElement, isClosed, recordToTypeDescNodes,
recordToAnnotationsNodes, recordToElementNodes, diagnosticMessages, textFieldName,
withNameSpace);
withNameSpace, withoutAttributes, withoutAttributeAnnot);
if (recordToTypeDescNodes.containsKey(xmlNodeName)) {
RecordTypeDescriptorNode previousRecordTypeDescriptorNode =
(RecordTypeDescriptorNode) recordToTypeDescNodes.get(xmlNodeName);
Expand Down Expand Up @@ -237,7 +240,8 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
Map<String, AnnotationNode> recordToAnnotationNodes,
Map<String, Element> recordToElementNodes,
List<DiagnosticMessage> diagnosticMessages,
String textFieldName, boolean withNameSpace) {
String textFieldName, boolean withNameSpace,
boolean withoutAttributes, boolean withoutAttributeAnnot) {
List<Node> recordFields = new ArrayList<>();

String xmlNodeName = xmlElement.getNodeName();
Expand All @@ -249,16 +253,17 @@ private static List<Node> 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<String, Boolean> 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())
Expand Down Expand Up @@ -315,18 +320,18 @@ private static List<Node> 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;
Expand All @@ -345,7 +350,7 @@ private static List<Node> 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);
}
}
Expand Down Expand Up @@ -467,7 +472,8 @@ private static List<Node> updateRecordFields(Map<String, RecordFieldNode> 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 =
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -520,7 +527,8 @@ private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean wi
}
annotations.add(getXMLAttributeNode());
NodeList<AnnotationNode> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public CompletableFuture<XMLToRecordResponse> 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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -65,4 +70,12 @@ public String getTextFieldName() {
public boolean getIsWithNameSpace() {
return withNameSpace;
}

public boolean getWithoutAttributes() {
return withoutAttributes;
}

public boolean getWithoutAttributeAnnot() {
return withoutAttributeAnnot;
}
}
Loading