diff --git a/src/java/org/apache/poi/sl/usermodel/TableShape.java b/src/java/org/apache/poi/sl/usermodel/TableShape.java index 5070cf4052..13c5d0dc52 100644 --- a/src/java/org/apache/poi/sl/usermodel/TableShape.java +++ b/src/java/org/apache/poi/sl/usermodel/TableShape.java @@ -21,10 +21,29 @@ public interface TableShape< S extends Shape, P extends TextParagraph > extends Shape, PlaceableShape { + /** + * Return the maximum number of columns. + * If the table contains merged cells, the number of columns might be less than the maximum. + * + * @return the maximum number of column + */ int getNumberOfColumns(); + /** + * Return the number of rows + * + * @return the row count + */ int getNumberOfRows(); + /** + * Gets a cell + * + * @param row the row index (0-based) + * @param col the column index (0-based) + * @return the cell or null if the cell doesn't exists, e.g. when accessing + * a merged cell or if the index is out of bounds + */ TableCell getCell(int row, int col); /** diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java index d0d474944a..21472d709b 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java @@ -55,7 +55,6 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable _rows; - @SuppressWarnings("deprecation") /*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); @@ -83,7 +82,21 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable rows = getRows(); + if (row < 0 || rows.size() <= row) { + return null; + } + XSLFTableRow r = rows.get(row); + if (r == null) { + // empty row + return null; + } + List cells = r.getCells(); + if (col < 0 || cells.size() <= col) { + return null; + } + // cell can be potentially empty ... + return cells.get(col); } @Internal diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java index 4fe197782e..737d2927a2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFShapeFactory.java @@ -58,7 +58,7 @@ public final class HSLFShapeFactory { for (EscherProperty ep : props) { if (ep.getPropertyNumber() == EscherProperties.GROUPSHAPE__TABLEPROPERTIES && ep instanceof EscherSimpleProperty - && ((EscherSimpleProperty)ep).getPropertyValue() == 1) { + && (((EscherSimpleProperty)ep).getPropertyValue() & 1) == 1) { isTable = true; break; } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTable.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTable.java index 814ffe7455..b82bed34a6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTable.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTable.java @@ -52,6 +52,7 @@ implements HSLFShapeContainer, TableShape { protected HSLFTableCell[][] cells; + private int columnCount = -1; /** * Create a new Table of the given number of rows and columns @@ -114,20 +115,31 @@ implements HSLFShapeContainer, TableShape { super(escherRecord, parent); } - /** - * Gets a cell - * - * @param row the row index (0-based) - * @param col the column index (0-based) - * @return the cell - */ + @Override public HSLFTableCell getCell(int row, int col) { - return cells[row][col]; + if (row < 0 || cells.length <= row) { + return null; + } + HSLFTableCell[] r = cells[row]; + if (r == null || col < 0 || r.length <= col) { + // empty row + return null; + } + // cell can be potentially empty ... + return r[col]; } @Override public int getNumberOfColumns() { - return cells[0].length; + if (columnCount == -1) { + // check all rows in case of merged rows + for (HSLFTableCell[] hc : cells) { + if (hc != null) { + columnCount = Math.max(columnCount, hc.length); + } + } + } + return columnCount; } @Override diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java index 6e11a41b54..68684bf8d6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -768,6 +768,15 @@ public final class TestBugs { ex.close(); } } + + @Test + public void bug58733() throws IOException { + File sample = HSLFTestDataSamples.getSampleFile("bug58733_671884.ppt"); + PowerPointExtractor ex = new PowerPointExtractor(sample.getAbsolutePath()); + assertNotNull(ex.getText()); + ex.close(); + } + private static HSLFSlideShow open(String fileName) throws IOException { File sample = HSLFTestDataSamples.getSampleFile(fileName); diff --git a/test-data/slideshow/bug58733_671884.ppt b/test-data/slideshow/bug58733_671884.ppt new file mode 100644 index 0000000000..08c46e0558 Binary files /dev/null and b/test-data/slideshow/bug58733_671884.ppt differ