From d4c74a6c9eb614fd8820606c81be39f65d7e5363 Mon Sep 17 00:00:00 2001 From: Abhishek Shekhar Date: Mon, 8 Jan 2018 21:26:29 +0530 Subject: Utility Methods: clean xml of all empty nodes Change-Id: I29e26659da6d031237dffe940373515ccaa645c8 Issue-ID: SO-377 Signed-off-by: Abhishek Shekhar --- .../mso/bpmn/common/scripts/MsoUtils.groovy | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'bpmn/MSOCommonBPMN') 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) + } } -- cgit 1.2.3-korg