pro.verron.officestamper.core.DocumentUtil 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.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.ClassFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.wml.*;
import org.jvnet.jaxb2_commons.ppp.Child;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.OfficeStamperException;
import java.util.*;
import java.util.stream.Stream;
/**
* Utility class to retrieve elements from a document.
*
* @author Joseph Verron
* @author DallanMC
* @version ${version}
* @since 1.4.7
*/
public class DocumentUtil {
private DocumentUtil() {
throw new OfficeStamperException("Utility classes shouldn't be instantiated");
}
public static Stream streamObjectElements(
DocxPart source,
Class elementClass
) {
ClassFinder finder = new ClassFinder(elementClass);
TraversalUtil.visit(source.part(), finder);
return finder.results.stream()
.map(elementClass::cast);
}
/**
* Retrieve the first element from an object.
*
* @param subDocument the object to get the first element from
*
* @return the first element
*/
public static Object lastElement(WordprocessingMLPackage subDocument) {
var mainDocumentPart = subDocument.getMainDocumentPart();
var mainDocumentPartContent = mainDocumentPart.getContent();
return mainDocumentPartContent.get(mainDocumentPartContent.size() - 1);
}
/**
* Retrieve the last element from an object.
*
* @param subDocument the object to get the last element from
*
* @return the last element
*/
public static List