Skip to content

Commit

Permalink
Add detailed logging for expression resolution
Browse files Browse the repository at this point in the history
Introduce a logger to ObjectResolver and add detailed debug messages to capture placeholder expressions and their resolutions. This change helps in tracking the replacement process, improving traceability and debugging of document processing.
  • Loading branch information
caring-coder committed Sep 20, 2024
1 parent 9a60a0a commit a753a43
Showing 1 changed file with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pro.verron.officestamper.api;

import org.docx4j.TextUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;

Expand All @@ -16,6 +18,8 @@
*/
public interface ObjectResolver {

Logger LOGGER = LoggerFactory.getLogger(ObjectResolver.class);

/**
* Resolves the expression in the given document with the provided object.
*
Expand All @@ -28,15 +32,15 @@ public interface ObjectResolver {
*
* @throws OfficeStamperException if no resolver is found for the object
*/
default R resolve(
DocxPart document,
Placeholder placeholder,
Object object
) {
default R resolve(DocxPart document, Placeholder placeholder, Object object) {
R resolution = resolve(document, placeholder.content(), object);
var msg = "Expression '{}' replaced by '{}' with resolver {}";
LoggerFactory.getLogger(ObjectResolver.class)
.debug(msg, placeholder, resolution, this);
if (LOGGER.isDebugEnabled()) {
var message = "Expression '{}' replaced by '{}' with resolver {}";
var expression = placeholder.expression();
var text = TextUtils.getText(resolution);
var resolverName = getClass().getSimpleName();
LOGGER.debug(message, expression, text, resolverName);
}
return resolution;
}

Expand All @@ -54,23 +58,15 @@ default R resolve(
*
* @throws OfficeStamperException if no resolver is found for the object
*/
default R resolve(
DocxPart docxPart,
String expression,
Object object
) {
default R resolve(DocxPart docxPart, String expression, Object object) {
return resolve(docxPart.document(), expression, object);
}

/**
* @deprecated replaced by {@link #resolve(DocxPart, String, Object)}
*/
@Deprecated(since = "2.3", forRemoval = true)
default R resolve(
WordprocessingMLPackage document,
String expression,
Object object
) {
default R resolve(WordprocessingMLPackage document, String expression, Object object) {
throw new OfficeStamperException("Should not be called, only legacy implementation might still override this");
}

Expand Down

0 comments on commit a753a43

Please sign in to comment.