]> source.dussan.org Git - poi.git/commitdiff
Support for extraction of endnotes from docx files
authorYegor Kozlov <yegor@apache.org>
Sat, 18 Jul 2009 09:28:38 +0000 (09:28 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 18 Jul 2009 09:28:38 +0000 (09:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@795329 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
src/scratchpad/testcases/org/apache/poi/hwpf/data/endnotes.docx [new file with mode: 0755]

index 7e764b2c68b341fe935dde5865f895031aa5e996..73abd537df69ac100c3d8f219fe7bac38bc268d3 100644 (file)
@@ -33,8 +33,9 @@
 
     <changes>
         <release version="3.5-beta7" date="2009-??-??">
-           <action dev="POI-DEVELOPERS" type="fix">45556 - Fixed ExtractorFactory to support .xltx and .dotx files</action>
+           <action dev="POI-DEVELOPERS" type="fix">47517 - Fixed ExtractorFactory to support .xltx and .dotx files</action>
            <action dev="POI-DEVELOPERS" type="add">45556 - Support for extraction of footnotes from docx files</action>
+           <action dev="POI-DEVELOPERS" type="add">45555 - Support for extraction of endnotes from docx files</action>
            <action dev="POI-DEVELOPERS" type="add">47520 - Initial support for custom XML mappings in XSSF</action>
            <action dev="POI-DEVELOPERS" type="fix">47460 - Fixed NPE when retrieving core properties from a newly created workbook</action>
            <action dev="POI-DEVELOPERS" type="fix">47498 - Fixed HyperlinkRecord to properly handle URL monikers</action>
index 2e86e79c7de1b93980846680ef23f2b787bb6f95..dc08bdcce725db4a315e290328f3b63be9a6dd38 100644 (file)
@@ -53,6 +53,7 @@ public class XWPFDocument extends POIXMLDocument {
     protected List<XWPFParagraph> paragraphs;
     protected List<XWPFTable> tables;
     protected Map<Integer, XWPFFootnote> footnotes;
+    protected Map<Integer, XWPFFootnote> endnotes;
 
     /** Handles the joy of different headers/footers for different pages */
     private XWPFHeaderFooterPolicy headerFooterPolicy;
@@ -81,6 +82,7 @@ public class XWPFDocument extends POIXMLDocument {
         paragraphs = new ArrayList<XWPFParagraph>();
         tables= new ArrayList<XWPFTable>();
         footnotes = new HashMap<Integer, XWPFFootnote>();
+        endnotes = new HashMap<Integer, XWPFFootnote>();
 
         try {
             DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream());
@@ -143,6 +145,12 @@ public class XWPFDocument extends POIXMLDocument {
                 for(CTFtnEdn ctFtnEdn : footnotesDocument.getFootnotes().getFootnoteArray()) {
                     footnotes.put(ctFtnEdn.getId().intValue(), new XWPFFootnote(this, ctFtnEdn));
                 }
+            } else if (relation.equals(XWPFRelation.ENDNOTE.getRelation())){
+                EndnotesDocument endnotesDocument = EndnotesDocument.Factory.parse(p.getPackagePart().getInputStream());
+
+                for(CTFtnEdn ctFtnEdn : endnotesDocument.getEndnotes().getEndnoteArray()) {
+                    endnotes.put(ctFtnEdn.getId().intValue(), new XWPFFootnote(this, ctFtnEdn));
+                }
             }
         }
     }
@@ -218,6 +226,10 @@ public class XWPFDocument extends POIXMLDocument {
         return footnotes.get(id);
     }
 
+    public XWPFFootnote getEndnoteByID(int id) {
+        return endnotes.get(id);
+    }
+
     public Collection<XWPFFootnote> getFootnotes() {
         return footnotes == null ? new ArrayList<XWPFFootnote>() : footnotes.values();
     }
index 1ddda9d4fc50d8f68296a0328564e1209f01f887..81a4add75e45eb3ea0bb31edf95336620284658b 100644 (file)
@@ -93,7 +93,10 @@ public class XWPFParagraph {
                     if (o instanceof CTFtnEdnRef) {
                         CTFtnEdnRef ftn = (CTFtnEdnRef) o;
                         footnoteText.append("[").append(ftn.getId()).append(": ");
-                        XWPFFootnote footnote = document.getFootnoteByID(ftn.getId().intValue());
+                        XWPFFootnote footnote = 
+                                ftn.getDomNode().getLocalName().equals("footnoteReference") ?
+                                        document.getFootnoteByID(ftn.getId().intValue()) :
+                                        document.getEndnoteByID(ftn.getId().intValue());
 
                         boolean first = true;
                         for (XWPFParagraph p : footnote.getParagraphs()) {
index c1b91bb07a17a5a55743e39056e3be66f35749f5..d28915e357b116f299002111784832ba35590485 100755 (executable)
@@ -112,6 +112,12 @@ public final class XWPFRelation extends POIXMLRelation {
             null,
             null
     );
+    public static final XWPFRelation ENDNOTE = new XWPFRelation(
+            null,
+            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes",
+            null,
+            null
+    );
 
 
     private XWPFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
index 1527e562b7519a2191edffded639db6299f7a91c..e923c40fcb37c8b3b73d0f46de2656a87b38c861 100644 (file)
@@ -185,7 +185,15 @@ public class TestXWPFWordExtractor extends TestCase {
         assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
     }
 
-    //TODO use the same logic as in HSSFTestDataSamples
+    public void testEndnotes() throws Exception {
+        XWPFDocument doc = open("endnotes.docx");
+        XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
+
+        assertTrue(extractor.getText().contains("XXX"));
+    }
+
+
+    //TODO use the same logic for opening test files as in HSSFTestDataSamples
     private XWPFDocument open(String sampleFileName) throws IOException {
         File file = new File(
                 System.getProperty("HWPF.testdata.path"), sampleFileName);
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/data/endnotes.docx b/src/scratchpad/testcases/org/apache/poi/hwpf/data/endnotes.docx
new file mode 100755 (executable)
index 0000000..a5db349
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hwpf/data/endnotes.docx differ