]> source.dussan.org Git - poi.git/commitdiff
Fix bug #49936 - Handle HWPF documents with problematic HeaderStories better
authorNick Burch <nick@apache.org>
Fri, 17 Sep 2010 14:14:19 +0000 (14:14 +0000)
committerNick Burch <nick@apache.org>
Fri, 17 Sep 2010 14:14:19 +0000 (14:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@998146 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hwpf/usermodel/HeaderStories.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java
test-data/document/HeaderFooterProblematic.doc [new file with mode: 0644]

index 0da406cdc32092c35d2a27c9a7d28924f6ab92af..5038f8314580a6ead7bd64572f2798c56d6f5837 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="fix">49936 - Handle HWPF documents with problematic HeaderStories better</action>
            <action dev="poi-developers" type="fix">49933 - Support sections in Word 6 and Word 95 files (HWPFOldDocument)</action>
            <action dev="poi-developers" type="fix">49941 - Correctly handle space preservation of XSSFRichTextRuns when applying fonts to parts of the string</action>
            <action dev="poi-developers" type="fix">Correct XWPFRun detection of bold/italic in a paragraph with multiple runs of different styles</action>
index 4afaba9d1e302290225fb338e32f6d07f9422994..c0322115b31f1aa9a776093d4a80729861c48d92 100644 (file)
@@ -157,10 +157,18 @@ public final class HeaderStories {
                        // Empty story
                        return "";
                }
+               if(prop.getEnd() < prop.getStart()) {
+                  // Broken properties?
+                  return "";
+               }
+
+               // Ensure we're getting a sensible length
+               String rawText = headerStories.text();
+               int start = Math.min(prop.getStart(), rawText.length());
+               int end = Math.min(prop.getEnd(), rawText.length());
 
                // Grab the contents
-               String text =
-                       headerStories.text().substring(prop.getStart(), prop.getEnd());
+               String text = rawText.substring(start, end);
 
                // Strip off fields and macros if requested
                if(stripFields) {
index 3ca917514f019a52dfe26b0ba3744e1b0cea5463..19e67a46db6e097675dc663d69914de8519f6df5 100644 (file)
@@ -21,6 +21,7 @@ import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.hwpf.HWPFDocument;
 import org.apache.poi.hwpf.HWPFTestCase;
 import org.apache.poi.hwpf.HWPFTestDataSamples;
+import org.apache.poi.hwpf.extractor.WordExtractor;
 import org.apache.poi.hwpf.model.StyleSheet;
 
 /**
@@ -232,6 +233,27 @@ public final class TestProblems extends HWPFTestCase {
          }
       }
    }
+   
+   /**
+    * Bug #49936 - Problems with reading the header out of
+    *  the Header Stories
+    */
+   public void testProblemHeaderStories49936() throws Exception {
+      HWPFDocument doc = HWPFTestDataSamples.openSampleFile("HeaderFooterProblematic.doc");
+      HeaderStories hs = new HeaderStories(doc);
+      
+      assertEquals("", hs.getFirstHeader());
+      assertEquals("\r", hs.getEvenHeader());
+      assertEquals("", hs.getOddHeader());
+      
+      assertEquals("", hs.getFirstFooter());
+      assertEquals("", hs.getEvenFooter());
+      assertEquals("", hs.getOddFooter());
+      
+      WordExtractor ext = new WordExtractor(doc);
+      assertEquals("\n", ext.getHeaderText());
+      assertEquals("", ext.getFooterText());
+   }
 
    /**
     * Bug #48245 - don't include the text from the
diff --git a/test-data/document/HeaderFooterProblematic.doc b/test-data/document/HeaderFooterProblematic.doc
new file mode 100644 (file)
index 0000000..a4d9d30
Binary files /dev/null and b/test-data/document/HeaderFooterProblematic.doc differ