]> source.dussan.org Git - poi.git/commitdiff
more compact HTML in Word-to-HTML converter
authorSergey Vladimirov <sergey@apache.org>
Thu, 21 Jul 2011 04:25:39 +0000 (04:25 +0000)
committerSergey Vladimirov <sergey@apache.org>
Thu, 21 Jul 2011 04:25:39 +0000 (04:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1149035 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java

index 032d732c11ca675678f7f83a95d4d9e5d8360baa..3fff0aa4bc8be31b3c24c15f833aebe61911ab1c 100644 (file)
@@ -45,6 +45,8 @@ import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 
 import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH;
@@ -523,6 +525,32 @@ public class WordToHtmlConverter extends AbstractWordConverter
                     htmlDocumentFacade.getOrCreateCssClass(
                             pElement.getTagName(), "p", style.toString() ) );
 
+        {
+            // compact spans
+            NodeList childNodes = pElement.getChildNodes();
+            for ( int i = 0; i < childNodes.getLength() - 1; i++ )
+            {
+                Node child1 = childNodes.item( i );
+                Node child2 = childNodes.item( i + 1 );
+                if ( child1.getNodeType() != Node.ELEMENT_NODE
+                        || child2.getNodeType() != Node.ELEMENT_NODE
+                        || !WordToHtmlUtils.equals( "span",
+                                ( (Element) child1 ).getTagName() )
+                        || !WordToHtmlUtils.equals( "span",
+                                ( (Element) child2 ).getTagName() )
+                        || !WordToHtmlUtils.equals(
+                                ( (Element) child1 ).getAttribute( "class" ),
+                                ( (Element) child2 ).getAttribute( "class" ) ) )
+                    continue;
+
+                // merge
+                while ( child2.getChildNodes().getLength() > 0 )
+                    child1.appendChild( child2.getFirstChild() );
+                child2.getParentNode().removeChild( child2 );
+                i--;
+            }
+        }
+
         return;
     }