From 88ebe095dc1f576e3ec4ebf4d38e4c58f2ddcd3e Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 12 Oct 2021 20:41:32 +0000 Subject: [PATCH] use workbook's xssf factory git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894174 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xssf/model/StylesTable.java | 3 ++- .../apache/poi/xssf/usermodel/XSSFDrawing.java | 7 ++++--- .../org/apache/poi/xssf/usermodel/XSSFSheet.java | 16 ++++++++-------- .../poi/xssf/usermodel/XSSFSignatureLine.java | 2 +- .../apache/poi/xssf/usermodel/XSSFWorkbook.java | 10 +++++++++- .../apache/poi/xssf/usermodel/TestXSSFBugs.java | 3 --- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java index 767b69e226..20e2e4d990 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java @@ -175,7 +175,8 @@ public class StylesTable extends POIXMLDocumentPart implements Styles { public void ensureThemesTable() { if (theme != null) return; - setTheme((ThemesTable)workbook.createRelationship(XSSFRelation.THEME, XSSFFactory.getInstance())); + XSSFFactory factory = workbook == null ? XSSFFactory.getInstance() : workbook.getXssfFactory(); + setTheme((ThemesTable)workbook.createRelationship(XSSFRelation.THEME, factory)); } /** diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index c781ca7285..b19f8e4611 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -241,10 +241,12 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing comments 1) try { sheetComments = (CommentsTable)createRelationship( - XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), Math.toIntExact(sheet.getSheetId())); + XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), Math.toIntExact(sheet.getSheetId())); } catch(PartAlreadyExistsException e) { // Technically a sheet doesn't need the same number as // it's comments, and clearly someone has already pinched // our number! Go for the next available one instead sheetComments = (CommentsTable)createRelationship( - XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), -1); + XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1); } } return sheetComments; @@ -4161,7 +4161,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } } - RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false); + RelationPart rp = createRelationship(XSSFRelation.TABLE, getWorkbook().getXssfFactory(), tableNumber, false); XSSFTable table = rp.getDocumentPart(); tbl.setId(rp.getRelationship().getId()); table.getCTTable().setId(tableNumber); @@ -4419,14 +4419,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { int tableId = getWorkbook().getPivotTables().size()+1; //Create relationship between pivotTable and the worksheet XSSFPivotTable pivotTable = (XSSFPivotTable) createRelationship(XSSFRelation.PIVOT_TABLE, - XSSFFactory.getInstance(), tableId); + getWorkbook().getXssfFactory(), tableId); pivotTable.setParentSheet(this); pivotTables.add(pivotTable); XSSFWorkbook workbook = getWorkbook(); //Create relationship between the pivot cache defintion and the workbook XSSFPivotCacheDefinition pivotCacheDefinition = (XSSFPivotCacheDefinition) workbook. - createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, XSSFFactory.getInstance(), tableId); + createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, getWorkbook().getXssfFactory(), tableId); String rId = workbook.getRelationId(pivotCacheDefinition); //Create relationship between pivotTable and pivotCacheDefinition without creating a new instance PackagePart pivotPackagePart = pivotTable.getPackagePart(); @@ -4440,7 +4440,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { //Create relationship between pivotcacherecord and pivotcachedefinition XSSFPivotCacheRecords pivotCacheRecords = (XSSFPivotCacheRecords) pivotCacheDefinition. - createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, XSSFFactory.getInstance(), tableId); + createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, getWorkbook().getXssfFactory(), tableId); //Set relationships id for pivotCacheDefinition to pivotCacheRecords pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setId(pivotCacheDefinition.getRelationId(pivotCacheRecords)); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSignatureLine.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSignatureLine.java index 478bca9626..99a6ddd58b 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSignatureLine.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSignatureLine.java @@ -91,7 +91,7 @@ public class XSSFSignatureLine extends SignatureLine { XSSFVMLDrawing vml = sheet.getVMLDrawing(false); POIXMLRelation xtype = mapType(type); int idx = wb.getNextPartNumber(xtype, -1); - POIXMLDocumentPart.RelationPart rp = vml.createRelationship(xtype, XSSFFactory.getInstance(), idx, false); + POIXMLDocumentPart.RelationPart rp = vml.createRelationship(xtype, wb.getXssfFactory(), idx, false); POIXMLDocumentPart dp = rp.getDocumentPart(); try (OutputStream out = dp.getPackagePart().getOutputStream()) { out.write(image); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index a85ef12453..b61cd50129 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -338,6 +338,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su this(part.getInputStream()); } + /** + * @return the XSSFFactory + * @since POI 5.1.0 + */ + public XSSFFactory getXssfFactory() { + return xssfFactory; + } + protected void beforeDocumentRead() { // Ensure it isn't a XLSB file, which we don't support if (getCorePart().getContentType().equals(XSSFRelation.XLSB_BINARY_WORKBOOK.getContentType())) { @@ -1987,7 +1995,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su if (!getCreationHelper().getReferencedWorkbooks().containsKey(name)){ externalLinkIdx = this.getNextPartNumber(XSSFRelation.EXTERNAL_LINKS, this.getPackagePart().getPackage().getPartsByContentType(XSSFRelation.EXTERNAL_LINKS.getContentType()).size()); - POIXMLDocumentPart.RelationPart rp = this.createRelationship(XSSFRelation.EXTERNAL_LINKS, XSSFFactory.getInstance(), externalLinkIdx, false); + POIXMLDocumentPart.RelationPart rp = this.createRelationship(XSSFRelation.EXTERNAL_LINKS, xssfFactory, externalLinkIdx, false); ExternalLinksTable linksTable = rp.getDocumentPart(); linksTable.setLinkedFileName(name); this.getExternalLinksTable().add(linksTable); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index e65c92e48e..3ffc986111 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -3618,9 +3618,6 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { b1 = sheet.getRow(0).getCell(1); assertEquals(2.0, a1.getNumericCellValue()); assertEquals("#REF!+3*$A$1", b1.getCellFormula()); - try (FileOutputStream fos = new FileOutputStream("abc.xlsx")) { - wb1.write(fos); - } } }