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
This commit is contained in:
Sergey Vladimirov 2011-07-07 08:24:55 +00:00
parent 6a3feed6e1
commit 9ad2d70cec
2 changed files with 61 additions and 9 deletions

View File

@ -51,14 +51,15 @@ public final class TableRow extends Paragraph
final short expectedCellsCount = _tprops.getItcMac();
int lastCellStart = 0;
List<TableCell> cells = new ArrayList<TableCell>( expectedCellsCount );
for ( int p = 0; p < (endIdxExclusive - startIdxInclusive); p++ )
List<TableCell> cells = new ArrayList<TableCell>(
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()

View File

@ -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() );
}
}