Implement remove if empty paragraph.
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / docbook / DbkUtils.java
index 44346b25dbb669c5bfb1c88442509c2c25983d11..b654c73178646597e50badabdbe2c6e13989a725 100644 (file)
@@ -106,6 +106,32 @@ public class DbkUtils {
                return p;
        }
 
+       /**
+        * Removes a paragraph if it empty. The sesison is not saved.
+        * 
+        * @return true if the paragraph was empty and it was removed
+        */
+       public static boolean removeIfEmptyParagraph(Node node) {
+               try {
+                       if (isDbk(node, DbkType.para)) {
+                               NodeIterator nit = node.getNodes();
+                               if (!nit.hasNext())
+                                       return false;// log this unexpected situation?
+                               Node first = nit.nextNode();
+                               if (nit.hasNext())
+                                       return false;
+                               String str = JcrxApi.getXmlValue(first);
+                               if (str != null && str.trim().equals("")) {
+                                       node.remove();
+                                       return true;
+                               }
+                       }
+                       return false;
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot remove possibly empty paragraph", e);
+               }
+       }
+
        public static Node insertImageAfter(Node sibling) {
                try {