/*
 *  The MIT License (MIT)
 *
 *  Copyright (c) 2016 Lachlan Dowding
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package permafrost.tundra.xml.dom;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * A collection of convenience methods for working with org.w3c.dom.Element objects.
 */
public class ElementHelper {
    /**
     * Disallow instantiation of this class.
     */
    private ElementHelper() {}

    /**
     * Returns the concatenated content of the given Element from its TEXT_NODE and CDATA_SECTION_NODE children.
     *
     * @param element   The element to return the content of.
     * @return          The concatenated text content from the given element's TEXT_NODE and CDATA_SECTION_NODE
     *                  children.
     */
    public static String getContent(Element element) {
        String output = null;

        if (element != null && element.hasChildNodes()) {
            boolean hasContent = false;

            StringBuilder builder = new StringBuilder();

            for (Node child : Nodes.of(element.getChildNodes())) {
                switch(child.getNodeType()) {
                    case Node.CDATA_SECTION_NODE:
                    case Node.TEXT_NODE:
                        hasContent = true;
                        builder.append(child.getNodeValue());
                        break;
                    default:
                        // for non-text children, do nothing
                        break;
                }
            }

            if (hasContent) output = builder.toString();
        }

        return output;
    }
}

@@ -1836,16 +1836,20 @@
 ring get
+Text
 Content(
@@ -1879,93 +1879,96 @@
     
-String output = null;%0A%0A        if (element != null && element.hasChildNodes()) %7B%0A    
+if (element == null) return null;%0A%0A        StringBuilder builder = new StringBuilder();%0A
     
@@ -1978,24 +1978,28 @@
  boolean has
+Text
 Content = fa
@@ -2005,80 +2005,44 @@
 alse
-;%0A%0A            StringBuilder builder = new StringBuilder();%0A%0A   
+, hasMixedContent = false;%0A%0A
         
  for
@@ -2037,17 +2037,16 @@
         
- 
 for (Nod
@@ -2088,28 +2088,24 @@
 Nodes())) %7B%0A
-    
             
@@ -2142,36 +2142,32 @@
                 
-    
 case Node.CDATA_
@@ -2188,36 +2188,32 @@
                 
- 
-   
 case Node.TEXT_N
@@ -2241,15 +2241,15 @@
     
-    has
+hasText
 Cont
@@ -2260,20 +2260,16 @@
 = true;%0A
-    
         
@@ -2330,36 +2330,32 @@
                 
-    
 break;%0A         
@@ -2344,32 +2344,136 @@
   break;%0A       
+         case Node.ELEMENT_NODE:%0A                    hasMixedContent = true;%0A                    break;%0A
              def
@@ -2469,16 +2469,19 @@
         
+   
 default:
@@ -2481,20 +2481,16 @@
 efault:%0A
-    
         
@@ -2550,36 +2550,32 @@
                 
-    
 break;%0A         
@@ -2575,23 +2575,30 @@
         
+  %7D%0A
       
-%7D
+  %7D%0A
 %0A       
@@ -2594,24 +2594,181 @@
  %7D%0A%0A        
+String content = builder.toString();%0A        if (!hasTextContent %7C%7C (hasMixedContent && content.trim().equals(%22%22))) content = null;%0A%0A        return content;%0A
     %7D%0A%0A     
@@ -2770,60 +2770,750 @@
     
+/**%0A
      
-   if (hasContent) output = builder.toString();
+* Return true if the given element has any children which are also elements.%0A     *%0A     * @param element   The element to check for element children.%0A     * @return          True if the given element has children which are also elements.%0A     */%0A    public static boolean hasChildElements(Element element) %7B%0A        boolean hasChildElements = false;%0A%0A        if (element != null) %7B%0A            if (element.hasChildNodes()) %7B%0A                Nodes nodes = Nodes.of(element.getChildNodes());%0A                for (Node node : nodes) %7B%0A                    if (node.getNodeType() == Node.ELEMENT_NODE) %7B%0A                        hasChildElements = true;%0A                        break;%0A                    %7D%0A                %7D%0A            %7D
 %0A   
@@ -3539,14 +3539,24 @@
 urn 
-output
+hasChildElements
 ;%0A  

Refactor ElementHelper.getContent: rename to getTextContent, and return null when element has mixed content and the text content is only whitespace