From c8bb280df04ca981e9dee4031e09b36e8cb14318 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Mon, 11 Jul 2011 21:59:33 +0000 Subject: [PATCH] make Table elements constructors accepts char positions (as other constructors); fix SprmBuffer usage for PAPX git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1145377 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/apache/poi/hwpf/model/PAPX.java | 2 +- .../apache/poi/hwpf/usermodel/Paragraph.java | 19 ++++--- .../org/apache/poi/hwpf/usermodel/Range.java | 49 +++++-------------- .../org/apache/poi/hwpf/usermodel/Table.java | 11 +++-- .../apache/poi/hwpf/usermodel/TableCell.java | 2 +- .../poi/hwpf/usermodel/TableIterator.java | 4 +- .../apache/poi/hwpf/usermodel/TableRow.java | 10 ++-- 7 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/PAPX.java b/src/scratchpad/src/org/apache/poi/hwpf/model/PAPX.java index 83ea717c9..5c9ab6088 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/PAPX.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/PAPX.java @@ -41,7 +41,7 @@ public final class PAPX extends BytePropertyNode { public PAPX(int fcStart, int fcEnd, CharIndexTranslator translator, byte[] papx, ParagraphHeight phe, byte[] dataStream) { - super(fcStart, fcEnd, translator, new SprmBuffer(papx, 0)); + super(fcStart, fcEnd, translator, new SprmBuffer(papx, 2)); _phe = phe; SprmBuffer buf = findHuge(new SprmBuffer(papx, 2), dataStream); if(buf != null) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java index ac3ce8986..9640bc5c0 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java @@ -84,14 +84,17 @@ public class Paragraph extends Range implements Cloneable { protected ParagraphProperties _props; protected SprmBuffer _papx; - protected Paragraph(int startIdxInclusive, int endIdxExclusive, Table parent) - { - super(startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent); - PAPX papx = _paragraphs.get(_parEnd - 1); - _props = papx.getParagraphProperties(_doc.getStyleSheet()); - _papx = papx.getSprmBuf(); - _istd = papx.getIstd(); - } + protected Paragraph( int startIdxInclusive, int endIdxExclusive, + Table parent ) + { + super( startIdxInclusive, endIdxExclusive, parent ); + + initAll(); + PAPX papx = _paragraphs.get( _parEnd - 1 ); + _props = papx.getParagraphProperties( _doc.getStyleSheet() ); + _papx = papx.getSprmBuf(); + _istd = papx.getIstd(); + } protected Paragraph(PAPX papx, Range parent) { diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index 92e00874f..03a0deb03 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -186,6 +186,7 @@ public class Range { // TODO -instantiable superclass * @param parent * The parent Range this range belongs to. */ + @Deprecated protected Range(int startIdx, int endIdx, int idxType, Range parent) { _doc = parent._doc; _sections = parent._sections; @@ -194,37 +195,6 @@ public class Range { // TODO -instantiable superclass _text = parent._text; _parent = new WeakReference(parent); - switch (idxType) { - case TYPE_PARAGRAPH: - _parStart = parent._parStart + startIdx; - _parEnd = parent._parStart + endIdx; - _start = _paragraphs.get(_parStart).getStart(); - _end = _paragraphs.get(_parEnd - 1).getEnd(); - _parRangeFound = true; - break; - case TYPE_CHARACTER: - _charStart = parent._charStart + startIdx; - _charEnd = parent._charStart + endIdx; - _start = _characters.get(_charStart - 1).getStart(); - _end = _characters.get(_charEnd).getEnd(); - _charRangeFound = true; - break; - case TYPE_SECTION: - _sectionStart = parent._sectionStart + startIdx; - _sectionEnd = parent._sectionStart + endIdx; - _start = _sections.get(_sectionStart - 1).getStart(); - _end = _sections.get(_sectionEnd).getEnd(); - _sectionRangeFound = true; - break; - case TYPE_TEXT: - _textStart = parent._textStart + startIdx; - _textEnd = parent._textStart + endIdx; - _start = _text.get(_textStart - 1).getStart(); - _end = _text.get(_textEnd).getEnd(); - _textRangeFound = true; - break; - } - sanityCheckStartEnd(); } @@ -534,7 +504,7 @@ public class Range { // TODO -instantiable superclass byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE]; LittleEndian.putShort(withIndex, (short) styleIndex); System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length); - SprmBuffer buf = new SprmBuffer(withIndex, 0); + SprmBuffer buf = new SprmBuffer(withIndex, 2); _doc.getParagraphTable().insert(_parStart, _start, buf); insertBefore(text, baseChp); @@ -584,7 +554,7 @@ public class Range { // TODO -instantiable superclass byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE]; LittleEndian.putShort(withIndex, (short) styleIndex); System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length); - SprmBuffer buf = new SprmBuffer(withIndex, 0); + SprmBuffer buf = new SprmBuffer(withIndex, 2); _doc.getParagraphTable().insert(_parEnd, _end, buf); _parEnd++; @@ -902,12 +872,12 @@ public class Range { // TODO -instantiable superclass } } - final Range overallrange = getDocument().getOverallRange(); + Range overallRange = _doc.getOverallRange(); int limit = _paragraphs.size(); for ( ; tableEndInclusive < limit - 1; tableEndInclusive++ ) { - Paragraph next = new Paragraph( _paragraphs.get( tableEndInclusive + 1 ), - overallrange ); + Paragraph next = new Paragraph( + _paragraphs.get( tableEndInclusive + 1 ), overallRange ); if ( !next.isInTable() || next.getTableLevel() < tableLevel ) break; } @@ -923,8 +893,11 @@ public class Range { // TODO -instantiable superclass throw new ArrayIndexOutOfBoundsException( "The table's end is negative, which isn't allowed!" ); } - return new Table( r._parStart, tableEndInclusive + 1, r._doc.getRange(), - paragraph.getTableLevel() ); + + int endOffsetExclusive = _paragraphs.get( tableEndInclusive ).getEnd(); + + return new Table( paragraph.getStartOffset(), endOffsetExclusive, + this, paragraph.getTableLevel() ); } /** diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Table.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Table.java index 246f01a5e..fb99df52f 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Table.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Table.java @@ -30,7 +30,7 @@ public final class Table extends Range Table( int startIdxInclusive, int endIdxExclusive, Range parent, int levelNum ) { - super( startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent ); + super( startIdxInclusive, endIdxExclusive, parent ); _tableLevel = levelNum; initRows(); } @@ -58,11 +58,14 @@ public final class Table extends Range int numParagraphs = numParagraphs(); while ( rowEnd < numParagraphs ) { - Paragraph p = getParagraph( rowEnd ); + Paragraph startRowP = getParagraph( rowStart ); + Paragraph endRowP = getParagraph( rowEnd ); rowEnd++; - if ( p.isTableRowEnd() && p.getTableLevel() == _tableLevel ) + if ( endRowP.isTableRowEnd() + && endRowP.getTableLevel() == _tableLevel ) { - _rows.add( new TableRow( rowStart, rowEnd, this, _tableLevel ) ); + _rows.add( new TableRow( startRowP.getStartOffset(), endRowP + .getEndOffset(), this, _tableLevel ) ); rowStart = rowEnd; } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java index e319307bf..83a7b0bce 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java @@ -29,7 +29,7 @@ public final class TableCell TableRow parent, int levelNum, TableCellDescriptor tcd, int leftEdge, int width ) { - super( startIdxInclusive, endIdxExclusive, Range.TYPE_PARAGRAPH, parent ); + super( startIdxInclusive, endIdxExclusive, parent ); _tcd = tcd; _leftEdge = leftEdge; _width = width; diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableIterator.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableIterator.java index 9f2e208e7..b2b11d288 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableIterator.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableIterator.java @@ -66,7 +66,9 @@ public final class TableIterator break; } } - return new Table(startIndex, endIndex, _range, _levelNum); + return new Table( _range.getParagraph( startIndex ).getStartOffset(), + _range.getParagraph( endIndex - 1 ).getEndOffset(), _range, + _levelNum ); } } 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 bd41a339d..16a2967bb 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java @@ -51,7 +51,8 @@ public final class TableRow extends Range super( startIdxInclusive, endIdxExclusive, parent ); Paragraph last = getParagraph( numParagraphs() - 1 ); - _tprops = TableSprmUncompressor.uncompressTAP( last._papx ); + _papx = last._papx; + _tprops = TableSprmUncompressor.uncompressTAP( _papx ); _levelNum = levelNum; initCells(); } @@ -146,9 +147,10 @@ public final class TableRow extends Range && _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 ); + TableCell tableCell = new TableCell( getParagraph( + lastCellStart ).getStartOffset(), getParagraph( p ) + .getEndOffset(), this, _levelNum, tableCellDescriptor, + leftEdge, rightEdge - leftEdge ); cells.add( tableCell ); lastCellStart = p + 1; }