Skip to content

Commit

Permalink
refactor: remove DocumentWalker and integrate docx4j TraversalUtil
Browse files Browse the repository at this point in the history
Use docx4j TraversalUtil and ClassFinder to replace expressions in paragraphs.
This change simplifies the codebase by removing redundant classes and leveraging library utilities.
  • Loading branch information
caring-coder committed Nov 4, 2024
1 parent ff0d1af commit c3f2179
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 268 deletions.

This file was deleted.

159 changes: 0 additions & 159 deletions engine/src/main/java/pro/verron/officestamper/core/DocumentWalker.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package pro.verron.officestamper.preset.processors.repeat;

import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.ClassFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.Comments;
import org.docx4j.wml.P;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tr;
import org.springframework.lang.Nullable;
import pro.verron.officestamper.api.*;
import pro.verron.officestamper.core.CommentUtil;
import pro.verron.officestamper.core.StandardParagraph;
import pro.verron.officestamper.preset.CommentProcessorFactory;

import java.math.BigInteger;
Expand Down Expand Up @@ -82,10 +86,14 @@ private void repeatRows(DocxPart source) {
Comments.Comment comment = requireNonNull(commentWrapper.getComment());
BigInteger commentId = comment.getId();
CommentUtil.deleteCommentFromElements(rowClone.getContent(), commentId);
new ParagraphResolverDocumentWalker(source,
rowClone,
expressionContext,
this.placeholderReplacer).walk();
var classFinder = new ClassFinder(P.class);
TraversalUtil.visit(rowClone, classFinder);
var objects = classFinder.results;
for (Object object : objects) {
P result = (P) object;
StandardParagraph paragraph = StandardParagraph.from(source, result);
placeholderReplacer.resolveExpressionsForParagraph(source, paragraph, expressionContext);
}
changes.add(rowClone);
}
}
Expand All @@ -102,8 +110,8 @@ private void repeatRows(DocxPart source) {
/** {@inheritDoc} */
@Override public void repeatTableRow(@Nullable List<Object> objects) {
var tr = this.getParagraph()
.parent(Tr.class)
.orElseThrow(OfficeStamperException.throwing("This paragraph is not in a table row."));
.parent(Tr.class)
.orElseThrow(OfficeStamperException.throwing("This paragraph is not in a table row."));
tableRowsToRepeat.put(tr, objects);
tableRowsCommentsToRemove.put(tr, getCurrentCommentWrapper());
}
Expand Down

0 comments on commit c3f2179

Please sign in to comment.