search = new TextNavigation("SIMPLE", doc);
int i = 0;
TextSelection item = null;
while (search.hasNext()) {
item = (TextSelection) search.nextSelection();
Paragraph paragraph = sourcedoc.getParagraphByIndex(0, true);
item.replaceWith(paragraph);
i++;
}
search = new TextNavigation("Hello1 from SIMPLE source document!", doc);
int j = 0;
while (search.hasNext()) {
search.nextSelection();
j++;
}
Assert.assertTrue(i == j);
// 2 Task1, #1 at the start of original Paragraph, #2 replace original
// Paragraph
search = new TextNavigation("Task1", doc);
i = 0;
while (search.hasNext()) {
item = (TextSelection) search.nextSelection();
Paragraph paragraph = sourcedoc.getParagraphByIndex(1, true);
item.replaceWith(paragraph);
i++;
}
search = new TextNavigation("Hello2 from source document!", doc);
j = 0;
while (search.hasNext()) {
search.nextSelection();
j++;
}
Assert.assertTrue(i == j);
// 1 Container, #1 at the end of original Paragraph,
search = new TextNavigation("Container", doc);
i = 0;
while (search.hasNext()) {
item = (TextSelection) search.nextSelection();
Paragraph paragraph = sourcedoc.getParagraphByIndex(2, true);
item.replaceWith(paragraph);
i++;
}
search = new TextNavigation("Hello3 from source document!", doc);
j = 0;
while (search.hasNext()) {
search.nextSelection();
j++;
}
Assert.assertTrue(i == j);
try {
doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithParagraphResult.odt"));
} catch (Exception e) {
Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
}
doc = (TextDocument) Document.loadDocument(ResourceUtilities
.getAbsolutePath(TEST_FILE));
search = new TextNavigation("<<target>>", doc);
i = 0;
try {
while (search.hasNext()) {
i++;
item = (TextSelection) search.nextSelection();
Paragraph paragraph = sourcedoc.getParagraphByIndex(1, true);
paragraph = item.replaceWith(paragraph);
Assert.assertNotNull(paragraph);
Assert.assertEquals(
TextExtractor.getText(paragraph.getOdfElement()),
"Hello2 from source document!");
}
doc.save(ResourceUtilities
.newTestOutputFile("TextSelectionReplacewithPararaphResult-BasicCases.odt"));
verifyReplaceWithParagraphByBasicCases("TextSelectionReplacewithPararaphResult-BasicCases.odt");