]> source.dussan.org Git - poi.git/commitdiff
correctly detect cells of inner tables, do not include last "fake" cell in row
authorSergey Vladimirov <sergey@apache.org>
Thu, 7 Jul 2011 08:24:55 +0000 (08:24 +0000)
committerSergey Vladimirov <sergey@apache.org>
Thu, 7 Jul 2011 08:24:55 +0000 (08:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1143707 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestTableRow.java [new file with mode: 0644]

index 52d81cafcd283fcf968b2c4b4dc247f1e1cf75d6..95d378480eab42198eb99dec1cd8d3a279783c2e 100644 (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()
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 (file)
index 0000000..d3458e3
--- /dev/null
@@ -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() );
+    }
+
+}