| 1 | package pro.verron.officestamper.core; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
| 4 | import org.docx4j.openpackaging.parts.Part; | |
| 5 | import org.docx4j.wml.ContentAccessor; | |
| 6 | import pro.verron.officestamper.api.*; | |
| 7 | import pro.verron.officestamper.utils.svg.SvgUtils; | |
| 8 | ||
| 9 | import java.util.ArrayList; | |
| 10 | import java.util.List; | |
| 11 | import java.util.Map; | |
| 12 | ||
| 13 | import static org.docx4j.openpackaging.parts.relationships.Namespaces.FOOTER; | |
| 14 | import static org.docx4j.openpackaging.parts.relationships.Namespaces.HEADER; | |
| 15 | ||
| 16 | /// The [DocxStamper] class is an implementation of the [OfficeStamper] interface used to stamp DOCX templates with a | |
| 17 | /// context object and write the result to an output stream. | |
| 18 | /// | |
| 19 | /// @author Tom Hombergs | |
| 20 | /// @author Joseph Verron | |
| 21 | /// @since 1.0.0 | |
| 22 | public class DocxStamper | |
| 23 | implements OfficeStamper<WordprocessingMLPackage> { | |
| 24 | ||
| 25 | private final List<PreProcessor> preprocessors; | |
| 26 | private final List<PostProcessor> postprocessors; | |
| 27 | private final EngineFactory engineFactory; | |
| 28 | private final EvaluationContextFactory contextFactory; | |
| 29 | private final Map<Class<?>, Object> interfaceFunctions; | |
| 30 | private final List<CustomFunction> customFunctions; | |
| 31 | private final Map<Class<?>, CommentProcessorFactory> commentProcessors; | |
| 32 | ||
| 33 | /// Creates new [DocxStamper] with the given configuration. | |
| 34 | /// | |
| 35 | /// @param configuration the configuration to use for this [DocxStamper]. | |
| 36 | public DocxStamper(OfficeStamperConfiguration configuration) { | |
| 37 | this.contextFactory = configuration.getEvaluationContextFactory(); | |
| 38 | this.interfaceFunctions = configuration.getExpressionFunctions(); | |
| 39 | this.customFunctions = configuration.customFunctions(); | |
| 40 | this.commentProcessors = configuration.getCommentProcessors(); | |
| 41 | // Apply global SVG safe-mode preference early so that any SVG manipulations during stamping honor it. | |
| 42 |
2
1. <init> : removed call to pro/verron/officestamper/utils/svg/SvgUtils::disableSafeMode → NO_COVERAGE 2. <init> : negated conditional → TIMED_OUT |
if (SecurityMode.PERMISSIVE.equals(configuration.getSvgSecurityMode())) SvgUtils.disableSafeMode(); |
| 43 |
1
1. <init> : removed call to pro/verron/officestamper/utils/svg/SvgUtils::enableSafeMode → TIMED_OUT |
else SvgUtils.enableSafeMode(); |
| 44 | this.engineFactory = processorContext -> { | |
| 45 | var parserConfiguration = configuration.getParserConfiguration(); | |
| 46 | var exceptionResolver = configuration.getExceptionResolver(); | |
| 47 | var resolvers = configuration.getResolvers(); | |
| 48 | var registry = new ObjectResolverRegistry(resolvers); | |
| 49 | var traceabilityReporter = configuration.getTraceabilityReporter(); | |
| 50 |
1
1. lambda$new$0 : replaced return value with null for pro/verron/officestamper/core/DocxStamper::lambda$new$0 → KILLED |
return new Engine(parserConfiguration, exceptionResolver, registry, processorContext, traceabilityReporter); |
| 51 | }; | |
| 52 | this.preprocessors = new ArrayList<>(configuration.getPreprocessors()); | |
| 53 | this.postprocessors = new ArrayList<>(configuration.getPostprocessors()); | |
| 54 | } | |
| 55 | ||
| 56 | /// Reads in a .docx template and "stamps" it, using the specified context object to fill out any expressions it | |
| 57 | /// finds. | |
| 58 | /// | |
| 59 | /// In the .docx template you have the following options to influence the "stamping" process: | |
| 60 | /// - Use expressions like `${name}` or `${person.isOlderThan(18)}` in the template's text. These expressions are | |
| 61 | /// resolved against the contextRoot object you pass into this method and are replaced by the results. | |
| 62 | /// - Use comments within the .docx template to mark certain paragraphs to be manipulated. | |
| 63 | /// | |
| 64 | /// Within comments, you can put expressions in which you can use the following methods by default: | |
| 65 | /// - `displayParagraphIf(boolean)` to conditionally display paragraphs or not | |
| 66 | /// - `displayTableRowIf(boolean)` to conditionally display table rows or not | |
| 67 | /// - `displayTableIf(boolean)` to conditionally display whole tables or not | |
| 68 | /// - `repeatTableRow(List<Object>)` to create a new table row for each object in the list and resolve expressions | |
| 69 | /// within the table cells against one of the objects within the list. | |
| 70 | /// | |
| 71 | /// If you need a wider vocabulary of methods available in the comments, you can create your own [CommentProcessor] | |
| 72 | /// and register it via [OfficeStamperConfiguration#addCommentProcessor(Class, CommentProcessorFactory)]. | |
| 73 | /// | |
| 74 | /// @param document the .docx template to stamp | |
| 75 | /// @param contextRoot the context object to use for stamping | |
| 76 | /// @return the stamped document | |
| 77 | @Override | |
| 78 | public WordprocessingMLPackage stamp(WordprocessingMLPackage document, Object contextRoot) { | |
| 79 |
1
1. stamp : removed call to pro/verron/officestamper/core/DocxStamper::preprocess → KILLED |
preprocess(document); |
| 80 |
1
1. stamp : removed call to pro/verron/officestamper/core/DocxStamper::process → KILLED |
process(document, contextRoot); |
| 81 |
1
1. stamp : removed call to pro/verron/officestamper/core/DocxStamper::postprocess → KILLED |
postprocess(document); |
| 82 |
1
1. stamp : replaced return value with null for pro/verron/officestamper/core/DocxStamper::stamp → KILLED |
return document; |
| 83 | } | |
| 84 | ||
| 85 | private void preprocess(WordprocessingMLPackage document) { | |
| 86 |
2
1. preprocess : removed call to java/util/List::forEach → KILLED 2. lambda$preprocess$0 : removed call to pro/verron/officestamper/api/PreProcessor::process → KILLED |
preprocessors.forEach(processor -> processor.process(document)); |
| 87 | } | |
| 88 | ||
| 89 | private void process(WordprocessingMLPackage document, Object contextRoot) { | |
| 90 | var mainDocumentPart = document.getMainDocumentPart(); | |
| 91 | var mainPart = new TextualDocxPart(document, mainDocumentPart, mainDocumentPart); | |
| 92 |
1
1. process : removed call to pro/verron/officestamper/core/DocxStamper::process → KILLED |
process(mainPart, contextRoot); |
| 93 | ||
| 94 | var relationshipsPart = mainDocumentPart.getRelationshipsPart(); | |
| 95 | for (var relationship : relationshipsPart.getRelationshipsByType(HEADER)) { | |
| 96 | Part part1 = relationshipsPart.getPart(relationship); | |
| 97 | TextualDocxPart textualDocxPart = new TextualDocxPart(document, part1, (ContentAccessor) part1); | |
| 98 |
1
1. process : removed call to pro/verron/officestamper/core/DocxStamper::process → KILLED |
process(textualDocxPart, contextRoot); |
| 99 | } | |
| 100 | ||
| 101 | for (var relationship : relationshipsPart.getRelationshipsByType(FOOTER)) { | |
| 102 | Part part = relationshipsPart.getPart(relationship); | |
| 103 | TextualDocxPart textualDocxPart = new TextualDocxPart(document, part, (ContentAccessor) part); | |
| 104 |
1
1. process : removed call to pro/verron/officestamper/core/DocxStamper::process → KILLED |
process(textualDocxPart, contextRoot); |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | private void postprocess(WordprocessingMLPackage document) { | |
| 109 |
2
1. postprocess : removed call to java/util/List::forEach → KILLED 2. lambda$postprocess$0 : removed call to pro/verron/officestamper/api/PostProcessor::process → KILLED |
postprocessors.forEach(processor -> processor.process(document)); |
| 110 | } | |
| 111 | ||
| 112 | private void process(DocxPart part, Object contextRoot) { | |
| 113 | var contextTree = new ContextRoot(contextRoot); | |
| 114 | var iterator = DocxHook.ofHooks(part::content, part); | |
| 115 |
1
1. process : negated conditional → KILLED |
while (iterator.hasNext()) { |
| 116 | var hook = iterator.next(); | |
| 117 | var officeStamperContextFactory = new OfficeStamperEvaluationContextFactory(customFunctions, | |
| 118 | commentProcessors, | |
| 119 | interfaceFunctions, | |
| 120 | contextFactory); | |
| 121 |
1
1. process : negated conditional → TIMED_OUT |
if (hook.run(engineFactory, contextTree, officeStamperContextFactory)) { |
| 122 |
1
1. process : removed call to pro/verron/officestamper/utils/iterator/ResetableIterator::reset → KILLED |
iterator.reset(); |
| 123 | } | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | } | |
Mutations | ||
| 42 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 50 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 86 |
1.1 2.2 |
|
| 92 |
1.1 |
|
| 98 |
1.1 |
|
| 104 |
1.1 |
|
| 109 |
1.1 2.2 |
|
| 115 |
1.1 |
|
| 121 |
1.1 |
|
| 122 |
1.1 |