Skip to content

Commit

Permalink
Step4 (#499)
Browse files Browse the repository at this point in the history
Introduce ProcessorContext
Let comments be treated as placeholder
  • Loading branch information
caring-coder authored Oct 19, 2024
2 parents b1040fa + d80d724 commit 214d1d8
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 350 deletions.
2 changes: 2 additions & 0 deletions engine/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@

opens pro.verron.officestamper.experimental to pro.verron.officestamper.test;
exports pro.verron.officestamper.experimental to pro.verron.officestamper.test;
exports pro.verron.officestamper.utils;
opens pro.verron.officestamper.utils;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public Comment getCurrentCommentWrapper() {
this.currentComment = currentComment;
}

@Override public void setProcessorContext(ProcessorContext processorContext) {
setParagraph(processorContext.paragraph());
setCurrentRun(processorContext.run());
setCurrentCommentWrapper(processorContext.comment());
}

public R getCurrentRun() {
return currentRun;
}
Expand All @@ -50,25 +56,25 @@ public R getCurrentRun() {
this.currentRun = run;
}

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

public Paragraph getParagraph() {
return paragraph;
}

/**
*
* @param paragraph coordinates of the currently processed paragraph within the template.
*
* @deprecated use {@link #setParagraph(Paragraph)} instead
*/
@Deprecated(since = "2.6", forRemoval = true) public void setParagraph(P paragraph) {
this.paragraph = StandardParagraph.from(paragraph);
}

@Override public void setParagraph(Paragraph paragraph) {
public void setParagraph(Paragraph paragraph) {
this.paragraph = paragraph;
}

public Paragraph getParagraph() {
return paragraph;
}

@Override public Object getParent() {
return paragraph.getP()
.getParent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.*;
import pro.verron.officestamper.core.Placeholders;
import pro.verron.officestamper.core.StandardParagraph;

import java.util.List;
import java.util.Set;
Expand All @@ -11,6 +13,13 @@
*/
public interface Comment {

/**
* Converts the comment to a Placeholder representation.
*
* @return the Placeholder representation of the comment
*/
Placeholder asPlaceholder();

/**
* Retrieves the parent of the comment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
*/
public interface CommentProcessor {

default void setProcessorContext(
Paragraph paragraph, @Nullable R run, @Nullable Comment comment
) {
setParagraph(paragraph);
setCurrentRun(run);
setCurrentCommentWrapper(comment);
}
void setProcessorContext(ProcessorContext processorContext);

/**
* Passes the run that is currently being processed (i.e., the run that is commented in the
Expand Down Expand Up @@ -69,10 +63,6 @@ default void commitChanges(DocxPart docxPart) {

Paragraph getParagraph();

default void setParagraph(Paragraph paragraph) {
setParagraph(paragraph.getP());
}

/**
* Passes the paragraph that is currently being processed (i.e., the paragraph that is commented in the
* .docx template). This method is always called BEFORE the custom
Expand All @@ -81,7 +71,7 @@ default void setParagraph(Paragraph paragraph) {
*
* @param paragraph coordinates of the currently processed paragraph within the template.
*
* @deprecated use {@link #setParagraph(Paragraph)} instead
* @deprecated use {@link #setProcessorContext(ProcessorContext)} instead
*/
@Deprecated(since = "2.6", forRemoval = true)
void setParagraph(P paragraph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.docx4j.openpackaging.parts.Part;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.P;
import org.docx4j.wml.R;

import java.util.List;
import java.util.stream.Stream;
Expand All @@ -12,5 +12,6 @@ public interface DocxPart
Part part();
DocxPart from(ContentAccessor accessor);
List<Object> content();
Stream<P> streamParagraphs();
Stream<Paragraph> streamParagraphs();
Stream<R> streamRun();
}
20 changes: 16 additions & 4 deletions engine/src/main/java/pro/verron/officestamper/api/Paragraph.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package pro.verron.officestamper.api;

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

import java.util.List;

/**
* 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);

Check warning on line 15 in engine/src/main/java/pro/verron/officestamper/api/Paragraph.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class is exposed outside of its visibility scope

Class `StandardComment` is not exported from module 'pro.verron.officestamper'

R firstRun(); // TODO replace with API not exposing the docx4j API directly

P getP(); // TODO replace with API not exposing the docx4j API directly

/**
* Replaces all occurrences of a placeholder with a specified replacement value within a paragraph.
*
Expand All @@ -17,8 +27,7 @@ public interface Paragraph {
* @deprecated was used by the core to deal with multiline paragraphs, users should fallback to
* {@link #replace(Placeholder, Object)} only
*/
@Deprecated(since = "2.4", forRemoval = true)
default void replaceAll(Placeholder placeholder, R replacement) {
@Deprecated(since = "2.4", forRemoval = true) default void replaceAll(Placeholder placeholder, R replacement) {
while (contains(placeholder.expression())) {
replace(placeholder, replacement);
}
Expand All @@ -33,8 +42,7 @@ default void replaceAll(Placeholder placeholder, R replacement) {
*
* @deprecated was used by the core to deal with multiline paragraphs
*/
@Deprecated(since = "2.4", forRemoval = true)
default boolean contains(String expression) {
@Deprecated(since = "2.4", forRemoval = true) default boolean contains(String expression) {
return asString().contains(expression);
}

Expand All @@ -52,4 +60,8 @@ default boolean contains(String expression) {
* @return the paragraph as a string
*/
String asString();

List<Object> paragraphContent(); // TODO replace with API not exposing the docx4j API directly

Object parent(); // TODO replace with API not exposing the docx4j API directly
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pro.verron.officestamper.api;

import org.docx4j.wml.R;

public record ProcessorContext(
Paragraph paragraph, R run, Comment comment, Placeholder placeholder
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Optional;

import static pro.verron.officestamper.core.CommentCollectorWalker.collectComments;
import static pro.verron.officestamper.core.Placeholders.findProcessors;

/**
* Allows registration of {@link CommentProcessor} objects. Each registered
Expand Down Expand Up @@ -89,7 +90,8 @@ private <T> Optional<Comment> runProcessorsOnRunComment(
return CommentUtil.getCommentAround(run, source.document())
.flatMap(c -> Optional.ofNullable(comments.get(c.getId())))
.flatMap(c -> {
commentProcessors.setContext(paragraph, run, c);
var context = new ProcessorContext(paragraph, run, c, c.asPlaceholder());
commentProcessors.setContext(context);
var comment = runCommentProcessors(expressionContext, c);
comments.remove(c.getComment()
.getId());
Expand All @@ -115,7 +117,8 @@ private <T> Optional<Comment> runProcessorsOnParagraphComment(
return CommentUtil.getCommentFor(paragraphContent, source.document())
.flatMap(c -> Optional.ofNullable(comments.get(c.getId())))
.flatMap(c -> {
commentProcessors.setContext(paragraph, null, c);
var context = new ProcessorContext(paragraph, null, c, c.asPlaceholder());
commentProcessors.setContext(context);
var comment = runCommentProcessors(expressionContext, c);
comments.remove(c.getComment()
.getId());
Expand All @@ -132,11 +135,13 @@ private <T> Optional<Comment> runProcessorsOnParagraphComment(
* @param <T> type of the context root object
*/
private <T> void runProcessorsOnInlineContent(T context, Paragraph paragraph) {
var text = paragraph.asString();
var placeholders = Placeholders.findProcessors(text);

for (var placeholder : placeholders) {
commentProcessors.setContext(source, paragraph, placeholder);
var processorContexts = findProcessors(paragraph.asString())
.stream()
.map(p -> newProcessorContext(paragraph, p))
.toList();
for (var processorContext : processorContexts) {
commentProcessors.setContext(processorContext);
var placeholder = processorContext.placeholder();
try {
expressionResolver.setContext(context);
expressionResolver.resolve(placeholder);
Expand All @@ -150,9 +155,7 @@ private <T> void runProcessorsOnInlineContent(T context, Paragraph paragraph) {
}
}

private <T> Optional<Comment> runCommentProcessors(
T context, Comment comment
) {
private <T> Optional<Comment> runCommentProcessors(T context, Comment comment) {
var placeholder = comment.asPlaceholder();
try {
expressionResolver.setContext(context);
Expand All @@ -165,4 +168,10 @@ private <T> Optional<Comment> runCommentProcessors(
return Optional.empty();
}
}

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
@@ -1,50 +1,23 @@
package pro.verron.officestamper.core;

import org.docx4j.wml.*;
import org.springframework.lang.Nullable;
import pro.verron.officestamper.api.*;
import pro.verron.officestamper.utils.WmlFactory;

import java.math.BigInteger;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;

public class CommentProcessors
extends AbstractMap<Class<?>, CommentProcessor> {

private final Map<Class<?>, CommentProcessor> processors;

public CommentProcessors(Map<Class<?>, CommentProcessor> processors) {
this.processors = processors;
}

void setContext(
DocxPart source,
Paragraph paragraph,
Placeholder placeholder
) {
var commentWrapper = new StandardComment(source.document());
commentWrapper.setComment(WmlFactory.newComment(placeholder.content()));
var commentRangeStart = new CommentRangeStart();
commentRangeStart.setId(BigInteger.TEN);
commentRangeStart.setParent(paragraph.getP());
commentWrapper.setCommentRangeStart(commentRangeStart);
var commentRangeEnd = new CommentRangeEnd();
commentRangeEnd.setId(BigInteger.TEN);
commentRangeEnd.setParent(paragraph.getP());
commentWrapper.setCommentRangeEnd(commentRangeEnd);
var commentReference = new R.CommentReference();
commentReference.setId(BigInteger.TEN);
commentReference.setParent(paragraph.getP());
commentWrapper.setCommentReference(commentReference);
var run = (R) paragraph.paragraphContent()
.get(0);
setContext(paragraph, run, commentWrapper);
}

public void setContext(Paragraph paragraph, @Nullable R run, Comment comment) {
public void setContext(ProcessorContext context) {
for (var processor : processors.values()) {
processor.setProcessorContext(paragraph, run, comment);
processor.setProcessorContext(context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.*;
import java.util.stream.Stream;

import static pro.verron.officestamper.utils.WmlFactory.newRun;

/**
* Utility class to retrieve elements from a document.
*
Expand All @@ -28,7 +30,6 @@ private DocumentUtil() {
throw new OfficeStamperException("Utility classes shouldn't be instantiated");
}


public static <T> Stream<T> streamObjectElements(
DocxPart source,
Class<T> elementClass
Expand Down Expand Up @@ -108,7 +109,7 @@ public static Map<R, R> walkObjectsAndImportImages(
var imageData = docxImageExtractor.getRunDrawingData(currentR);
var maxWidth = docxImageExtractor.getRunDrawingMaxWidth(currentR);
var imagePart = tryCreateImagePart(target, imageData);
var runWithImage = RunUtil.createRunWithImage(maxWidth, imagePart);
var runWithImage = newRun(maxWidth, imagePart, "dummyFileName", "dummyAltText");
replacements.put(currentR, runWithImage);
}
else if (currentObj instanceof ContentAccessor contentAccessor)
Expand Down

This file was deleted.

Loading

0 comments on commit 214d1d8

Please sign in to comment.