Utility Methods: clean xml of all empty nodes 47/27647/1
authorAbhishek Shekhar <abhishek.shekhar1@amdocs.com>
Mon, 8 Jan 2018 15:56:29 +0000 (21:26 +0530)
committerAbhishek Shekhar <abhishek.shekhar1@amdocs.com>
Mon, 8 Jan 2018 15:57:43 +0000 (21:27 +0530)
Change-Id: I29e26659da6d031237dffe940373515ccaa645c8
Issue-ID: SO-377
Signed-off-by: Abhishek Shekhar <abhishek.shekhar1@amdocs.com>
bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/MsoUtils.groovy

index 0699245..719aeb8 100644 (file)
@@ -986,4 +986,31 @@ class MsoUtils {
 \r
                return requestId\r
        }\r
+\r
+       /**\r
+        * Remove all the empty nodes and attributes from the within the given node\r
+        * @param node\r
+        * @return true if all empty nodes and attributes were removed.\r
+        */\r
+       public boolean cleanNode( Node node ) {\r
+               node.attributes().with { a ->\r
+                       a.findAll { !it.value }.each { a.remove( it.key ) }\r
+               }\r
+               node.children().with { kids ->\r
+                       kids.findAll { it instanceof Node ? !cleanNode( it ) : false }\r
+                                       .each { kids.remove( it ) }\r
+               }\r
+               node.attributes() || node.children() || node.text()\r
+       }\r
+\r
+       /**\r
+        *\r
+        * @param xml\r
+        * @return String representation of xml after removing the empty nodes and attributes\r
+        */\r
+       public String cleanNode(String xmlString) {\r
+               def xml = new XmlParser(false, false).parseText(xmlString)\r
+               cleanNode(xml)\r
+               return XmlUtil.serialize(xml)\r
+       }\r
 }\r