Skip to content

Commit

Permalink
Merge branch 'main' into renovate/org.pitest-pitest-maven-1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
caring-coder authored Dec 14, 2024
2 parents 54e1a3c + 8e40a5a commit a53a389
Show file tree
Hide file tree
Showing 38 changed files with 1,822 additions and 920 deletions.
8 changes: 4 additions & 4 deletions engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.1</version>
<version>3.11.2</version>
<configuration>
<defaultAuthor>Joseph Verron</defaultAuthor>
<defaultVersion>${project.version}</defaultVersion>
Expand Down Expand Up @@ -251,7 +251,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.1</version>
<version>3.11.2</version>
<reportSets>
<reportSet>
<reports>
Expand Down Expand Up @@ -296,7 +296,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>6.2.0</version>
<version>6.2.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
Expand All @@ -313,7 +313,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.0</version>
<version>6.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ public interface NeedsBiFunctionImpl<T, U> {
public interface NeedsTriFunctionImpl<T, U, V> {
void withImplementation(TriFunction<T, U, V, ?> function);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,9 @@ OfficeStamperConfiguration setSpelParserConfiguration(
<T, U, V> NeedsTriFunctionImpl<T, U, V> addCustomFunction(
String name, Class<T> class0, Class<U> class1, Class<V> class2
);

List<PostProcessor> getPostprocessors();

void addPostprocessor(PostProcessor postProcessor);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pro.verron.officestamper.api;

import org.springframework.util.function.ThrowingFunction;

import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -56,4 +59,8 @@ public OfficeStamperException() {
public static Supplier<OfficeStamperException> throwing(String message) {
return () -> new OfficeStamperException(message);
}

public static <T, U> Function<T, U> throwing(ThrowingFunction<T, U> function) {
return ThrowingFunction.of(function, OfficeStamperException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.docx4j.wml.P;
import org.docx4j.wml.R;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -107,5 +108,5 @@ public interface Paragraph {
*/
<T> Optional<T> parent(Class<T> aClass);

Optional<Comments.Comment> getComment();
Collection<Comments.Comment> getComment();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pro.verron.officestamper.api;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public interface PostProcessor {
void process(WordprocessingMLPackage document);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public <T> void runProcessors(T expressionContext) {
var comments = collectComments();
var runParent = StandardParagraph.from(source, (P) run.getParent());
var optional = runProcessorsOnRunComment(comments, expressionContext, run, runParent);
commentProcessors.commitChanges(source);
optional.ifPresent(proceedComments::add);
});
commentProcessors.commitChanges(source);

// we run the paragraph afterward so that the comments inside work before the whole paragraph comments
source.streamParagraphs()
.forEach(p -> {
var comments = collectComments();
var paragraphComment = p.getComment();
paragraphComment.ifPresent((pc -> {
paragraphComment.forEach((pc -> {
var optional = runProcessorsOnParagraphComment(comments, expressionContext, p, pc.getId());
commentProcessors.commitChanges(source);
optional.ifPresent(proceedComments::add);
Expand Down
45 changes: 23 additions & 22 deletions engine/src/main/java/pro/verron/officestamper/core/CommentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private CommentUtil() {
* @return Optional of the comment, if found, Optional.empty() otherwise.
*/
public static Optional<Comments.Comment> getCommentAround(R run, WordprocessingMLPackage document) {
ContentAccessor parent = (ContentAccessor) ((Child) run).getParent();
ContentAccessor parent = (ContentAccessor) run.getParent();
if (parent == null) return Optional.empty();
return getComment(run, document, parent);
}
Expand Down Expand Up @@ -99,16 +99,6 @@ private static Optional<Comments.Comment> findComment(WordprocessingMLPackage do

}

/**
* Retrieves the CommentsPart from the given Parts object.
*
* @param parts the Parts object containing the various parts of the document.
* @return an Optional containing the CommentsPart if found, or an empty Optional if not found.
*/
public static Optional<CommentsPart> getCommentsPart(Parts parts) {
return Optional.ofNullable((CommentsPart) parts.get(WORD_COMMENTS_PART_NAME));
}

/**
* Retrieves the comment associated with a given paragraph content within a WordprocessingMLPackage document.
*
Expand All @@ -118,7 +108,7 @@ public static Optional<CommentsPart> getCommentsPart(Parts parts) {
* @return an Optional containing the found comment, or Optional.empty() if no comment is associated with the given
* paragraph content.
*/
public static Optional<Comments.Comment> getCommentFor(
public static Collection<Comments.Comment> getCommentFor(
List<Object> paragraphContent, WordprocessingMLPackage document
) {
var comments = getCommentsPart(document.getParts()).map(CommentUtil::extractContent)
Expand All @@ -130,9 +120,28 @@ public static Optional<Comments.Comment> getCommentFor(
return paragraphContent.stream()
.filter(CommentRangeStart.class::isInstance)
.map(CommentRangeStart.class::cast)
.findFirst()
.map(CommentRangeStart::getId)
.flatMap(commentId -> findCommentById(comments, commentId));
.flatMap(commentId -> findCommentById(comments, commentId).stream())
.toList();
}

/**
* Retrieves the CommentsPart from the given Parts object.
*
* @param parts the Parts object containing the various parts of the document.
*
* @return an Optional containing the CommentsPart if found, or an empty Optional if not found.
*/
public static Optional<CommentsPart> getCommentsPart(Parts parts) {
return Optional.ofNullable((CommentsPart) parts.get(WORD_COMMENTS_PART_NAME));
}

public static Comments extractContent(CommentsPart commentsPart) {
try {
return commentsPart.getContents();
} catch (Docx4JException e) {
throw new OfficeStamperException("Error while searching comment.", e);
}
}

private static Optional<Comments.Comment> findCommentById(List<Comments.Comment> comments, BigInteger id) {
Expand Down Expand Up @@ -270,12 +279,4 @@ private static Comments extractComments(Set<Comment> commentChildren) {
}
return newComments(list);
}

public static Comments extractContent(CommentsPart commentsPart) {
try {
return commentsPart.getContents();
} catch (Docx4JException e) {
throw new OfficeStamperException("Error while searching comment.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.ClassFinder;
import org.docx4j.model.structure.HeaderFooterPolicy;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.JaxbXmlPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.utils.TraversalUtilVisitor;
import org.docx4j.wml.*;
import org.jvnet.jaxb2_commons.ppp.Child;
import org.springframework.lang.Nullable;
import org.springframework.util.function.ThrowingFunction;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.OfficeStamperException;

import java.util.*;
import java.util.stream.Stream;

import static java.util.Optional.ofNullable;
import static java.util.stream.Stream.Builder;
import static pro.verron.officestamper.utils.WmlFactory.newRun;

/**
Expand All @@ -30,10 +38,7 @@ private DocumentUtil() {
throw new OfficeStamperException("Utility classes shouldn't be instantiated");
}

public static <T> Stream<T> streamObjectElements(
DocxPart source,
Class<T> elementClass
) {
public static <T> Stream<T> streamObjectElements(DocxPart source, Class<T> elementClass) {
ClassFinder finder = new ClassFinder(elementClass);
TraversalUtil.visit(source.part(), finder);
return finder.results.stream()
Expand All @@ -60,13 +65,8 @@ public static List<Object> allElements(WordprocessingMLPackage subDocument) {
*
* @return a {@link Map} object
*/
public static Map<R, R> walkObjectsAndImportImages(
WordprocessingMLPackage source,
WordprocessingMLPackage target
) {
return walkObjectsAndImportImages(source.getMainDocumentPart(),
source,
target);
public static Map<R, R> walkObjectsAndImportImages(WordprocessingMLPackage source, WordprocessingMLPackage target) {
return walkObjectsAndImportImages(source.getMainDocumentPart(), source, target);
}

/**
Expand Down Expand Up @@ -122,10 +122,7 @@ private static boolean isImageRun(R run) {
.anyMatch(Drawing.class::isInstance);
}

private static BinaryPartAbstractImage tryCreateImagePart(
WordprocessingMLPackage destDocument,
byte[] imageData
) {
private static BinaryPartAbstractImage tryCreateImagePart(WordprocessingMLPackage destDocument, byte[] imageData) {
try {
return BinaryPartAbstractImage.createImagePart(destDocument, imageData);
} catch (Exception e) {
Expand All @@ -146,10 +143,8 @@ private static BinaryPartAbstractImage tryCreateImagePart(
public static ContentAccessor findSmallestCommonParent(Object o1, Object o2) {
if (depthElementSearch(o1, o2) && o2 instanceof ContentAccessor contentAccessor)
return findInsertableParent(contentAccessor);
else if (o2 instanceof Child child)
return findSmallestCommonParent(o1, child.getParent());
else
throw new OfficeStamperException();
else if (o2 instanceof Child child) return findSmallestCommonParent(o1, child.getParent());
else throw new OfficeStamperException();
}

/**
Expand All @@ -168,8 +163,7 @@ public static boolean depthElementSearch(Object searchTarget, Object content) {
else if (content instanceof ContentAccessor contentAccessor) {
for (Object object : contentAccessor.getContent()) {
Object unwrappedObject = XmlUtils.unwrap(object);
if (searchTarget.equals(unwrappedObject)
|| depthElementSearch(searchTarget, unwrappedObject)) {
if (searchTarget.equals(unwrappedObject) || depthElementSearch(searchTarget, unwrappedObject)) {
return true;
}
}
Expand All @@ -185,4 +179,38 @@ private static ContentAccessor findInsertableParent(Object searchFrom) {
default -> throw new OfficeStamperException("Unexpected parent " + searchFrom.getClass());
};
}

public static void visitDocument(WordprocessingMLPackage document, TraversalUtilVisitor<?> visitor) {
var mainDocumentPart = document.getMainDocumentPart();
TraversalUtil.visit(mainDocumentPart, visitor);
streamHeaderFooterPart(document).forEach(f -> TraversalUtil.visit(f, visitor));
visitPartIfExists(visitor, mainDocumentPart.getFootnotesPart());
visitPartIfExists(visitor, mainDocumentPart.getEndNotesPart());
}

private static Stream<Object> streamHeaderFooterPart(WordprocessingMLPackage document) {
return document.getDocumentModel()
.getSections()
.stream()
.map(SectionWrapper::getHeaderFooterPolicy)
.flatMap(DocumentUtil::extractHeaderFooterParts);
}

private static void visitPartIfExists(TraversalUtilVisitor<?> visitor, @Nullable JaxbXmlPart<?> part) {
ThrowingFunction<JaxbXmlPart<?>, Object> throwingFunction = JaxbXmlPart::getContents;
Optional.ofNullable(part)
.map(c -> throwingFunction.apply(c, OfficeStamperException::new))
.ifPresent(c -> TraversalUtil.visit(c, visitor));
}

private static Stream<JaxbXmlPart<?>> extractHeaderFooterParts(HeaderFooterPolicy hfp) {
Builder<JaxbXmlPart<?>> builder = Stream.builder();
ofNullable(hfp.getFirstHeader()).ifPresent(builder::add);
ofNullable(hfp.getDefaultHeader()).ifPresent(builder::add);
ofNullable(hfp.getEvenHeader()).ifPresent(builder::add);
ofNullable(hfp.getFirstFooter()).ifPresent(builder::add);
ofNullable(hfp.getDefaultFooter()).ifPresent(builder::add);
ofNullable(hfp.getEvenFooter()).ifPresent(builder::add);
return builder.build();
}
}
Loading

0 comments on commit a53a389

Please sign in to comment.