poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1538044 - in /poi/trunk: src/ooxml/java/org/apache/poi/xwpf/extractor/ src/ooxml/testcases/org/apache/poi/xwpf/extractor/ src/scratchpad/testcases/org/apache/poi/hwpf/extractor/ test-data/document/
Date Fri, 01 Nov 2013 19:43:46 GMT
Author: nick
Date: Fri Nov  1 19:43:46 2013
New Revision: 1538044

URL: http://svn.apache.org/r1538044
Log:
Fix bug #55733 - XWPFWordExtractor need
s to handle .docx files with neither headers nor footers

Added:
    poi/trunk/test-data/document/55733.docx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java?rev=1538044&r1=1538043&r2=1538044&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java Fri Nov
 1 19:43:46 2013
@@ -17,7 +17,6 @@
 package org.apache.poi.xwpf.extractor;
 
 import java.io.IOException;
-import java.util.Iterator;
 import java.util.List;
 
 import org.apache.poi.POIXMLDocument;
@@ -34,7 +33,6 @@ import org.apache.poi.xwpf.usermodel.XWP
 import org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 import org.apache.poi.xwpf.usermodel.XWPFRelation;
-import org.apache.poi.xwpf.usermodel.XWPFRun;
 import org.apache.poi.xwpf.usermodel.XWPFSDT;
 import org.apache.poi.xwpf.usermodel.XWPFTable;
 import org.apache.poi.xwpf.usermodel.XWPFTableCell;
@@ -85,24 +83,24 @@ public class XWPFWordExtractor extends P
 		System.out.println(extractor.getText());
 	}
 	
-	public String getText() {
-		StringBuffer text = new StringBuffer();
-		XWPFHeaderFooterPolicy hfPolicy = document.getHeaderFooterPolicy();
+    public String getText() {
+        StringBuffer text = new StringBuffer();
+        XWPFHeaderFooterPolicy hfPolicy = document.getHeaderFooterPolicy();
+
+        // Start out with all headers
+        extractHeaders(text, hfPolicy);
+
+        // Process all body elements
+        for (IBodyElement e : document.getBodyElements()){
+        	appendBodyElementText(text, e);
+        	text.append('\n');
+        }
 
-		// Start out with all headers
-		extractHeaders(text, hfPolicy);
-		
-		// body elements
-      for (IBodyElement e : document.getBodyElements()){
-         appendBodyElementText(text, e);
-         text.append('\n');
-     }
-		
-		// Finish up with all the footers
-		extractFooters(text, hfPolicy);
-		
-		return text.toString();
-	}
+        // Finish up with all the footers
+        extractFooters(text, hfPolicy);
+
+        return text.toString();
+    }
 
    public void appendBodyElementText(StringBuffer text, IBodyElement e){
       if (e instanceof XWPFParagraph){
@@ -178,6 +176,8 @@ public class XWPFWordExtractor extends P
    }
    
 	private void extractFooters(StringBuffer text, XWPFHeaderFooterPolicy hfPolicy) {
+		if (hfPolicy == null) return;
+		
 		if(hfPolicy.getFirstPageFooter() != null) {
 			text.append( hfPolicy.getFirstPageFooter().getText() );
 		}
@@ -190,6 +190,8 @@ public class XWPFWordExtractor extends P
 	}
 
 	private void extractHeaders(StringBuffer text, XWPFHeaderFooterPolicy hfPolicy) {
+		if (hfPolicy == null) return;
+		
 		if(hfPolicy.getFirstPageHeader() != null) {
 			text.append( hfPolicy.getFirstPageHeader().getText() );
 		}

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java?rev=1538044&r1=1538043&r2=1538044&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
(original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
Fri Nov  1 19:43:46 2013
@@ -351,5 +351,16 @@ public class TestXWPFWordExtractor exten
             assertEquals("controlled content loading-"+targ, true, hit);
         }
         assertEquals("controlled content loading hit count", targs.length, hits);
+        ex.close();
+    }
+
+    /** No Header or Footer in document */
+    public void testBug55733() throws Exception {
+        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("55733.docx");
+    	XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
+
+        // Check it gives text without error
+        extractor.getText();
+        extractor.close();
     }
 }

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java?rev=1538044&r1=1538043&r2=1538044&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractorBugs.java
Fri Nov  1 19:43:46 2013
@@ -45,5 +45,4 @@ public final class TestWordExtractorBugs
         // Check it gives text without error
         extractor.getText();
     }
-    
 }

Added: poi/trunk/test-data/document/55733.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/55733.docx?rev=1538044&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/55733.docx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message