diff options
author | Nick Burch <nick@apache.org> | 2008-03-13 13:03:25 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-03-13 13:03:25 +0000 |
commit | ed17ee30bb97bad885c48c7a90bba49294093484 (patch) | |
tree | 8412ac5331a27a8f94a5413e3ee4b5ec3a308120 /src/scratchpad | |
parent | c532455620f42677f3365284257f6682be0abe8d (diff) | |
download | poi-ed17ee30bb97bad885c48c7a90bba49294093484.tar.gz poi-ed17ee30bb97bad885c48c7a90bba49294093484.zip |
Patch from Raghu from bug #44580 - Fix bug #28627, where hwpf Range.delete() didn't work properly
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@636751 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad')
-rw-r--r-- | src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java | 14 | ||||
-rw-r--r-- | src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java | 7 | ||||
-rw-r--r-- | src/scratchpad/testcases/org/apache/poi/hwpf/data/Bug28627.doc | bin | 0 -> 19968 bytes | |||
-rw-r--r-- | src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java | 62 |
4 files changed, 68 insertions, 15 deletions
diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java index 593214b180..67c634d9f6 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java @@ -90,7 +90,19 @@ public class TextPiece extends PropertyNode implements Comparable public void adjustForDelete(int start, int length) { - + int myStart = getStart(); + int myEnd = getEnd(); + int end = start + length; + + /* do we have to delete from this text piece? */ + if (start <= myEnd && end >= myStart) { + /* find where the deleted area overlaps with this text piece */ + int overlapStart = Math.max(myStart, start); + int overlapEnd = Math.min(myEnd, end); + ((StringBuffer)_buf).delete(overlapStart, overlapEnd); + + super.adjustForDelete(start, length); + } } public int characterLength() 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 60e00f3256..f2d9a615f8 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -494,6 +494,7 @@ public class Range int numSections = _sections.size(); int numRuns = _characters.size(); int numParagraphs = _paragraphs.size(); + int numTextPieces = _text.size(); for (int x = _charStart; x < numRuns; x++) { @@ -512,6 +513,12 @@ public class Range SEPX sepx = (SEPX)_sections.get(x); sepx.adjustForDelete(_start, _end - _start); } + + for (int x = _textStart; x < numTextPieces; x++) + { + TextPiece piece = (TextPiece)_text.get(x); + piece.adjustForDelete(_start, _end - _start); + } } /** diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/data/Bug28627.doc b/src/scratchpad/testcases/org/apache/poi/hwpf/data/Bug28627.doc Binary files differnew file mode 100644 index 0000000000..91b031d1d7 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/data/Bug28627.doc diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java index e82c4d1304..23681486f3 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java @@ -16,19 +16,14 @@ */ package org.apache.poi.hwpf.usermodel; -import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; -import java.util.Iterator; -import java.util.List; +import java.io.FileOutputStream; + +import junit.framework.TestCase; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.model.StyleSheet; -import org.apache.poi.hwpf.model.TextPiece; -import org.apache.poi.hwpf.usermodel.Paragraph; -import org.apache.poi.hwpf.usermodel.Range; -import org.apache.poi.util.LittleEndian; - -import junit.framework.TestCase; /** * Test various problem documents @@ -36,16 +31,18 @@ import junit.framework.TestCase; * @author Nick Burch (nick at torchbox dot com) */ public class TestProblems extends TestCase { + private String dirname = System.getProperty("HWPF.testdata.path"); protected void setUp() throws Exception { - } - + } + /** * ListEntry passed no ListTable */ public void testListEntryNoListTable() throws Exception { - HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/ListEntryNoListTable.doc")); + HWPFDocument doc = new HWPFDocument(new FileInputStream( + new File(dirname, "ListEntryNoListTable.doc"))); Range r = doc.getRange(); StyleSheet styleSheet = doc.getStyleSheet(); @@ -62,7 +59,8 @@ public class TestProblems extends TestCase { * AIOOB for TableSprmUncompressor.unCompressTAPOperation */ public void testSprmAIOOB() throws Exception { - HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/AIOOB-Tap.doc")); + HWPFDocument doc = new HWPFDocument(new FileInputStream( + new File(dirname, "AIOOB-Tap.doc"))); Range r = doc.getRange(); StyleSheet styleSheet = doc.getStyleSheet(); @@ -79,7 +77,8 @@ public class TestProblems extends TestCase { * Test for TableCell not skipping the last paragraph */ public void testTableCellLastParagraph() throws Exception { - HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/Bug44292.doc")); + HWPFDocument doc = new HWPFDocument(new FileInputStream( + new File(dirname, "Bug44292.doc"))); Range r = doc.getRange(); //get the table @@ -104,4 +103,39 @@ public class TestProblems extends TestCase { // Last cell should have one paragraph assertEquals(1, cell.numParagraphs()); } + + public void testRangeDelete() throws Exception { + HWPFDocument doc = new HWPFDocument(new FileInputStream( + new File(dirname, "Bug28627.doc"))); + + Range range = doc.getRange(); + int numParagraphs = range.numParagraphs(); + + int totalLength = 0, deletedLength = 0; + + for (int i = 0; i < numParagraphs; i++) { + Paragraph para = range.getParagraph(i); + String text = para.text(); + + totalLength += text.length(); + if (text.indexOf("{delete me}") > -1) { + para.delete(); + deletedLength = text.length(); + } + } + + // check the text length after deletion + int newLength = 0; + range = doc.getRange(); + numParagraphs = range.numParagraphs(); + + for (int i = 0; i < numParagraphs; i++) { + Paragraph para = range.getParagraph(i); + String text = para.text(); + + newLength += text.length(); + } + + assertEquals(newLength, totalLength - deletedLength); + } } |