rewrite table bounds detection for Word 97, including inner table support

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1143070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-05 14:13:27 +00:00
parent fa46737e44
commit 8ac3172759
9 changed files with 240 additions and 175 deletions

View File

@ -241,6 +241,14 @@ public abstract class AbstractWordConverter
if ( paragraph.isInTable() if ( paragraph.isInTable()
&& paragraph.getTableLevel() != currentTableLevel ) && paragraph.getTableLevel() != currentTableLevel )
{ {
if ( paragraph.getTableLevel() < currentTableLevel )
throw new IllegalStateException(
"Trying to process table cell with higher level ("
+ paragraph.getTableLevel()
+ ") than current table level ("
+ currentTableLevel
+ ") as inner table part" );
Table table = range.getTable( paragraph ); Table table = range.getTable( paragraph );
processTable( wordDocument, flow, table ); processTable( wordDocument, flow, table );

View File

@ -84,9 +84,9 @@ public class Paragraph extends Range implements Cloneable {
protected ParagraphProperties _props; protected ParagraphProperties _props;
protected SprmBuffer _papx; protected SprmBuffer _papx;
protected Paragraph(int startIdx, int endIdx, Table parent) protected Paragraph(int startIdxInclusive, int endIdxExclusive, Table parent)
{ {
super(startIdx, endIdx, Range.TYPE_PARAGRAPH, parent); super(startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent);
PAPX papx = _paragraphs.get(_parEnd - 1); PAPX papx = _paragraphs.get(_parEnd - 1);
_props = papx.getParagraphProperties(_doc.getStyleSheet()); _props = papx.getParagraphProperties(_doc.getStyleSheet());
_papx = papx.getSprmBuf(); _papx = papx.getSprmBuf();

View File

@ -90,10 +90,10 @@ public class Range { // TODO -instantiable superclass
/** All paragraphs that belong to the document this Range belongs to. */ /** All paragraphs that belong to the document this Range belongs to. */
protected List<PAPX> _paragraphs; protected List<PAPX> _paragraphs;
/** The start index in the paragraphs list for this Range */ /** The start index in the paragraphs list for this Range, inclusive */
protected int _parStart; protected int _parStart;
/** The end index in the paragraphs list for this Range. */ /** The end index in the paragraphs list for this Range, exclusive */
protected int _parEnd; protected int _parEnd;
/** Have we loaded the characterRun indexes yet. */ /** Have we loaded the characterRun indexes yet. */
@ -178,9 +178,9 @@ public class Range { // TODO -instantiable superclass
* lists. * lists.
* *
* @param startIdx * @param startIdx
* The starting index in the list. * The starting index in the list, inclusive
* @param endIdx * @param endIdx
* The ending index in the list. * The ending index in the list, exclusive
* @param idxType * @param idxType
* The list type. * The list type.
* @param parent * @param parent
@ -199,27 +199,27 @@ public class Range { // TODO -instantiable superclass
_parStart = parent._parStart + startIdx; _parStart = parent._parStart + startIdx;
_parEnd = parent._parStart + endIdx; _parEnd = parent._parStart + endIdx;
_start = _paragraphs.get(_parStart).getStart(); _start = _paragraphs.get(_parStart).getStart();
_end = _paragraphs.get(_parEnd).getEnd(); _end = _paragraphs.get(_parEnd - 1).getEnd();
_parRangeFound = true; _parRangeFound = true;
break; break;
case TYPE_CHARACTER: case TYPE_CHARACTER:
_charStart = parent._charStart + startIdx; _charStart = parent._charStart + startIdx;
_charEnd = parent._charStart + endIdx; _charEnd = parent._charStart + endIdx;
_start = _characters.get(_charStart).getStart(); _start = _characters.get(_charStart - 1).getStart();
_end = _characters.get(_charEnd).getEnd(); _end = _characters.get(_charEnd).getEnd();
_charRangeFound = true; _charRangeFound = true;
break; break;
case TYPE_SECTION: case TYPE_SECTION:
_sectionStart = parent._sectionStart + startIdx; _sectionStart = parent._sectionStart + startIdx;
_sectionEnd = parent._sectionStart + endIdx; _sectionEnd = parent._sectionStart + endIdx;
_start = _sections.get(_sectionStart).getStart(); _start = _sections.get(_sectionStart - 1).getStart();
_end = _sections.get(_sectionEnd).getEnd(); _end = _sections.get(_sectionEnd).getEnd();
_sectionRangeFound = true; _sectionRangeFound = true;
break; break;
case TYPE_TEXT: case TYPE_TEXT:
_textStart = parent._textStart + startIdx; _textStart = parent._textStart + startIdx;
_textEnd = parent._textStart + endIdx; _textEnd = parent._textStart + endIdx;
_start = _text.get(_textStart).getStart(); _start = _text.get(_textStart - 1).getStart();
_end = _text.get(_textEnd).getEnd(); _end = _text.get(_textEnd).getEnd();
_textRangeFound = true; _textRangeFound = true;
break; break;
@ -833,6 +833,12 @@ public class Range { // TODO -instantiable superclass
public Paragraph getParagraph(int index) { public Paragraph getParagraph(int index) {
initParagraphs(); initParagraphs();
if ( index + _parStart >= _parEnd )
throw new IndexOutOfBoundsException( "Paragraph #" + index + " ("
+ (index + _parStart) + ") not in range [" + _parStart
+ "; " + _parEnd + ")" );
PAPX papx = _paragraphs.get(index + _parStart); PAPX papx = _paragraphs.get(index + _parStart);
ParagraphProperties props = papx.getParagraphProperties(_doc.getStyleSheet()); ParagraphProperties props = papx.getParagraphProperties(_doc.getStyleSheet());
@ -880,7 +886,7 @@ public class Range { // TODO -instantiable superclass
r.initAll(); r.initAll();
int tableLevel = paragraph.getTableLevel(); int tableLevel = paragraph.getTableLevel();
int tableEnd = r._parEnd; int tableEndInclusive = r._parEnd ;
if ( r._parStart != 0 ) if ( r._parStart != 0 )
{ {
@ -895,24 +901,30 @@ public class Range { // TODO -instantiable superclass
} }
} }
final Range overallrange = getDocument() instanceof HWPFDocument ? ((HWPFDocument) getDocument())
.getOverallRange() : getDocument().getRange();
int limit = _paragraphs.size(); int limit = _paragraphs.size();
for ( ; tableEnd < limit; tableEnd++ ) for ( ; tableEndInclusive < limit - 1; tableEndInclusive++ )
{ {
Paragraph next = new Paragraph( _paragraphs.get( tableEnd ), this ); Paragraph next = new Paragraph( _paragraphs.get( tableEndInclusive + 1 ),
overallrange );
if ( !next.isInTable() || next.getTableLevel() < tableLevel ) if ( !next.isInTable() || next.getTableLevel() < tableLevel )
break; break;
} }
initAll(); initAll();
if (tableEnd > _parEnd) { if ( tableEndInclusive + 1 > _parEnd )
{
throw new ArrayIndexOutOfBoundsException( throw new ArrayIndexOutOfBoundsException(
"The table's bounds fall outside of this Range" ); "The table's bounds fall outside of this Range" );
} }
if (tableEnd < 0) { if ( tableEndInclusive < 0 )
{
throw new ArrayIndexOutOfBoundsException( throw new ArrayIndexOutOfBoundsException(
"The table's end is negative, which isn't allowed!" ); "The table's end is negative, which isn't allowed!" );
} }
return new Table(r._parStart, tableEnd, r._doc.getRange(), paragraph.getTableLevel()); return new Table( r._parStart, tableEndInclusive + 1, r._doc.getRange(),
paragraph.getTableLevel() );
} }
/** /**
@ -989,6 +1001,10 @@ public class Range { // TODO -instantiable superclass
*/ */
private int[] findRange(List<? extends PropertyNode> rpl, int min, int start, int end) { private int[] findRange(List<? extends PropertyNode> rpl, int min, int start, int end) {
int x = min; int x = min;
if ( rpl.size() == min )
return new int[] { min, min };
PropertyNode node = rpl.get( x ); PropertyNode node = rpl.get( x );
while (node==null || (node.getEnd() <= start && x < rpl.size() - 1)) { while (node==null || (node.getEnd() <= start && x < rpl.size() - 1)) {

View File

@ -25,9 +25,9 @@ public final class Table extends Range
private int _tableLevel; private int _tableLevel;
Table( int startIdx, int endIdx, Range parent, int levelNum ) Table( int startIdxInclusive, int endIdxExclusive, Range parent, int levelNum )
{ {
super( startIdx, endIdx, Range.TYPE_PARAGRAPH, parent ); super( startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent );
_rows = new ArrayList<TableRow>(); _rows = new ArrayList<TableRow>();
_tableLevel = levelNum; _tableLevel = levelNum;
@ -41,8 +41,8 @@ public final class Table extends Range
rowEnd++; rowEnd++;
if ( p.isTableRowEnd() && p.getTableLevel() == levelNum ) if ( p.isTableRowEnd() && p.getTableLevel() == levelNum )
{ {
_rows.add( new TableRow( rowStart, rowEnd, this, levelNum ) ); _rows.add( new TableRow( rowStart, rowEnd + 1, this, levelNum ) );
rowStart = rowEnd; rowStart = rowEnd + 1;
} }
} }
} }

View File

@ -25,9 +25,11 @@ public final class TableCell
private int _leftEdge; private int _leftEdge;
private int _width; private int _width;
public TableCell(int startIdx, int endIdx, TableRow parent, int levelNum, TableCellDescriptor tcd, int leftEdge, int width) public TableCell( int startIdxInclusive, int endIdxExclusive,
TableRow parent, int levelNum, TableCellDescriptor tcd,
int leftEdge, int width )
{ {
super(startIdx, endIdx, Range.TYPE_PARAGRAPH, parent); super( startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent );
_tcd = tcd; _tcd = tcd;
_leftEdge = leftEdge; _leftEdge = leftEdge;
_width = width; _width = width;

View File

@ -17,10 +17,14 @@
package org.apache.poi.hwpf.usermodel; package org.apache.poi.hwpf.usermodel;
import org.apache.poi.hwpf.sprm.TableSprmUncompressor; import java.util.ArrayList;
import java.util.List;
public final class TableRow import org.apache.poi.hwpf.sprm.TableSprmUncompressor;
extends Paragraph import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
public final class TableRow extends Paragraph
{ {
private final static char TABLE_CELL_MARK = '\u0007'; private final static char TABLE_CELL_MARK = '\u0007';
@ -30,49 +34,81 @@ public final class TableRow
private final static short SPRM_FTABLEHEADER = 0x3404; private final static short SPRM_FTABLEHEADER = 0x3404;
private final static short SPRM_DYAROWHEIGHT = (short) 0x9407; private final static short SPRM_DYAROWHEIGHT = (short) 0x9407;
private static final POILogger logger = POILogFactory
.getLogger( TableRow.class );
int _levelNum; int _levelNum;
private TableProperties _tprops; private TableProperties _tprops;
private TableCell[] _cells; private TableCell[] _cells;
public TableRow(int startIdx, int endIdx, Table parent, int levelNum) public TableRow( int startIdxInclusive, int endIdxExclusive, Table parent,
int levelNum )
{ {
super(startIdx, endIdx, parent); super( startIdxInclusive, endIdxExclusive, parent );
_tprops = TableSprmUncompressor.uncompressTAP( _papx.toByteArray(), 2 ); _tprops = TableSprmUncompressor.uncompressTAP( _papx.toByteArray(), 2 );
_levelNum = levelNum; _levelNum = levelNum;
_cells = new TableCell[_tprops.getItcMac()]; final short expectedCellsCount = _tprops.getItcMac();
int start = 0; int lastCellStart = 0;
int end = 0; List<TableCell> cells = new ArrayList<TableCell>( expectedCellsCount );
for ( int p = 0; p < (endIdxExclusive - startIdxInclusive); p++ )
for (int cellIndex = 0; cellIndex < _cells.length; cellIndex++)
{ {
Paragraph p = getParagraph(start); Paragraph paragraph = getParagraph( p );
String s = p.text(); String s = paragraph.text();
while (! ( s.length() > 0 && (s.charAt(s.length() - 1) == TABLE_CELL_MARK) || if ( s.length() > 0
p.isEmbeddedCellMark() && p.getTableLevel() == levelNum) && end < endIdx) && s.charAt( s.length() - 1 ) == TABLE_CELL_MARK
&& paragraph.getTableLevel() == levelNum )
{ {
end++; TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
p = getParagraph(end); && _tprops.getRgtc().length > cells.size() ? _tprops
s = p.text(); .getRgtc()[cells.size()] : new TableCellDescriptor();
final short leftEdge = _tprops.getRgdxaCenter() != null
&& _tprops.getRgdxaCenter().length > cells.size() ? _tprops
.getRgdxaCenter()[cells.size()] : 0;
final short rightEdge = _tprops.getRgdxaCenter() != null
&& _tprops.getRgdxaCenter().length > cells.size() + 1 ? _tprops
.getRgdxaCenter()[cells.size() + 1] : 0;
TableCell tableCell = new TableCell( lastCellStart, p + 1,
this, levelNum, tableCellDescriptor, leftEdge,
rightEdge - leftEdge );
cells.add( tableCell );
lastCellStart = p + 1;
}
} }
// Create it for the correct paragraph range if ( lastCellStart < (endIdxExclusive - startIdxInclusive - 1) )
_cells[cellIndex] = new TableCell(start, end, this, levelNum, {
_tprops.getRgtc()[cellIndex], TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
_tprops.getRgdxaCenter()[cellIndex], && _tprops.getRgtc().length > cells.size() ? _tprops
_tprops.getRgdxaCenter()[cellIndex+1]-_tprops.getRgdxaCenter()[cellIndex]); .getRgtc()[cells.size()] : new TableCellDescriptor();
// Now we've decided where everything is, tweak the final short leftEdge = _tprops.getRgdxaCenter() != null
// record of the paragraph end so that the && _tprops.getRgdxaCenter().length > cells.size() ? _tprops
// paragraph level counts work .getRgdxaCenter()[cells.size()] : 0;
// This is a bit hacky, we really need a better fix... final short rightEdge = _tprops.getRgdxaCenter() != null
_cells[cellIndex]._parEnd++; && _tprops.getRgdxaCenter().length > cells.size() + 1 ? _tprops
.getRgdxaCenter()[cells.size() + 1] : 0;
// Next! TableCell tableCell = new TableCell( lastCellStart,
end++; (endIdxExclusive - startIdxInclusive - 1), this, levelNum,
start = end; tableCellDescriptor, leftEdge, rightEdge - leftEdge );
cells.add( tableCell );
} }
if ( cells.size() != expectedCellsCount )
{
logger.log( POILogger.WARN,
"Number of found table cells (" + cells.size()
+ ") for table row [" + getStartOffset() + "c; "
+ getEndOffset()
+ "c] not equals to stored property value "
+ expectedCellsCount );
_tprops.setItcMac( (short) cells.size() );
}
_cells = cells.toArray( new TableCell[cells.size()] );
} }
public int getRowJustification() public int getRowJustification()
@ -140,31 +176,38 @@ public final class TableRow
return _cells[index]; return _cells[index];
} }
public BorderCode getTopBorder() { public BorderCode getTopBorder()
{
return _tprops.getBrcBottom(); return _tprops.getBrcBottom();
} }
public BorderCode getBottomBorder() { public BorderCode getBottomBorder()
{
return _tprops.getBrcBottom(); return _tprops.getBrcBottom();
} }
public BorderCode getLeftBorder() { public BorderCode getLeftBorder()
{
return _tprops.getBrcLeft(); return _tprops.getBrcLeft();
} }
public BorderCode getRightBorder() { public BorderCode getRightBorder()
{
return _tprops.getBrcRight(); return _tprops.getBrcRight();
} }
public BorderCode getHorizontalBorder() { public BorderCode getHorizontalBorder()
{
return _tprops.getBrcHorizontal(); return _tprops.getBrcHorizontal();
} }
public BorderCode getVerticalBorder() { public BorderCode getVerticalBorder()
{
return _tprops.getBrcVertical(); return _tprops.getBrcVertical();
} }
public BorderCode getBarBorder() { public BorderCode getBarBorder()
{
throw new UnsupportedOperationException( "not applicable for TableRow" ); throw new UnsupportedOperationException( "not applicable for TableRow" );
} }

View File

@ -40,7 +40,8 @@ public class TestWordToConverterSuite
/** /**
* YK: a quick hack to exclude failing documents from the suite. * YK: a quick hack to exclude failing documents from the suite.
*/ */
private static List<String> failingFiles = Arrays.asList(); private static List<String> failingFiles = Arrays
.asList( "ProblemExtracting.doc" );
public static Test suite() public static Test suite()
{ {

View File

@ -68,11 +68,6 @@ public class TestWordToHtmlConverter extends TestCase
assertTrue( result.substring( 0, 2000 ).contains( "<table>" ) ); assertTrue( result.substring( 0, 2000 ).contains( "<table>" ) );
} }
public void testBug33519() throws Exception
{
getHtmlText( "Bug33519.doc" );
}
public void testBug46610_2() throws Exception public void testBug46610_2() throws Exception
{ {
String result = getHtmlText( "Bug46610_2.doc" ); String result = getHtmlText( "Bug46610_2.doc" );

View File

@ -79,14 +79,14 @@ public final class TestRange extends TestCase
assertEquals( range.getStartOffset(), 0 ); assertEquals( range.getStartOffset(), 0 );
assertEquals( range.getEndOffset(), 766 ); assertEquals( range.getEndOffset(), 766 );
Paragraph lastInMainRange = range.getParagraph( range.numParagraphs() ); Paragraph lastInMainRange = range.getParagraph( range.numParagraphs() - 1);
assertTrue( lastInMainRange.getEndOffset() <= 766 ); assertTrue( lastInMainRange.getEndOffset() <= 766 );
Section section = range.getSection( 0 ); Section section = range.getSection( 0 );
assertTrue( section.getEndOffset() <= 766 ); assertTrue( section.getEndOffset() <= 766 );
Paragraph lastInMainSection = section.getParagraph( section Paragraph lastInMainSection = section.getParagraph( section
.numParagraphs() ); .numParagraphs() - 1);
assertTrue( lastInMainSection.getEndOffset() <= 766 ); assertTrue( lastInMainSection.getEndOffset() <= 766 );
} }
} }