Skip to content

Commit

Permalink
refactor: unify paragraph streaming logic and enable method chaining …
Browse files Browse the repository at this point in the history
…for configuration

Extract `streamParagraphs` logic into `DocumentUtil` to centralize functionality. Modify `addPreprocessor` and `addPostprocessor` methods to return `OfficeStamperConfiguration`, allowing method chaining.

This improves code reusability, readability, and consistency, while supporting fluent API design for configurations.
  • Loading branch information
caring-coder committed Dec 23, 2024
1 parent 73329a5 commit 4b15ab7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ OfficeStamperConfiguration addCommentProcessor(
*
* @param preprocessor the pre-processor to add
*/
void addPreprocessor(PreProcessor preprocessor);


OfficeStamperConfiguration addPreprocessor(PreProcessor preprocessor);

/**
* Retrieves the EvaluationContextConfigurer for configuring the Spring Expression Language (SPEL) EvaluationContext
Expand Down Expand Up @@ -152,5 +150,5 @@ <T, U, V> NeedsTriFunctionImpl<T, U, V> addCustomFunction(

List<PostProcessor> getPostprocessors();

void addPostprocessor(PostProcessor postProcessor);
OfficeStamperConfiguration addPostprocessor(PostProcessor postProcessor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.util.function.ThrowingFunction;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.OfficeStamperException;
import pro.verron.officestamper.api.Paragraph;

import java.util.*;
import java.util.stream.Stream;
Expand All @@ -38,13 +39,6 @@ private DocumentUtil() {
throw new OfficeStamperException("Utility classes shouldn't be instantiated");
}

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()
.map(elementClass::cast);
}

/**
* Retrieve the last element from an object.
*
Expand Down Expand Up @@ -213,4 +207,22 @@ private static Stream<JaxbXmlPart<?>> extractHeaderFooterParts(HeaderFooterPolic
ofNullable(hfp.getEvenFooter()).ifPresent(builder::add);
return builder.build();
}

static Stream<Paragraph> streamParagraphs(TextualDocxPart source) {
var paragraphs = streamObjectElements(source, P.class)
.map(p -> StandardParagraph.from(source, p));
var sdtRuns = streamObjectElements(source, SdtRun.class)
.map(SdtRun::getSdtContent)
.filter(CTSdtContentRun.class::isInstance)
.map(CTSdtContentRun.class::cast)
.map(paragraph -> StandardParagraph.from(source, paragraph));
return Stream.concat(paragraphs, sdtRuns);
}

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()
.map(elementClass::cast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void resetResolvers() {
///
/// @param interfaceClass the interface holding methods to expose in the expression language.
/// @param implementation the implementation to call to evaluate invocations of those methods.
/// Must implement the mentioned interface.
/// Must implement the mentioned interface.
///
/// @return a [DocxStamperConfiguration] object
@Override
Expand Down Expand Up @@ -96,9 +96,9 @@ public DocxStamperConfiguration addCommentProcessor(
///
/// @param preprocessor the preprocessor to add.
@Override
public void addPreprocessor(PreProcessor preprocessor) {
public OfficeStamperConfiguration addPreprocessor(PreProcessor preprocessor) {
preprocessors.add(preprocessor);

return this;
}

@Override
Expand Down Expand Up @@ -239,7 +239,8 @@ public List<PostProcessor> getPostprocessors() {
}

@Override
public void addPostprocessor(PostProcessor postprocessor) {
public OfficeStamperConfiguration addPostprocessor(PostProcessor postprocessor) {
postprocessors.add(postprocessor);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.*;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.Paragraph;

Expand Down Expand Up @@ -33,13 +35,7 @@ public TextualDocxPart(


public Stream<Paragraph> streamParagraphs() {
return Stream.concat(DocumentUtil.streamObjectElements(this, P.class)
.map(p -> StandardParagraph.from(this, p)),
DocumentUtil.streamObjectElements(this, SdtRun.class)
.map(SdtRun::getSdtContent)
.filter(CTSdtContentRun.class::isInstance)
.map(CTSdtContentRun.class::cast)
.map(paragraph -> StandardParagraph.from(this, paragraph)));
return DocumentUtil.streamParagraphs(this);
}

@Override public Stream<R> streamRun() {
Expand Down

0 comments on commit 4b15ab7

Please sign in to comment.