Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P #487

Merged
merged 26 commits into from
Oct 19, 2024
Merged

P #487

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
253bbc8
Fix bug in CommentUtil for commented run handling
caring-coder Oct 9, 2024
a77c766
Refactor null check in replaceWordWith method
caring-coder Oct 9, 2024
d0f4684
Refactor ReplaceWithProcessor for clarity and efficiency
caring-coder Oct 9, 2024
584e7ce
Refactor test method names for clarity
caring-coder Oct 9, 2024
36d0bdc
Refactor tests to use simplified configuration methods
caring-coder Oct 9, 2024
c1a40b3
Enhance Stringifier for better style differentiation
caring-coder Oct 9, 2024
12e61fe
Add remove method to Paragraph interface
caring-coder Oct 9, 2024
a6f40f4
Add replace method to Paragraph API
caring-coder Oct 9, 2024
88a64d6
Refactor paragraph handling to remove getP() API
caring-coder Oct 9, 2024
8345bb7
Add elements method to Paragraphs and simplify replace logic
caring-coder Oct 9, 2024
5d6e49b
Add TODO for ParagraphRepeatProcessor mapping improvement
caring-coder Oct 9, 2024
180d05d
Update TODO comments for clarity and consistency
caring-coder Oct 9, 2024
c3c2574
Add supplier method to OfficeStamperException
caring-coder Oct 13, 2024
bb02d15
Refactor preprocessors into separate classes
caring-coder Oct 19, 2024
42d686b
Add preprocessor to remove malformed comments in DOCX files
caring-coder Oct 19, 2024
a87e7be
Add Javadoc to ProcessorContext record
caring-coder Oct 19, 2024
4cc2236
Add @Nullable annotations to method parameters
caring-coder Oct 19, 2024
2041109
Refactor table row and cell assertions
caring-coder Oct 19, 2024
a185d22
Refactor paragraph methods to include source document context
caring-coder Oct 19, 2024
eeb9aab
Update displayIf, repeatParagraph, and toString methods
caring-coder Oct 19, 2024
986e920
Refactor: streamline removal methods in DisplayIfProcessor
caring-coder Oct 19, 2024
50d3029
Refactor asString() method in PowerpointParagraph
caring-coder Oct 19, 2024
7cf3faf
Refactor code to improve readability
caring-coder Oct 19, 2024
6fc3372
Refactor streamRun method in TextualDocxPart
caring-coder Oct 19, 2024
4fe73fd
Add new methods and refactor for cleaner API
caring-coder Oct 19, 2024
efe5abf
Remove unused import from DocxDocument.java
caring-coder Oct 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ public R getCurrentRun() {
this.currentRun = run;
}

//TODO replace api
@Override public Object getParent() {
return paragraph.parent();
}

public Paragraph getParagraph() {
return paragraph;
}
Expand All @@ -71,7 +66,7 @@ public Paragraph getParagraph() {
* @deprecated use {@link #setParagraph(Paragraph)} instead
*/
@Deprecated(since = "2.6", forRemoval = true) public void setParagraph(P paragraph) {
this.paragraph = StandardParagraph.from(paragraph);
this.paragraph = StandardParagraph.from((DocxPart) paragraph.getParent(), paragraph);
}

public void setParagraph(Paragraph paragraph) {
Expand Down
10 changes: 5 additions & 5 deletions engine/src/main/java/pro/verron/officestamper/api/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface Comment {
*
* @param commentRangeEnd the {@link CommentRangeEnd} object to set
*/
// TODO_LATER: Remove the setting method from interface to increase immutability
// TODO: Remove the setting method from interface to increase immutability
void setCommentRangeEnd(CommentRangeEnd commentRangeEnd);

/**
Expand All @@ -61,7 +61,7 @@ public interface Comment {
*
* @param commentRangeStart the CommentRangeStart object to set
*/
// TODO_LATER: Remove the setting method from interface to increase immutability
// TODO: Remove the setting method from interface to increase immutability
void setCommentRangeStart(CommentRangeStart commentRangeStart);

/**
Expand All @@ -76,7 +76,7 @@ public interface Comment {
*
* @param commentReference the comment reference to set
*/
// TODO_LATER: Remove the setting method from interface to increase immutability
// TODO: Remove the setting method from interface to increase immutability
void setCommentReference(R.CommentReference commentReference);

/**
Expand All @@ -91,7 +91,7 @@ public interface Comment {
*
* @param comments the set of Comment objects representing the children of the comment
*/
// TODO_LATER: Remove the setting method from interface to increase immutability
// TODO: Remove the setting method from interface to increase immutability
void setChildren(Set<Comment> comments);

/**
Expand All @@ -106,7 +106,7 @@ public interface Comment {
*
* @param comment the comment to set
*/
// TODO_LATER: Remove the setting method from interface to increase immutability
// TODO: Remove the setting method from interface to increase immutability
void setComment(Comments.Comment comment);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public interface CommentProcessor {
*/
void setCurrentRun(@Nullable R run);

Object getParent();

/**
* This method is called after all comments in the .docx template have been passed to the comment processor.
* All manipulations of the .docx document SHOULD BE done in this method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package pro.verron.officestamper.api;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;

import java.util.stream.Stream;

public interface DocxDocument {
WordprocessingMLPackage document();
Stream<DocxPart> streamParts(String type);
CommentsPart commentsPart();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pro.verron.officestamper.api;

import java.util.function.Supplier;

/**
* OfficeStamperException is a subclass of RuntimeException that represents an exception that can be thrown during the
* processing of an Office document using the OfficeStamper library.
Expand Down Expand Up @@ -51,4 +53,7 @@ public OfficeStamperException() {
super("Unexpected exception");
}

public static Supplier<OfficeStamperException> throwing(String message) {
return () -> new OfficeStamperException(message);
}
}
54 changes: 49 additions & 5 deletions engine/src/main/java/pro/verron/officestamper/api/Paragraph.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
package pro.verron.officestamper.api;

import org.docx4j.wml.Comments;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import pro.verron.officestamper.core.StandardComment;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
* The Paragraph interface represents a paragraph in a text document.
* It provides methods for replacing a placeholder within the paragraph and retrieving the paragraph as a string.
*/
public interface Paragraph {

StandardComment fakeComment(DocxPart source, Placeholder placeholder);
/**
* Creates a processor context for the given placeholder within this paragraph.
*
* @param placeholder The placeholder to create a context for.
* @return The processor context for the specified placeholder.
*/
ProcessorContext processorContext(Placeholder placeholder);

/**
* Replaces specified contiguous elements within the paragraph with new elements.
*
* @param toRemove The list of elements to be removed from the paragraph.
* @param toAdd The list of elements to be added to the paragraph.
*/
void replace(List<P> toRemove, List<P> toAdd);

R firstRun(); // TODO replace with API not exposing the docx4j API directly
/**
* Removes the paragraph from the document.
* This method is intended to be used when a paragraph needs to be deleted.
*/
void remove();

/**
* Retrieves the paragraph associated with this object.
* TODO replace with API not exposing the docx4j API directly
*
* @return the paragraph object
*
* @deprecated As of version 2.6, due to its direct exposure of the docx4j API. It is scheduled for removal in
* the future.
*/
@Deprecated(since = "2.6", forRemoval = true)
P getP(); // TODO replace with API not exposing the docx4j API directly

/**
Expand Down Expand Up @@ -61,7 +91,21 @@ public interface Paragraph {
*/
String asString();

List<Object> paragraphContent(); // TODO replace with API not exposing the docx4j API directly
/**
* Applies the specified consumer function to the paragraph content.
*
* @param pConsumer The consumer function to apply to the paragraph content.
*/
void apply(Consumer<P> pConsumer);

/**
* Retrieves the parent of the current paragraph that matches the specified class type.
*
* @param aClass The class type to match for the parent element.
* @param <T> The type of the parent element to be returned.
* @return An {@code Optional} containing the matched parent element if found, otherwise an empty {@code Optional}.
*/
<T> Optional<T> parent(Class<T> aClass);

Object parent(); // TODO replace with API not exposing the docx4j API directly
Optional<Comments.Comment> getComment();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import org.docx4j.wml.R;

public record ProcessorContext(
Paragraph paragraph, R run, Comment comment, Placeholder placeholder
) {}
/**
* Represents the context in which a processor operates in a text document.
* Contains information about the paragraph, run, comment, and placeholder being processed.
*
* @param paragraph The paragraph associated with this context.
* @param run The run object representing a run of text.
* @param comment The comment associated with this context.
* @param placeholder The placeholder being processed in this context.
*/
public record ProcessorContext(Paragraph paragraph, R run, Comment comment, Placeholder placeholder) {}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package pro.verron.officestamper.core;

import org.docx4j.TextUtils;
import org.docx4j.wml.CommentRangeEnd;
import org.docx4j.wml.CommentRangeStart;
import org.docx4j.wml.Comments;
import org.docx4j.wml.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pro.verron.officestamper.api.Comment;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.OfficeStamperException;

import java.math.BigInteger;
import java.util.*;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;

class CommentCollectorWalker
extends BaseDocumentWalker {
private static final Logger logger = LoggerFactory.getLogger(CommentCollectorWalker.class);
private final DocxPart document;
private final Map<BigInteger, Comment> allComments;
private final Queue<Comment> stack;
Expand Down Expand Up @@ -51,56 +44,9 @@ static Map<BigInteger, Comment> collectComments(DocxPart docxPart) {
.filter(comment -> allComments.containsKey(comment.getId()))
.forEach(comment -> allComments.get(comment.getId())
.setComment(comment));
return cleanMalformedComments(rootComments);
}

private static Map<BigInteger, Comment> cleanMalformedComments(Map<BigInteger, Comment> rootComments) {
Map<BigInteger, Comment> filteredCommentEntries = new HashMap<>();

rootComments.forEach((key, comment) -> {
if (isCommentMalformed(comment)) {
var commentContent = getCommentContent(comment);
logger.error("Skipping malformed comment, missing range start and/or range end : {}", commentContent);
}
else {
filteredCommentEntries.put(key, comment);
comment.setChildren(cleanMalformedComments(comment.getChildren()));
}
});
return filteredCommentEntries;
}

private static Set<Comment> cleanMalformedComments(Set<Comment> children) {
return children
.stream()
.filter(comment -> {
if (isCommentMalformed(comment)) {
var commentContent = getCommentContent(comment);
logger.error("Skipping malformed comment, missing range start and/or range end : {}",
commentContent);
return false;
}
comment.setChildren(cleanMalformedComments(comment.getChildren()));
return true;
})
.collect(toSet());
return new HashMap<>(rootComments);
}

private static boolean isCommentMalformed(Comment comment) {
return comment.getCommentRangeStart() == null
|| comment.getCommentRangeEnd() == null
|| comment.getComment() == null;
}

private static String getCommentContent(Comment comment) {
return comment.getComment() == null
? "<no content>"
: comment.getComment()
.getContent()
.stream()
.map(TextUtils::getText)
.collect(joining(""));
}

@Override
protected void onCommentRangeStart(CommentRangeStart commentRangeStart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -63,7 +62,7 @@ public <T> void runProcessors(T expressionContext) {
source.streamRun()
.forEach(run -> {
var comments = collectComments(source);
var runParent = StandardParagraph.from((P) run.getParent());
var runParent = StandardParagraph.from(source, (P) run.getParent());
var optional = runProcessorsOnRunComment(comments, expressionContext, run, runParent);
commentProcessors.commitChanges(source);
optional.ifPresent(proceedComments::add);
Expand All @@ -73,9 +72,12 @@ public <T> void runProcessors(T expressionContext) {
source.streamParagraphs()
.forEach(p -> {
var comments = collectComments(source);
var optional = runProcessorsOnParagraphComment(comments, expressionContext, p, p.paragraphContent());
commentProcessors.commitChanges(source);
optional.ifPresent(proceedComments::add);
var paragraphComment = p.getComment();
paragraphComment.ifPresent((pc -> {
var optional = runProcessorsOnParagraphComment(comments, expressionContext, p, pc.getId());
commentProcessors.commitChanges(source);
optional.ifPresent(proceedComments::add);
}));
});

source.streamParagraphs()
Expand Down Expand Up @@ -112,18 +114,18 @@ private <T> Optional<Comment> runProcessorsOnParagraphComment(
Map<BigInteger, Comment> comments,
T expressionContext,
Paragraph paragraph,
List<Object> paragraphContent
BigInteger paragraphCommentId
) {
return CommentUtil.getCommentFor(paragraphContent, source.document())
.flatMap(c -> Optional.ofNullable(comments.get(c.getId())))
.flatMap(c -> {
var context = new ProcessorContext(paragraph, null, c, c.asPlaceholder());
commentProcessors.setContext(context);
var comment = runCommentProcessors(expressionContext, c);
comments.remove(c.getComment()
.getId());
return comment;
});
if (!comments.containsKey(paragraphCommentId)) return Optional.empty();

var c = comments.get(paragraphCommentId);
var cPlaceholder = c.asPlaceholder();
var cComment = c.getComment();
var context = new ProcessorContext(paragraph, null, c, cPlaceholder);
commentProcessors.setContext(context);
var comment = runCommentProcessors(expressionContext, c);
comments.remove(cComment.getId());
return comment;
}

/**
Expand All @@ -135,10 +137,9 @@ private <T> Optional<Comment> runProcessorsOnParagraphComment(
* @param <T> type of the context root object
*/
private <T> void runProcessorsOnInlineContent(T context, Paragraph paragraph) {
var processorContexts = findProcessors(paragraph.asString())
.stream()
.map(p -> newProcessorContext(paragraph, p))
.toList();
var processorContexts = findProcessors(paragraph.asString()).stream()
.map(paragraph::processorContext)
.toList();
for (var processorContext : processorContexts) {
commentProcessors.setContext(processorContext);
var placeholder = processorContext.placeholder();
Expand All @@ -160,7 +161,7 @@ private <T> Optional<Comment> runCommentProcessors(T context, Comment comment) {
try {
expressionResolver.setContext(context);
expressionResolver.resolve(placeholder);
logger.debug("Comment '{}' successfully processed by a comment processor.", placeholder.expression());
logger.debug("Comment '{}' successfully processed by a comment processor.", placeholder);
return Optional.of(comment);
} catch (SpelEvaluationException | SpelParseException e) {
var message = "Comment '%s' failed to process.".formatted(placeholder.expression());
Expand All @@ -169,9 +170,4 @@ private <T> Optional<Comment> runCommentProcessors(T context, Comment comment) {
}
}

private ProcessorContext newProcessorContext(Paragraph paragraph, Placeholder placeholder) {
var firstRun = paragraph.firstRun();
var fakedComment = paragraph.fakeComment(source, placeholder);
return new ProcessorContext(paragraph, firstRun, fakedComment, placeholder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ else if (possibleComment != null && foundChild && unwrap(contentElement) instanc
}
// else restart
else {
possibleComment = null;
possibleComment = null;// TODO There is bug here when looking for a commented run and the run has
// ProofErr issues
foundChild = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static Pic getPic(R run) {
}

private String getImageRelPartName(String imageRelId) {
// TODO_LATER: find a better way to find image rel part name in source part store
// TODO: find a better way to find image rel part name in source part store
return wordprocessingMLPackage
.getMainDocumentPart()
.getRelationshipsPart()
Expand Down
Loading