]> source.dussan.org Git - poi.git/commitdiff
Fix bug #45538 - Include excel headers and footers in the output of ExcelExtractor
authorNick Burch <nick@apache.org>
Mon, 4 Aug 2008 21:21:16 +0000 (21:21 +0000)
committerNick Burch <nick@apache.org>
Mon, 4 Aug 2008 21:21:16 +0000 (21:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@682511 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java
src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/data/45538_classic_Footer.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/data/45538_classic_Header.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/data/45538_form_Footer.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/data/45538_form_Header.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java

index cd1dfd0856d3487717cf535a06577d5286fe6f20..c3080c322a26246ec918bcfc7bd0a52d0594c83c 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action>
            <action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action>
            <action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action>
            <action dev="POI-DEVELOPERS" type="fix">44953 - Extensive fixes for data validation</action>
index b09ea50a02a2a27b02538e8cd4cb1f1d7bd5f8fc..95d2eb708aa98e1c78117795de55f5a851897bad 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action>
            <action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action>
            <action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action>
            <action dev="POI-DEVELOPERS" type="fix">44953 - Extensive fixes for data validation</action>
index 75a73c654d5c0fef29715de2ed5293a0e81616a7..efa75e0d52b547b6cbcd8fe5124bdf2505c1484e 100644 (file)
@@ -19,8 +19,11 @@ package org.apache.poi.hssf.extractor;
 import java.io.IOException;
 
 import org.apache.poi.POIOLE2TextExtractor;
+import org.apache.poi.hssf.usermodel.HeaderFooter;
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFComment;
+import org.apache.poi.hssf.usermodel.HSSFFooter;
+import org.apache.poi.hssf.usermodel.HSSFHeader;
 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
@@ -89,6 +92,13 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
                                }
                        }
                        
+                       // Header text, if there is any
+                       if(sheet.getHeader() != null) {
+                               text.append(
+                                               extractHeaderFooter(sheet.getHeader())
+                               );
+                       }
+                       
                        int firstRow = sheet.getFirstRowNum();
                        int lastRow = sheet.getLastRowNum();
                        for(int j=firstRow;j<=lastRow;j++) {
@@ -154,7 +164,36 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
                                // Finish off the row
                                text.append("\n");
                        }
+                       
+                       // Finally Feader text, if there is any
+                       if(sheet.getFooter() != null) {
+                               text.append(
+                                               extractHeaderFooter(sheet.getFooter())
+                               );
+                       }
+               }
+               
+               return text.toString();
+       }
+       
+       private String extractHeaderFooter(HeaderFooter hf) {
+               StringBuffer text = new StringBuffer();
+               
+               if(hf.getLeft() != null) {
+                       text.append(hf.getLeft());
+               }
+               if(hf.getCenter() != null) {
+                       if(text.length() > 0)
+                               text.append("\t");
+                       text.append(hf.getCenter());
+               }
+               if(hf.getRight() != null) {
+                       if(text.length() > 0)
+                               text.append("\t");
+                       text.append(hf.getRight());
                }
+               if(text.length() > 0)
+                       text.append("\n");
                
                return text.toString();
        }
index a72010378ffbaa69d81f58b8c3736877aaacf6cf..2114ed597740026aeb5fde7637608f8353615760 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.poi.hssf.record.FooterRecord;
  * <P>
  * @author Shawn Laubach (slaubach at apache dot org)
  */
-public class HSSFFooter extends Object {
+public class HSSFFooter extends Object implements HeaderFooter {
 
     FooterRecord footerRecord;
     String left;
index 614caf9b447ba7900a07eaf5143185440c3a2e2e..d2c99f988824b6c817d6be0d30249d79f0419dcc 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.poi.hssf.record.HeaderRecord;
  *
  * @author Shawn Laubach (slaubach at apache dot org)
  */
-public class HSSFHeader
+public class HSSFHeader implements HeaderFooter
 {
 
     HeaderRecord headerRecord;
diff --git a/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java b/src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
new file mode 100644 (file)
index 0000000..8c5ef05
--- /dev/null
@@ -0,0 +1,33 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.hssf.usermodel;
+
+/**
+ * Common interface for {@link HSSFHeader} and
+ *  {@link HSSFFooter}.
+ */
+public interface HeaderFooter {
+    public String getLeft();
+    public void setLeft( String newLeft );
+
+    public String getCenter();
+    public void setCenter( String newCenter );
+
+    public String getRight();
+    public void setRight( String newRight );
+
+}
diff --git a/src/testcases/org/apache/poi/hssf/data/45538_classic_Footer.xls b/src/testcases/org/apache/poi/hssf/data/45538_classic_Footer.xls
new file mode 100644 (file)
index 0000000..2ab1d24
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45538_classic_Footer.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/data/45538_classic_Header.xls b/src/testcases/org/apache/poi/hssf/data/45538_classic_Header.xls
new file mode 100644 (file)
index 0000000..fe67b84
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45538_classic_Header.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/data/45538_form_Footer.xls b/src/testcases/org/apache/poi/hssf/data/45538_form_Footer.xls
new file mode 100644 (file)
index 0000000..6d1731d
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45538_form_Footer.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/data/45538_form_Header.xls b/src/testcases/org/apache/poi/hssf/data/45538_form_Header.xls
new file mode 100644 (file)
index 0000000..c2a2821
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45538_form_Header.xls differ
index 9bb137ff69cd1b9959344381a546a4508c6e18aa..0913ab7250ce009d887a12ce30ff2cc1f6876c73 100644 (file)
@@ -255,4 +255,20 @@ public final class TestExcelExtractor extends TestCase {
                                ex.getText());
                assertEquals("Excel With Embeded", ex.getSummaryInformation().getTitle());
        }
+       
+       /**
+        * Test that we get text from headers and footers
+        */
+       public void test45538() throws Exception {
+               String[] files = new String[] {
+                       "45538_classic_Footer.xls", "45538_form_Footer.xls",    
+                       "45538_classic_Header.xls", "45538_form_Header.xls"
+               };
+               for(int i=0; i<files.length; i++) {
+                       ExcelExtractor extractor = createExtractor(files[i]);
+                       String text = extractor.getText();
+                       assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc"));
+                       assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
+               }
+       }
 }