diff options
author | Dominik Stadler <centic@apache.org> | 2024-07-07 06:40:36 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2024-07-07 06:40:36 +0000 |
commit | 894ef6e1bdbbd700979f8f6fd71aa53863ddedc3 (patch) | |
tree | 8cfca9115e904156a0f042108d149cfb18d078de /poi-ooxml/src | |
parent | 397cee3a1283893916e2c14c77dbd14566e468df (diff) | |
download | poi-894ef6e1bdbbd700979f8f6fd71aa53863ddedc3.tar.gz poi-894ef6e1bdbbd700979f8f6fd71aa53863ddedc3.zip |
Fix Paragraph.searchText
The result was wrong for certain combinations of runs and found positions.
The adjusted implementation fixes this.
Also add a number of additional tests to verify results are correct.
Closes #655
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1918986 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml/src')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java | 4 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java | 80 |
2 files changed, 67 insertions, 17 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 2a337337f6..044ce9a400 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -1653,12 +1653,12 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para int startRun = startPos.getRun(), startText = startPos.getText(), startChar = startPos.getChar(); - int beginRunPos = 0, candCharPos = 0; + int beginRunPos = 0, candCharPos = 0, beginTextPos = 0, beginCharPos = 0; boolean newList = false; CTR[] rArray = paragraph.getRArray(); for (int runPos = startRun; runPos < rArray.length; runPos++) { - int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos; + int textPos = 0, charPos; CTR ctRun = rArray[runPos]; try (XmlCursor c = ctRun.newCursor()) { diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java index e778dbf502..518734f4f6 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java @@ -19,6 +19,7 @@ package org.apache.poi.xwpf.usermodel; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -444,34 +445,34 @@ public final class TestXWPFParagraph { assertEquals(4, paragraph.getRuns().size()); assertEquals(5, paragraph.getIRuns().size()); - assertTrue(paragraph.getRuns().get(1) instanceof XWPFHyperlinkRun); - assertTrue(paragraph.getRuns().get(2) instanceof XWPFHyperlinkRun); - assertTrue(paragraph.getRuns().get(3) instanceof XWPFFieldRun); + assertInstanceOf(XWPFHyperlinkRun.class, paragraph.getRuns().get(1)); + assertInstanceOf(XWPFHyperlinkRun.class, paragraph.getRuns().get(2)); + assertInstanceOf(XWPFFieldRun.class, paragraph.getRuns().get(3)); - assertTrue(paragraph.getIRuns().get(1) instanceof XWPFSDT); - assertTrue(paragraph.getIRuns().get(2) instanceof XWPFHyperlinkRun); + assertInstanceOf(XWPFSDT.class, paragraph.getIRuns().get(1)); + assertInstanceOf(XWPFHyperlinkRun.class, paragraph.getIRuns().get(2)); paragraph.removeRun(1); assertEquals(3, paragraph.getRuns().size()); - assertTrue(paragraph.getRuns().get(1) instanceof XWPFHyperlinkRun); - assertTrue(paragraph.getRuns().get(2) instanceof XWPFFieldRun); + assertInstanceOf(XWPFHyperlinkRun.class, paragraph.getRuns().get(1)); + assertInstanceOf(XWPFFieldRun.class, paragraph.getRuns().get(2)); - assertTrue(paragraph.getIRuns().get(1) instanceof XWPFSDT); - assertTrue(paragraph.getIRuns().get(2) instanceof XWPFHyperlinkRun); + assertInstanceOf(XWPFSDT.class, paragraph.getIRuns().get(1)); + assertInstanceOf(XWPFHyperlinkRun.class, paragraph.getIRuns().get(2)); paragraph.removeRun(1); assertEquals(2, paragraph.getRuns().size()); - assertTrue(paragraph.getRuns().get(1) instanceof XWPFFieldRun); + assertInstanceOf(XWPFFieldRun.class, paragraph.getRuns().get(1)); - assertTrue(paragraph.getIRuns().get(1) instanceof XWPFSDT); - assertTrue(paragraph.getIRuns().get(2) instanceof XWPFFieldRun); + assertInstanceOf(XWPFSDT.class, paragraph.getIRuns().get(1)); + assertInstanceOf(XWPFFieldRun.class, paragraph.getIRuns().get(2)); paragraph.removeRun(0); assertEquals(1, paragraph.getRuns().size()); - assertTrue(paragraph.getRuns().get(0) instanceof XWPFFieldRun); + assertInstanceOf(XWPFFieldRun.class, paragraph.getRuns().get(0)); - assertTrue(paragraph.getIRuns().get(0) instanceof XWPFSDT); - assertTrue(paragraph.getIRuns().get(1) instanceof XWPFFieldRun); + assertInstanceOf(XWPFSDT.class, paragraph.getIRuns().get(0)); + assertInstanceOf(XWPFFieldRun.class, paragraph.getIRuns().get(1)); XWPFRun newRun = paragraph.insertNewRun(0); assertEquals(2, paragraph.getRuns().size()); @@ -874,4 +875,53 @@ public final class TestXWPFParagraph { // TODO Make it easier to change } } + + @Test + void testRunIsEmpty() throws Exception { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFParagraph p = doc.createParagraph(); + assertTrue(p.runsIsEmpty()); + p.createRun().setText("abc"); + assertFalse(p.runsIsEmpty()); + } + } + + @Test + void testSearchText() throws Exception { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFParagraph paragraph = doc.createParagraph(); + paragraph.createRun().setText("abc"); + paragraph.createRun().setText("de"); + paragraph.createRun().setText("f"); + paragraph.createRun().setText("g"); + checkSearchText(paragraph, "a", 0, 0, 0, 0, 0, 0); + checkSearchText(paragraph, "b", 0, 0, 0, 0, 1, 1); + checkSearchText(paragraph, "ab", 0, 0, 0, 0, 0, 1); + checkSearchText(paragraph, "abc", 0, 0, 0, 0, 0, 2); + checkSearchText(paragraph, "abcd", 0, 1, 0, 0, 0, 0); + checkSearchText(paragraph, "abcde", 0, 1, 0, 0, 0, 1); + checkSearchText(paragraph, "bcde", 0, 1, 0, 0, 1, 1); + checkSearchText(paragraph, "bcdef", 0, 2, 0, 0, 1, 0); + checkSearchText(paragraph, "bcdefg", 0, 3, 0, 0, 1, 0); + checkSearchText(paragraph, "cdefg", 0, 3, 0, 0, 2, 0); + checkSearchText(paragraph, "defg", 1, 3, 0, 0, 0, 0); + checkSearchText(paragraph, "d", 1, 1, 0, 0, 0, 0); + checkSearchText(paragraph, "de", 1, 1, 0, 0, 0, 1); + checkSearchText(paragraph, "ef", 1, 2, 0, 0, 1, 0); + checkSearchText(paragraph, "f", 2, 2, 0, 0, 0, 0); + checkSearchText(paragraph, "fg", 2, 3, 0, 0, 0, 0); + checkSearchText(paragraph, "g", 3, 3, 0, 0, 0, 0); + } + } + + private static void checkSearchText(XWPFParagraph paragraph, String search, int beginRun, int endRun, int beginText, int endText, + int beginChar, int endChar) { + TextSegment result = paragraph.searchText(search, new PositionInParagraph()); + assertEquals(beginRun, result.getBeginRun(), "beginRun"); + assertEquals(endRun, result.getEndRun(), "endRun"); + assertEquals(beginText, result.getBeginText(), "beginText"); + assertEquals(endText, result.getEndText(), "endText"); + assertEquals(beginChar, result.getBeginChar(), "beginChar"); + assertEquals(endChar, result.getEndChar(), "endChar"); + } } |