From 9ad2d70cec39b723395305288c03d7788cc590e0 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Thu, 7 Jul 2011 08:24:55 +0000 Subject: [PATCH] correctly detect cells of inner tables, do not include last "fake" cell in row git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1143707 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hwpf/usermodel/TableRow.java | 28 +++++++++---- .../poi/hwpf/usermodel/TestTableRow.java | 42 +++++++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestTableRow.java diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java index 52d81cafcd..95d378480e 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java @@ -51,14 +51,15 @@ public final class TableRow extends Paragraph final short expectedCellsCount = _tprops.getItcMac(); int lastCellStart = 0; - List cells = new ArrayList( expectedCellsCount ); - for ( int p = 0; p < (endIdxExclusive - startIdxInclusive); p++ ) + List cells = new ArrayList( + expectedCellsCount + 1 ); + for ( int p = 0; p < ( endIdxExclusive - startIdxInclusive ); p++ ) { Paragraph paragraph = getParagraph( p ); String s = paragraph.text(); - if ( s.length() > 0 - && s.charAt( s.length() - 1 ) == TABLE_CELL_MARK + if ( ( ( s.length() > 0 && s.charAt( s.length() - 1 ) == TABLE_CELL_MARK ) || paragraph + .isEmbeddedCellMark() ) && paragraph.getTableLevel() == levelNum ) { TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null @@ -79,7 +80,7 @@ public final class TableRow extends Paragraph } } - if ( lastCellStart < (endIdxExclusive - startIdxInclusive - 1) ) + if ( lastCellStart < ( endIdxExclusive - startIdxInclusive - 1 ) ) { TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null && _tprops.getRgtc().length > cells.size() ? _tprops @@ -92,11 +93,20 @@ public final class TableRow extends Paragraph .getRgdxaCenter()[cells.size() + 1] : 0; TableCell tableCell = new TableCell( lastCellStart, - (endIdxExclusive - startIdxInclusive - 1), this, levelNum, - tableCellDescriptor, leftEdge, rightEdge - leftEdge ); + ( endIdxExclusive - startIdxInclusive - 1 ), this, + levelNum, tableCellDescriptor, leftEdge, rightEdge + - leftEdge ); cells.add( tableCell ); } + TableCell lastCell = cells.get( cells.size() - 1 ); + if ( lastCell.numParagraphs() == 1 + && ( lastCell.getParagraph( 0 ).isTableRowEnd() ) ) + { + // remove "fake" cell + cells.remove( cells.size() - 1 ); + } + if ( cells.size() != expectedCellsCount ) { logger.log( POILogger.WARN, @@ -152,7 +162,7 @@ public final class TableRow extends Paragraph public void setCantSplit( boolean cantSplit ) { _tprops.setFCantSplit( cantSplit ); - _papx.updateSprm( SPRM_FCANTSPLIT, (byte) (cantSplit ? 1 : 0) ); + _papx.updateSprm( SPRM_FCANTSPLIT, (byte) ( cantSplit ? 1 : 0 ) ); } public boolean isTableHeader() @@ -163,7 +173,7 @@ public final class TableRow extends Paragraph public void setTableHeader( boolean tableHeader ) { _tprops.setFTableHeader( tableHeader ); - _papx.updateSprm( SPRM_FTABLEHEADER, (byte) (tableHeader ? 1 : 0) ); + _papx.updateSprm( SPRM_FTABLEHEADER, (byte) ( tableHeader ? 1 : 0 ) ); } public int numCells() diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestTableRow.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestTableRow.java new file mode 100644 index 0000000000..d3458e33b5 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestTableRow.java @@ -0,0 +1,42 @@ +package org.apache.poi.hwpf.usermodel; + +import junit.framework.TestCase; +import org.apache.poi.POIDataSamples; +import org.apache.poi.hwpf.HWPFDocument; + +public class TestTableRow extends TestCase +{ + public void testInnerTableCellsDetection() throws Exception + { + HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples + .getDocumentInstance().openResourceAsStream( "innertable.doc" ) ); + hwpfDocument.getRange(); + + Range documentRange = hwpfDocument.getRange(); + Paragraph startOfInnerTable = documentRange.getParagraph( 6 ); + + Table innerTable = documentRange.getTable( startOfInnerTable ); + assertEquals( 2, innerTable.numRows() ); + + TableRow tableRow = innerTable.getRow( 0 ); + assertEquals( 2, tableRow.numCells() ); + } + + public void testOuterTableCellsDetection() throws Exception + { + HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples + .getDocumentInstance().openResourceAsStream( "innertable.doc" ) ); + hwpfDocument.getRange(); + + Range documentRange = hwpfDocument.getRange(); + Paragraph startOfOuterTable = documentRange.getParagraph( 0 ); + + Table outerTable = documentRange.getTable( startOfOuterTable ); + assertEquals( 3, outerTable.numRows() ); + + assertEquals( 3, outerTable.getRow( 0 ).numCells() ); + assertEquals( 3, outerTable.getRow( 1 ).numCells() ); + assertEquals( 3, outerTable.getRow( 2 ).numCells() ); + } + +} -- 2.39.5