Do not fail on different count of categories and points and fix cloning charts

We see many cases where documents have charts with a difference here, so
downgrade the exception to a warning for now

Also handle cloning charts in workbooks where the parts have unexpected names
by making sure that the chosen part-name is not yet taken.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894676 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2021-11-02 13:17:53 +00:00
parent d205318583
commit 7a5bc4227b
3 changed files with 19 additions and 2 deletions

View File

@ -23,6 +23,8 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
@ -45,6 +47,8 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
*/ */
@Beta @Beta
public abstract class XDDFChartData { public abstract class XDDFChartData {
private static final Logger LOGGER = LogManager.getLogger(XDDFChartData.class);
protected XDDFChart parent; protected XDDFChart parent;
protected List<Series> series; protected List<Series> series;
private XDDFCategoryAxis categoryAxis; private XDDFCategoryAxis categoryAxis;
@ -166,7 +170,7 @@ public abstract class XDDFChartData {
if (categoryData != null && values != null) { if (categoryData != null && values != null) {
int numOfPoints = category.getPointCount(); int numOfPoints = category.getPointCount();
if (numOfPoints != values.getPointCount()) { if (numOfPoints != values.getPointCount()) {
throw new IllegalStateException("Category and values must have the same point count, but had " + LOGGER.warn("Category and values must have the same point count, but had " +
numOfPoints + " categories and " + values.getPointCount() + " values."); numOfPoints + " categories and " + values.getPointCount() + " values.");
} }
} }

View File

@ -33,6 +33,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
@ -243,9 +244,21 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
protected RelationPart createChartRelationPart() { protected RelationPart createChartRelationPart() {
XSSFWorkbook wb = getSheet().getWorkbook(); XSSFWorkbook wb = getSheet().getWorkbook();
XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory(); XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory();
int chartNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.CHART.getContentType()) OPCPackage pkg = getPackagePart().getPackage();
int chartNumber = pkg.getPartsByContentType(XSSFRelation.CHART.getContentType())
.size() + 1; .size() + 1;
// some broken files have incorrectly named package parts,
// so we need to avoid duplicates here by checking and increasing
// the part-number
try {
while (pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.CHART.getFileName(chartNumber))) != null) {
chartNumber++;
}
} catch (InvalidFormatException e) {
throw new IllegalStateException("Failed for " + chartNumber, e);
}
return createRelationship(XSSFRelation.CHART, factory, chartNumber, false); return createRelationship(XSSFRelation.CHART, factory, chartNumber, false);
} }

Binary file not shown.