aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2008-08-05 11:26:38 +0000
committerNick Burch <nick@apache.org>2008-08-05 11:26:38 +0000
commit7360b1024729d13597ec4b108b4b221acce92ef9 (patch)
treec12d0dbddc8b8ae220d6a4e97e72bd0973115cec /src/ooxml/java/org/apache
parente7476735dc742bd5c6e86419572f6c96f98ecae2 (diff)
downloadpoi-7360b1024729d13597ec4b108b4b221acce92ef9.tar.gz
poi-7360b1024729d13597ec4b108b4b221acce92ef9.zip
Fix bug #45540 - Fix XSSF header and footer support, and include headers and footers in the output of XSSFExcelExtractor
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@682674 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java42
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java10
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java118
3 files changed, 112 insertions, 58 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
index 113436dc40..b026baf0ea 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
@@ -25,9 +25,8 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.OpenXML4JException;
@@ -37,7 +36,7 @@ import org.openxml4j.opc.Package;
* Helper class to extract text from an OOXML Excel file
*/
public class XSSFExcelExtractor extends POIXMLTextExtractor {
- private Workbook workbook;
+ private XSSFWorkbook workbook;
private boolean includeSheetNames = true;
private boolean formulasNotResults = false;
private boolean includeCellComments = false;
@@ -91,18 +90,23 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor {
StringBuffer text = new StringBuffer();
for(int i=0; i<workbook.getNumberOfSheets(); i++) {
- Sheet sheet = workbook.getSheetAt(i);
+ XSSFSheet sheet = (XSSFSheet)workbook.getSheetAt(i);
if(includeSheetNames) {
text.append(workbook.getSheetName(i) + "\n");
}
- // Header, if present
- if(sheet.getHeader() != null) {
- text.append(
- extractHeaderFooter(sheet.getHeader())
- );
- }
-
+ // Header(s), if present
+ text.append(
+ extractHeaderFooter(sheet.getFirstHeader())
+ );
+ text.append(
+ extractHeaderFooter(sheet.getOddHeader())
+ );
+ text.append(
+ extractHeaderFooter(sheet.getEvenHeader())
+ );
+
+ // Rows and cells
for (Object rawR : sheet) {
Row row = (Row)rawR;
for(Iterator<Cell> ri = row.cellIterator(); ri.hasNext();) {
@@ -133,12 +137,16 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor {
text.append("\n");
}
- // Finally footer, if present
- if(sheet.getFooter() != null) {
- text.append(
- extractHeaderFooter(sheet.getFooter())
- );
- }
+ // Finally footer(s), if present
+ text.append(
+ extractHeaderFooter(sheet.getFirstFooter())
+ );
+ text.append(
+ extractHeaderFooter(sheet.getOddFooter())
+ );
+ text.append(
+ extractHeaderFooter(sheet.getEvenFooter())
+ );
}
return text.toString();
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java
index 6d1b152e04..8c79761e95 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java
@@ -24,21 +24,21 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public abstract class XSSFHeaderFooter implements HeaderFooter {
private HeaderFooterHelper helper;
private CTHeaderFooter headerFooter;
- private String value;
public XSSFHeaderFooter(CTHeaderFooter headerFooter) {
this.headerFooter = headerFooter;
- this.value = getText();
- this.value = this.value != null ? this.value : "";
this.helper = new HeaderFooterHelper();
}
public CTHeaderFooter getHeaderFooter() {
return this.headerFooter;
}
-
+
public String getValue() {
- return this.value;
+ String value = getText();
+ if(value == null)
+ return "";
+ return value;
}
public abstract String getText();
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java
index 18fddf52ab..41e3ea462e 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java
@@ -19,55 +19,101 @@ package org.apache.poi.xssf.usermodel.helpers;
public class HeaderFooterHelper {
+ // Note - XmlBeans handles entity encoding for us,
+ // so these should be & forms, not the &amp; ones!
+ private static final String HeaderFooterEntity_L = "&L";
+ private static final String HeaderFooterEntity_C = "&C";
+ private static final String HeaderFooterEntity_R = "&R";
- private static final String HeaderFooterEntity = "&amp;";
- private static final String HeaderFooterEntity_R = "&amp;R";
- private static final String HeaderFooterEntity_L = "&amp;L";
- private static final String HeaderFooterEntity_C = "&amp;C";
+ // These are other entities that may be used in the
+ // left, center or right. Not exhaustive
+ public static final String HeaderFooterEntity_File = "&F";
+ public static final String HeaderFooterEntity_Date = "&D";
+ public static final String HeaderFooterEntity_Time = "&T";
- public String getCenterSection(String string) {
- return getSection(string, HeaderFooterEntity_C);
- }
public String getLeftSection(String string) {
- return getSection(string, HeaderFooterEntity_L);
+ return getParts(string)[0];
+ }
+ public String getCenterSection(String string) {
+ return getParts(string)[1];
}
public String getRightSection(String string) {
- return getSection(string, HeaderFooterEntity_R);
+ return getParts(string)[2];
}
- public String setCenterSection(String string, String newCenter) {
- return setSection(string, newCenter, HeaderFooterEntity_C);
- }
public String setLeftSection(String string, String newLeft) {
- return setSection(string, newLeft, HeaderFooterEntity_L);
+ String[] parts = getParts(string);
+ parts[0] = newLeft;
+ return joinParts(parts);
+ }
+ public String setCenterSection(String string, String newCenter) {
+ String[] parts = getParts(string);
+ parts[1] = newCenter;
+ return joinParts(parts);
}
public String setRightSection(String string, String newRight) {
- return setSection(string, newRight, HeaderFooterEntity_R);
+ String[] parts = getParts(string);
+ parts[2] = newRight;
+ return joinParts(parts);
}
- public String setSection(String string, String newSection, String entity) {
- string = string != null ? string : "";
- String oldSection = getSection(string, entity);
- if (oldSection.equals("")) {
- return string.concat(entity + newSection);
- }
- return string.replaceAll(entity + oldSection, entity + newSection);
+ /**
+ * Split into left, center, right
+ */
+ private String[] getParts(String string) {
+ String[] parts = new String[] { "", "", "" };
+ if(string == null)
+ return parts;
+
+ // They can come in any order, which is just nasty
+ // Work backwards from the end, picking the last
+ // on off each time as we go
+ int lAt = 0;
+ int cAt = 0;
+ int rAt = 0;
+
+ while(
+ // Ensure all indicies get updated, then -1 tested
+ (lAt = string.indexOf(HeaderFooterEntity_L)) > -2 &&
+ (cAt = string.indexOf(HeaderFooterEntity_C)) > -2 &&
+ (rAt = string.indexOf(HeaderFooterEntity_R)) > -2 &&
+ (lAt > -1 || cAt > -1 || rAt > -1)
+ ) {
+ // Pick off the last one
+ if(rAt > cAt && rAt > lAt) {
+ parts[2] = string.substring(rAt + HeaderFooterEntity_R.length());
+ string = string.substring(0, rAt);
+ } else if(cAt > rAt && cAt > lAt) {
+ parts[1] = string.substring(cAt + HeaderFooterEntity_C.length());
+ string = string.substring(0, cAt);
+ } else {
+ parts[0] = string.substring(lAt + HeaderFooterEntity_L.length());
+ string = string.substring(0, lAt);
+ }
+ }
+
+ return parts;
}
-
- private String getSection(String string, String entity) {
- if (string == null) {
- return "";
- }
- String stringAfterEntity = "";
- if (string.indexOf(entity) >= 0) {
- stringAfterEntity = string.substring(string.indexOf(entity) + entity.length());
- }
- String nextEntity = "";
- if (stringAfterEntity.indexOf(HeaderFooterEntity) > 0) {
- nextEntity = stringAfterEntity.substring(stringAfterEntity.indexOf(HeaderFooterEntity), stringAfterEntity.indexOf(HeaderFooterEntity) + (HeaderFooterEntity.length()));
- stringAfterEntity = stringAfterEntity.substring(0, stringAfterEntity.indexOf(nextEntity));
- }
- return stringAfterEntity;
+ private String joinParts(String[] parts) {
+ return joinParts(parts[0], parts[1], parts[2]);
}
+ private String joinParts(String l, String c, String r) {
+ StringBuffer ret = new StringBuffer();
+ // Join as c, l, r
+ if(c.length() > 0) {
+ ret.append(HeaderFooterEntity_C);
+ ret.append(c);
+ }
+ if(l.length() > 0) {
+ ret.append(HeaderFooterEntity_L);
+ ret.append(l);
+ }
+ if(r.length() > 0) {
+ ret.append(HeaderFooterEntity_R);
+ ret.append(r);
+ }
+
+ return ret.toString();
+ }
}