diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index a18332829..9c9a4b702 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,9 @@ + Include headers and footers int he extracted text from HWPF's WordExtractor + Added support to HWPF for headers and footers + Improve how HWPF deals with unicode internally. Should avoid some odd behaviour when manipulating unicode text 45577 - Added implementations for Excel functions NOW and TODAY 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord 45537 - Include headers and footers (of slides and notes) in the extracted text from HSLF diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b0bc9bb21..617031475 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,9 @@ + Include headers and footers int he extracted text from HWPF's WordExtractor + Added support to HWPF for headers and footers + Improve how HWPF deals with unicode internally. Should avoid some odd behaviour when manipulating unicode text 45577 - Added implementations for Excel functions NOW and TODAY 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord 45537 - Include headers and footers (of slides and notes) in the extracted text from HSLF diff --git a/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java b/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java index d73aa337c..63c6a18ca 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java @@ -25,6 +25,7 @@ import java.util.Iterator; import org.apache.poi.POIOLE2TextExtractor; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.model.TextPiece; +import org.apache.poi.hwpf.usermodel.HeaderStories; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -115,6 +116,65 @@ public class WordExtractor extends POIOLE2TextExtractor { return ret; } + /** + * Add the header/footer text, if it's not empty + */ + private void appendHeaderFooter(String text, StringBuffer out) { + if(text == null || text.length() == 0) + return; + + text = text.replace('\r', '\n'); + if(! text.endsWith("\n")) { + out.append(text); + out.append('\n'); + return; + } + if(text.endsWith("\n\n")) { + out.append(text.substring(0, text.length()-1)); + return; + } + out.append(text); + return; + } + /** + * Grab the text from the headers + */ + public String getHeaderText() { + HeaderStories hs = new HeaderStories(doc); + + StringBuffer ret = new StringBuffer(); + if(hs.getFirstHeader() != null) { + appendHeaderFooter(hs.getFirstHeader(), ret); + } + if(hs.getEvenHeader() != null) { + appendHeaderFooter(hs.getEvenHeader(), ret); + } + if(hs.getOddHeader() != null) { + appendHeaderFooter(hs.getOddHeader(), ret); + } + + return ret.toString(); + } + /** + * Grab the text from the footers + */ + public String getFooterText() { + HeaderStories hs = new HeaderStories(doc); + + StringBuffer ret = new StringBuffer(); + if(hs.getFirstFooter() != null) { + appendHeaderFooter(hs.getFirstFooter(), ret); + } + if(hs.getEvenFooter() != null) { + appendHeaderFooter(hs.getEvenFooter(), ret); + } + if(hs.getOddFooter() != null) { + appendHeaderFooter(hs.getOddFooter(), ret); + } + + return ret.toString(); + } + /** * Grab the text out of the text pieces. Might also include various * bits of crud, but will work in cases where the text piece -> paragraph @@ -158,10 +218,16 @@ public class WordExtractor extends POIOLE2TextExtractor { */ public String getText() { StringBuffer ret = new StringBuffer(); + + ret.append(getHeaderText()); + String[] text = getParagraphText(); for(int i=0; i -1 + ); + + + // Unicode + doc = new HWPFDocument( + new FileInputStream(filename5) + ); + extractor = new WordExtractor(doc); + + assertEquals( + "\n\nThis is a simple header, with a \u20ac euro symbol in it.\n\n", + extractor.getHeaderText() + ); + text = extractor.getText(); + assertTrue( + text.indexOf("This is a simple header") > -1 + ); + } + + public void testWithFooter() throws Exception { + // Non-unicode + HWPFDocument doc = new HWPFDocument( + new FileInputStream(filename4) + ); + extractor = new WordExtractor(doc); + + assertEquals( + "Footer Left\tFooter Middle Footer Right\n", + extractor.getFooterText() + ); + + String text = extractor.getText(); + assertTrue( + text.indexOf("Footer Left") > -1 + ); + + + // Unicode + doc = new HWPFDocument( + new FileInputStream(filename5) + ); + extractor = new WordExtractor(doc); + + assertEquals( + "\n\nThe footer, with Moli\u00e8re, has Unicode in it.\n", + extractor.getFooterText() + ); + text = extractor.getText(); + assertTrue( + text.indexOf("The footer, with") > -1 + ); + } }