mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
397cee3a12
commit
894ef6e1bd
|
@ -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()) {
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue