From: Abhishek Shekhar Date: Mon, 8 Jan 2018 15:56:29 +0000 (+0530) Subject: Utility Methods: clean xml of all empty nodes X-Git-Tag: v1.2.1~606^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F27647%2F1;p=so.git Utility Methods: clean xml of all empty nodes Change-Id: I29e26659da6d031237dffe940373515ccaa645c8 Issue-ID: SO-377 Signed-off-by: Abhishek Shekhar --- diff --git a/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/MsoUtils.groovy b/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/MsoUtils.groovy index 06992455a2..719aeb837f 100644 --- a/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/MsoUtils.groovy +++ b/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/MsoUtils.groovy @@ -986,4 +986,31 @@ class MsoUtils { return requestId } + + /** + * Remove all the empty nodes and attributes from the within the given node + * @param node + * @return true if all empty nodes and attributes were removed. + */ + public boolean cleanNode( Node node ) { + node.attributes().with { a -> + a.findAll { !it.value }.each { a.remove( it.key ) } + } + node.children().with { kids -> + kids.findAll { it instanceof Node ? !cleanNode( it ) : false } + .each { kids.remove( it ) } + } + node.attributes() || node.children() || node.text() + } + + /** + * + * @param xml + * @return String representation of xml after removing the empty nodes and attributes + */ + public String cleanNode(String xmlString) { + def xml = new XmlParser(false, false).parseText(xmlString) + cleanNode(xml) + return XmlUtil.serialize(xml) + } }