Skip to content

Commit

Permalink
Remove paragraphs from tables
Browse files Browse the repository at this point in the history
Up until now, removing paragraphs from a table via
`removeIfExists` failed, as it was not found.
This is fixed in this release
  • Loading branch information
AntonOellerer committed Jun 10, 2024
1 parent 34dc258 commit af06346
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '3.0.0'
version = '3.0.1'

java {
toolchain {
Expand Down
41 changes: 17 additions & 24 deletions src/main/java/com/docutools/jocument/impl/word/WordUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -145,8 +144,8 @@ public static void removeIfExists(IBodyElement element) {
XmlObject object = getParentObject(xmlCursor);
if (object.equals(document.getBody())) {
findPositionInBody(element).ifPresent(pos -> body.getXWPFDocument().removeBodyElement(pos));
} else if (object instanceof CTTc ctTc) {
removeElementFromTable(element, ctTc, xmlCursor, body);
} else if (object instanceof CTTc) {
removeElementFromTable(xwpfParagraph);
} else if (object instanceof CTHdrFtr ctHdrFtr) {
xmlCursor.toParent();
new XWPFFooter(body.getXWPFDocument(), ctHdrFtr).removeParagraph(xwpfParagraph);
Expand All @@ -159,8 +158,8 @@ public static void removeIfExists(IBodyElement element) {
XmlObject object = getParentObject(xmlCursor);
if (object.equals(document.getBody())) {
findPositionInBody(element).ifPresent(pos -> body.getXWPFDocument().removeBodyElement(pos));
} else if (object instanceof CTTc ctTc) {
removeElementFromTable(element, ctTc, xmlCursor, body);
} else if (object instanceof CTTc) {
removeElementFromTable(xwpfTable);
} else if (object instanceof CTHdrFtr ctHdrFtr) {
xmlCursor.toParent();
new XWPFFooter(body.getXWPFDocument(), ctHdrFtr).removeTable(xwpfTable);
Expand All @@ -176,11 +175,16 @@ private static XmlObject getParentObject(XmlCursor xmlCursor) {
return xmlCursor.getObject();
}

private static void removeElementFromTable(IBodyElement element, CTTc ctTc, XmlCursor xmlCursor, IBody body) {
XmlObject rowObject = getParentObject(xmlCursor);
XmlObject tableObject = getParentObject(xmlCursor);
XWPFTableCell cell = new XWPFTableCell(ctTc, new XWPFTableRow((CTRow) rowObject, new XWPFTable((CTTbl) tableObject, body)), body);
findPositionInParagraphs(element, cell.getParagraphs()).ifPresent(cell::removeParagraph);
private static void removeElementFromTable(XWPFParagraph xwpfParagraph) {
while (!xwpfParagraph.runsIsEmpty()) {
xwpfParagraph.removeRun(0);
}
}

private static void removeElementFromTable(XWPFTable xwpfTable) {
for (int i = 0; i < xwpfTable.getNumberOfRows(); i++) {
xwpfTable.removeRow(0);
}
}

private static boolean findInHeader(IBodyElement element) {
Expand Down Expand Up @@ -262,17 +266,6 @@ private static boolean findInParagraphs(IBodyElement element, List<XWPFParagraph
return false;
}

private static OptionalInt findPositionInParagraphs(IBodyElement element, List<XWPFParagraph> paragraphs) {
var position = 0;
for (XWPFParagraph paragraph : paragraphs) {
if (element.equals(paragraph)) {
return OptionalInt.of(position);
}
position++;
}
return OptionalInt.empty();
}

private static boolean findInTables(IBodyElement element, List<XWPFTable> tables) {
for (XWPFTable nestedTable : tables) {
if ((element instanceof XWPFTable && element.equals(nestedTable)) || findInTable(element, nestedTable)) {
Expand Down Expand Up @@ -375,7 +368,7 @@ public static Collection<Locale> detectLanguages(XWPFDocument document) {
}

/**
* Get all of the paragraphs which are embedded in a table.
* Get all the paragraphs which are embedded in a table.
* This is done recursively, so paragraphs in tables in a table cell will also be found.
*
* @param table The table to check for embedded paragraphs
Expand All @@ -386,8 +379,8 @@ public static Collection<XWPFParagraph> getTableEmbeddedParagraphs(XWPFTable tab
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
paragraphs.addAll(cell.getParagraphs());
for (XWPFTable subtable : cell.getTables()) {
paragraphs.addAll(getTableEmbeddedParagraphs(subtable));
for (XWPFTable subTable : cell.getTables()) {
paragraphs.addAll(getTableEmbeddedParagraphs(subTable));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlAnySimpleType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -133,7 +132,6 @@ void shouldReplacePlaceholdersInTables() throws InterruptedException, IOExceptio
}

@Test
@Disabled("Pending apache poi 5.2.4 release")
@DisplayName("Replace custom placeholders in tables.")
void shouldReplaceCustomPlaceholderInTable() throws InterruptedException, IOException {
// Arrange
Expand Down

0 comments on commit af06346

Please sign in to comment.