From 896840bb82919b74932e32fd355998a085d6f916 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Mon, 31 Aug 2009 17:02:06 +0000 Subject: [PATCH] fix for extraction paragraphs and sections from headers/footers with XWPFWordExtractor, see Bugzilla 47727 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@809662 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 3 +- .../poi/xwpf/extractor/XWPFWordExtractor.java | 86 ++++++++++++------ .../xwpf/model/XWPFHeaderFooterPolicy.java | 13 ++- .../xwpf/extractor/TestXWPFWordExtractor.java | 9 ++ test-data/document/Headers.docx | Bin 0 -> 13076 bytes 5 files changed, 81 insertions(+), 30 deletions(-) create mode 100755 test-data/document/Headers.docx diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index a27f2971d2..0191867715 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,7 +33,8 @@ - 47773 - Support for extraction of header / footer images in HWPF + 47773 - Fix for extraction paragraphs and sections from headers/footers with XWPFWordExtractor + 47727 - Support for extraction of header / footer images in HWPF moved all test data to a top-level directory 47721 - Added implementation for INDIRECT() 45583 - Avoid exception when reading ClipboardData packet in OLE property sets diff --git a/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java b/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java index 63059653f7..2c604b60e4 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java +++ b/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java @@ -21,6 +21,7 @@ import java.util.Iterator; import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLTextExtractor; +import org.apache.poi.POIXMLException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.model.XWPFCommentsDecorator; @@ -31,6 +32,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.xmlbeans.XmlException; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; /** * Helper class to extract text from an OOXML Word file @@ -72,45 +74,77 @@ public class XWPFWordExtractor extends POIXMLTextExtractor { public String getText() { StringBuffer text = new StringBuffer(); XWPFHeaderFooterPolicy hfPolicy = document.getHeaderFooterPolicy(); - + // Start out with all headers - // TODO - put them in where they're needed - if(hfPolicy.getFirstPageHeader() != null) { - text.append( hfPolicy.getFirstPageHeader().getText() ); - } - if(hfPolicy.getEvenPageHeader() != null) { - text.append( hfPolicy.getEvenPageHeader().getText() ); - } - if(hfPolicy.getDefaultHeader() != null) { - text.append( hfPolicy.getDefaultHeader().getText() ); - } + extractHeaders(text, hfPolicy); // First up, all our paragraph based text Iterator i = document.getParagraphsIterator(); while(i.hasNext()) { - XWPFParagraphDecorator decorator = new XWPFCommentsDecorator( - new XWPFHyperlinkDecorator(i.next(), null, fetchHyperlinks)); - text.append(decorator.getText()+"\n"); - } + XWPFParagraph paragraph = i.next(); + + + try { + CTSectPr ctSectPr = null; + if (paragraph.getCTP().getPPr()!=null) { + ctSectPr = paragraph.getCTP().getPPr().getSectPr(); + } + + XWPFHeaderFooterPolicy headerFooterPolicy = null; + + if (ctSectPr!=null) { + headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr); + + extractHeaders(text, headerFooterPolicy); + } + + XWPFParagraphDecorator decorator = new XWPFCommentsDecorator( + new XWPFHyperlinkDecorator(paragraph, null, fetchHyperlinks)); + text.append(decorator.getText()).append('\n'); + + if (ctSectPr!=null) { + extractFooters(text, headerFooterPolicy); + } + } catch (IOException e) { + throw new POIXMLException(e); + } catch (XmlException e) { + throw new POIXMLException(e); + } + } // Then our table based text Iterator j = document.getTablesIterator(); while(j.hasNext()) { - text.append(j.next().getText()+"\n"); + text.append(j.next().getText()).append('\n'); } // Finish up with all the footers - // TODO - put them in where they're needed - if(hfPolicy.getFirstPageFooter() != null) { - text.append( hfPolicy.getFirstPageFooter().getText() ); - } - if(hfPolicy.getEvenPageFooter() != null) { - text.append( hfPolicy.getEvenPageFooter().getText() ); - } - if(hfPolicy.getDefaultFooter() != null) { - text.append( hfPolicy.getDefaultFooter().getText() ); - } + extractFooters(text, hfPolicy); return text.toString(); } + + private void extractFooters(StringBuffer text, XWPFHeaderFooterPolicy hfPolicy) { + if(hfPolicy.getFirstPageFooter() != null) { + text.append( hfPolicy.getFirstPageFooter().getText() ); + } + if(hfPolicy.getEvenPageFooter() != null) { + text.append( hfPolicy.getEvenPageFooter().getText() ); + } + if(hfPolicy.getDefaultFooter() != null) { + text.append( hfPolicy.getDefaultFooter().getText() ); + } + } + + private void extractHeaders(StringBuffer text, XWPFHeaderFooterPolicy hfPolicy) { + if(hfPolicy.getFirstPageHeader() != null) { + text.append( hfPolicy.getFirstPageHeader().getText() ); + } + if(hfPolicy.getEvenPageHeader() != null) { + text.append( hfPolicy.getEvenPageHeader().getText() ); + } + if(hfPolicy.getDefaultHeader() != null) { + text.append( hfPolicy.getDefaultHeader().getText() ); + } + } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java index b6bc5f15eb..a0ffb87bf9 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java +++ b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java @@ -83,19 +83,26 @@ public class XWPFHeaderFooterPolicy { private XWPFHeader defaultHeader; private XWPFFooter defaultFooter; - + /** + * Figures out the policy for the given document, + * and creates any header and footer objects + * as required. + */ + public XWPFHeaderFooterPolicy(XWPFDocument doc) throws IOException, XmlException { + this(doc, doc.getDocument().getBody().getSectPr()); + } + /** * Figures out the policy for the given document, * and creates any header and footer objects * as required. */ - public XWPFHeaderFooterPolicy(XWPFDocument doc) throws IOException, XmlException { + public XWPFHeaderFooterPolicy(XWPFDocument doc, CTSectPr sectPr) throws IOException, XmlException { // Grab what headers and footers have been defined // For now, we don't care about different ranges, as it // doesn't seem that .docx properly supports that // feature of the file format yet this.doc = doc; - CTSectPr sectPr = doc.getDocument().getBody().getSectPr(); for(int i=0; igzFQcXFe~wWxj!qHK{>1(DmfzBB@X(kspmMEJOm1b?(;7?h#Tv54k)Hpz$!C)#uUCR?+Nz?hj{ z>R`Ea>+L^b7+tVxOq4zCU*iG*PfuU~xxX1SUJO>_8E}@$0PPhHXwW+LMwSk=Z+`6m zYsmk>*8AJ1hsQ2Tfbqiz9zm??4SKRJ!V*n=SrN-|A=(4;PuT}?K^)CrXm86SXqH>l z{(inSB0HDnmo~#$#06QYr#72CpQbCiYT;_t7zQP$HmH&6Yl0v;US(!&Ge^r66Q9Es zPG6S0Q5(ol!b4=tSe2(4Fxalt9E~FmqZk!4ihU7Y*4#dD{uV4XrEozF!OY}{uYN$4 zMQ09CixtZY*7#_CBm{L3O3&uY8+}Sk@qYUVbBYUUEK9lx(u^4W53nq2X1TK$LQgtj zkpa{&IWQp;XC6-JVCvdPj$d-!1}Zu63Dv04Jl*dE{c34Vd4R6%-)&_48zccU@JcfT z0DuF4263^mH>CZ;Pz-GhoUDNU?}y|2(`Z0|-Vb>9fA;nve#F}E2m2@7A`RZ@6Ik8K z5u_C>G|)T%ppcLtEDdGx?>$9}N%Dmc`?2$zg54`B#w;6jw6HziS4XH&g!KeNWW&;6 zD;Mn3c(=Rh`UvObo7VfrBj|o%X=z_*<)ez`sd1WKe6JZVtrjuQYiAvX#i1v-?3c@0 zz>>j-CEe#Pk0T7h6_U%$>VTA}JTUICA>B-Li`kYCig2laZ zued$)$u_HhwT8YTFw@{P_U8h&pa7D zNoRaD=b{516{&<>Xc#$V=SxHhC!GAj{``7p*))X6pqeC^O5DLQ7joH3yFq&b3qs|DyBwCX3Ggu38Yk}9sbzw#8wW!Hw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