pro.verron.officestamper.core.ObjectDeleter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of engine Show documentation
Show all versions of engine Show documentation
Office-stamper is a Java template engine for docx documents, forked from org.wickedsource.docx-stamper
package pro.verron.officestamper.core;
import jakarta.xml.bind.JAXBElement;
import org.docx4j.wml.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pro.verron.officestamper.api.OfficeStamperException;
import java.util.Iterator;
/**
* Utility class for deleting objects from a {@link Document}.
*
* @author Joseph Verron
* @author Tom Hombergs
* @version ${version}
* @since 1.0.0
*/
public class ObjectDeleter {
private static final Logger log = LoggerFactory.getLogger(ObjectDeleter.class);
private ObjectDeleter() {
throw new OfficeStamperException("Utility class shouldn't be instantiated");
}
/**
* Deletes the given paragraph from the document.
*
* @param paragraph the paragraph to delete.
*/
public static void deleteParagraph(P paragraph) {
if (paragraph.getParent() instanceof Tc parentCell) {
// paragraph within a table cell
ObjectDeleter.deleteFromCell(parentCell, paragraph);
} else {
((ContentAccessor) paragraph.getParent()).getContent().remove(paragraph);
}
}
/**
* Deletes the given table from the document.
*
* @param table the table to delete.
*/
public static void deleteTable(Tbl table) {
if (table.getParent() instanceof Tc parentCell) {
// nested table within a table cell
ObjectDeleter.deleteFromCell(parentCell, table);
} else {
// global table
((ContentAccessor) table.getParent()).getContent().remove(table.getParent());
// iterate through the containing list to find the jaxb element that contains the table.
for (Iterator