Class WmlUtils
java.lang.Object
pro.verron.officestamper.utils.WmlUtils
Utility class with methods to help in the interaction with WordprocessingMLPackage documents
and their elements, such as comments, parents, and child elements.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
ensureValidity
(org.docx4j.wml.Tc cell) Utility method to ensure the validity of a table cell by adding an empty paragraph if necessary.static List
<org.jvnet.jaxb2_commons.ppp.Child> extractCommentElements
(org.docx4j.openpackaging.packages.WordprocessingMLPackage document) Extracts a list of comment elements from the specified WordprocessingMLPackage document.static Optional
<org.docx4j.wml.Comments.Comment> findComment
(org.docx4j.openpackaging.packages.WordprocessingMLPackage document, BigInteger id) Finds a comment with the given ID in the specified WordprocessingMLPackage document.static <T> Optional
<T> getFirstParentWithClass
(org.jvnet.jaxb2_commons.ppp.Child child, Class<T> clazz, int depth) Attempts to find the first parent of a given child element that is an instance of the specified class within the defined search depth.static void
remove
(org.jvnet.jaxb2_commons.ppp.Child child) Removes the specified child element from its parent container.static boolean
serializable
(Object object) Checks if the given object is serializable to XML.
-
Method Details
-
getFirstParentWithClass
public static <T> Optional<T> getFirstParentWithClass(org.jvnet.jaxb2_commons.ppp.Child child, Class<T> clazz, int depth) Attempts to find the first parent of a given child element that is an instance of the specified class within the defined search depth.- Type Parameters:
T
- the type of the parent class to search for- Parameters:
child
- the child element from which the search for a parent begins.clazz
- the class type to match for the parentdepth
- the maximum amount levels to traverse up the parent hierarchy- Returns:
- an Optional containing the first parent matching the specified class, or an empty Optional if no match found.
-
extractCommentElements
public static List<org.jvnet.jaxb2_commons.ppp.Child> extractCommentElements(org.docx4j.openpackaging.packages.WordprocessingMLPackage document) Extracts a list of comment elements from the specified WordprocessingMLPackage document.- Parameters:
document
- the WordprocessingMLPackage document from which to extract comment elements- Returns:
- a list of Child objects representing the extracted comment elements
-
findComment
public static Optional<org.docx4j.wml.Comments.Comment> findComment(org.docx4j.openpackaging.packages.WordprocessingMLPackage document, BigInteger id) Finds a comment with the given ID in the specified WordprocessingMLPackage document.- Parameters:
document
- the WordprocessingMLPackage document to search for the commentid
- the ID of the comment to find- Returns:
- an Optional containing the Comment if found, or an empty Optional if not found.
-
remove
public static void remove(org.jvnet.jaxb2_commons.ppp.Child child) Removes the specified child element from its parent container. Depending on the type of the parent element, the removal process is delegated to the appropriate helper method. If the child is contained within a table cell and the cell is empty after removal, an empty paragraph is added to the cell.- Parameters:
child
- the child element to be removed- Throws:
OfficeStamperException
- if the parent of the child element is of an unexpected type
-
ensureValidity
public static void ensureValidity(org.docx4j.wml.Tc cell) Utility method to ensure the validity of a table cell by adding an empty paragraph if necessary.- Parameters:
cell
- the table cell to be checked and updated.
-
serializable
Checks if the given object is serializable to XML.- Parameters:
object
- the object to be checked for XML serialization- Returns:
- true if the object can be serialized to XML, false otherwise
-