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:
parent
b3def16e9a
commit
25f3636438
@ -51,14 +51,15 @@ public final class TableRow extends Paragraph
|
|||||||
final short expectedCellsCount = _tprops.getItcMac();
|
final short expectedCellsCount = _tprops.getItcMac();
|
||||||
|
|
||||||
int lastCellStart = 0;
|
int lastCellStart = 0;
|
||||||
List<TableCell> cells = new ArrayList<TableCell>( expectedCellsCount );
|
List<TableCell> cells = new ArrayList<TableCell>(
|
||||||
|
expectedCellsCount + 1 );
|
||||||
for ( int p = 0; p < ( endIdxExclusive - startIdxInclusive ); p++ )
|
for ( int p = 0; p < ( endIdxExclusive - startIdxInclusive ); p++ )
|
||||||
{
|
{
|
||||||
Paragraph paragraph = getParagraph( p );
|
Paragraph paragraph = getParagraph( p );
|
||||||
String s = paragraph.text();
|
String s = paragraph.text();
|
||||||
|
|
||||||
if ( s.length() > 0
|
if ( ( ( s.length() > 0 && s.charAt( s.length() - 1 ) == TABLE_CELL_MARK ) || paragraph
|
||||||
&& s.charAt( s.length() - 1 ) == TABLE_CELL_MARK
|
.isEmbeddedCellMark() )
|
||||||
&& paragraph.getTableLevel() == levelNum )
|
&& paragraph.getTableLevel() == levelNum )
|
||||||
{
|
{
|
||||||
TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
|
TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
|
||||||
@ -92,11 +93,20 @@ public final class TableRow extends Paragraph
|
|||||||
.getRgdxaCenter()[cells.size() + 1] : 0;
|
.getRgdxaCenter()[cells.size() + 1] : 0;
|
||||||
|
|
||||||
TableCell tableCell = new TableCell( lastCellStart,
|
TableCell tableCell = new TableCell( lastCellStart,
|
||||||
(endIdxExclusive - startIdxInclusive - 1), this, levelNum,
|
( endIdxExclusive - startIdxInclusive - 1 ), this,
|
||||||
tableCellDescriptor, leftEdge, rightEdge - leftEdge );
|
levelNum, tableCellDescriptor, leftEdge, rightEdge
|
||||||
|
- leftEdge );
|
||||||
cells.add( tableCell );
|
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 )
|
if ( cells.size() != expectedCellsCount )
|
||||||
{
|
{
|
||||||
logger.log( POILogger.WARN,
|
logger.log( POILogger.WARN,
|
||||||
|
@ -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() );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user