diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-10-12 20:41:32 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-10-12 20:41:32 +0000 |
commit | 88ebe095dc1f576e3ec4ebf4d38e4c58f2ddcd3e (patch) | |
tree | de73a8e10ff61d709fc905cdea9fb875e2685fe9 | |
parent | db71e2328a6fc907a8af888b1d9d926c0222177c (diff) | |
download | poi-88ebe095dc1f576e3ec4ebf4d38e4c58f2ddcd3e.tar.gz poi-88ebe095dc1f576e3ec4ebf4d38e4c58f2ddcd3e.zip |
use workbook's xssf factory
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894174 13f79535-47bb-0310-9956-ffa450edef68
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<XSS } protected RelationPart createChartRelationPart() { + XSSFWorkbook wb = getSheet().getWorkbook(); + XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory(); int chartNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.CHART.getContentType()) .size() + 1; - return createRelationship(XSSFRelation.CHART, XSSFFactory.getInstance(), chartNumber, false); + return createRelationship(XSSFRelation.CHART, factory, chartNumber, false); } /** @@ -286,8 +288,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS * {@link XSSFWorkbook#getAllPictures()} . */ protected PackageRelationship addPictureReference(int pictureIndex) { - XSSFWorkbook wb = (XSSFWorkbook) getParent().getParent(); - XSSFPictureData data = wb.getAllPictures().get(pictureIndex); + XSSFPictureData data = getSheet().getWorkbook().getAllPictures().get(pictureIndex); XSSFPictureData pic = new XSSFPictureData(data.getPackagePart()); RelationPart rp = addRelation(null, XSSFRelation.IMAGES, pic); return rp.getRelationship(); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 9b02b08066..a5710b066c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -563,7 +563,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // Default drawingNumber = #drawings.size() + 1 int drawingNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size() + 1; drawingNumber = getNextPartNumber(XSSFRelation.DRAWINGS, drawingNumber); - RelationPart rp = createRelationship(XSSFRelation.DRAWINGS, XSSFFactory.getInstance(), drawingNumber, false); + RelationPart rp = createRelationship(XSSFRelation.DRAWINGS, getWorkbook().getXssfFactory(), drawingNumber, false); XSSFDrawing drawing = rp.getDocumentPart(); String relId = rp.getRelationship().getId(); @@ -590,7 +590,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { if(autoCreate) { int drawingNumber = getNextPartNumber(XSSFRelation.VML_DRAWINGS, getPackagePart().getPackage().getPartsByContentType(XSSFRelation.VML_DRAWINGS.getContentType()).size()); - RelationPart rp = createRelationship(XSSFRelation.VML_DRAWINGS, XSSFFactory.getInstance(), drawingNumber, false); + RelationPart rp = createRelationship(XSSFRelation.VML_DRAWINGS, getWorkbook().getXssfFactory(), drawingNumber, false); drawing = rp.getDocumentPart(); String relId = rp.getRelationship().getId(); @@ -3531,13 +3531,13 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // the sheet has (i.e. sheet 1 -> 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); - } } } |