Skip to content

Commit

Permalink
Introduce OfficeStamperErrorHandler for custom error handling
Browse files Browse the repository at this point in the history
Implemented a new interface, OfficeStamperErrorHandler, and integrated it into various classes to allow users to define custom error resolution strategies. This change replaces the use of Function<ReflectiveOperationException, TypedValue> with the new interface for more flexibility and improved error handling consistency.
  • Loading branch information
caring-coder committed Sep 17, 2024
1 parent 7550ae5 commit 65cf573
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import java.util.Map;
import java.util.function.Function;

/**
* Represents the configuration for the OfficeStamper class.
*/
public interface OfficeStamperConfiguration {

static UnresolvedExpressionHandler getUnresolvedExpressionHandler(OfficeStamperConfiguration configuration) {
return configuration.isFailOnUnresolvedExpression()
? OfficeStamperException::sneakyThrow
: exception -> null;
}

/**
* Checks if the failOnUnresolvedExpression flag is set to true or false.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pro.verron.officestamper.api;

import org.springframework.lang.Nullable;

public interface OfficeStamperErrorHandler {
@Nullable Object resolve(Exception exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pro.verron.officestamper.api;

public interface UnresolvedExpressionHandler
extends OfficeStamperErrorHandler {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
Expand All @@ -12,6 +11,7 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,7 +49,8 @@ public DocxStamper(OfficeStamperConfiguration configuration) {
configuration.getResolvers(),
configuration.getCommentProcessors(),
configuration.getPreprocessors(),
configuration.getSpelParserConfiguration());
configuration.getSpelParserConfiguration(),
OfficeStamperConfiguration.getUnresolvedExpressionHandler(configuration));
}

private DocxStamper(
Expand All @@ -63,23 +64,19 @@ private DocxStamper(
List<ObjectResolver> resolvers,
Map<Class<?>, Function<ParagraphPlaceholderReplacer, CommentProcessor>> configurationCommentProcessors,
List<PreProcessor> preprocessors,
SpelParserConfiguration spelParserConfiguration
SpelParserConfiguration spelParserConfiguration,
UnresolvedExpressionHandler unresolvedExpressionHandler
) {
var commentProcessors = new HashMap<Class<?>, Object>();

Function<ReflectiveOperationException, TypedValue> onResolutionFail = failOnUnresolvedExpression
? DocxStamper::throwException
: exception -> new TypedValue(null);

final StandardMethodResolver methodResolver = new StandardMethodResolver(commentProcessors,
expressionFunctions,
onResolutionFail);
unresolvedExpressionHandler);

var evaluationContext = new StandardEvaluationContext();
evaluationContextConfigurer.configureEvaluationContext(evaluationContext);
evaluationContext.addMethodResolver(methodResolver);


var expressionParser = new SpelExpressionParser(spelParserConfiguration);
var expressionResolver = new ExpressionResolver(evaluationContext, expressionParser);

Expand All @@ -100,18 +97,12 @@ private DocxStamper(
commentProcessors.put(aClass, value);
}


this.commentProcessorRegistrySupplier = source -> new CommentProcessorRegistry(source,
expressionResolver,
commentProcessors,
failOnUnresolvedExpression);

this.preprocessors = preprocessors.stream()
.toList();
}

private static TypedValue throwException(ReflectiveOperationException exception) {
throw new OfficeStamperException("Error calling method", exception);
this.preprocessors = new ArrayList<>(preprocessors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public class DocxStamperConfiguration
public DocxStamperConfiguration() {
CommentProcessorFactory pf = new CommentProcessorFactory(this);
commentProcessors.put(CommentProcessorFactory.IRepeatProcessor.class, pf::repeat);
commentProcessors.put(CommentProcessorFactory.IParagraphRepeatProcessor.class,
pf::repeatParagraph);
commentProcessors.put(CommentProcessorFactory.IParagraphRepeatProcessor.class, pf::repeatParagraph);
commentProcessors.put(CommentProcessorFactory.IRepeatDocPartProcessor.class, pf::repeatDocPart);
commentProcessors.put(CommentProcessorFactory.ITableResolver.class, pf::tableResolver);
commentProcessors.put(CommentProcessorFactory.IDisplayIfProcessor.class, pf::displayIf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public void resolveExpressions(DocxPart document, Object expressionContext) {
* @param context the context root
*/
@Override public void resolveExpressionsForParagraph(
DocxPart docxPart, Paragraph paragraph,
DocxPart docxPart,
Paragraph paragraph,
Object context
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.TypedValue;
import org.springframework.lang.NonNull;
import pro.verron.officestamper.api.UnresolvedExpressionHandler;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

/**
* This class is a wrapper around a method call which can be executed by the Spring Expression Language.
Expand All @@ -19,7 +19,7 @@
public class StandardMethodExecutor implements MethodExecutor {

private final Invoker invoker;
private final Function<ReflectiveOperationException, TypedValue> onFail;
private final UnresolvedExpressionHandler onFail;

/**
* <p>Constructor for StandardMethodExecutor.</p>
Expand All @@ -28,7 +28,7 @@ public class StandardMethodExecutor implements MethodExecutor {
* @param onFail a function that is called if the invoker throws an exception. The function may return a default
* value to be returned by the {@link #execute(EvaluationContext, Object, Object...)} method.
*/
public StandardMethodExecutor(Invoker invoker, Function<ReflectiveOperationException, TypedValue> onFail) {
public StandardMethodExecutor(Invoker invoker, UnresolvedExpressionHandler onFail) {
this.invoker = invoker;
this.onFail = onFail;
}
Expand All @@ -45,7 +45,7 @@ public TypedValue execute(
Object value = invoker.invoke(arguments);
return new TypedValue(value);
} catch (InvocationTargetException | IllegalAccessException e) {
return onFail.apply(e);
return new TypedValue(onFail.resolve(e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.TypedValue;
import org.springframework.lang.NonNull;
import pro.verron.officestamper.api.UnresolvedExpressionHandler;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
* Resolves methods that are used as expression functions or comment processors.
Expand All @@ -23,7 +22,7 @@
public class StandardMethodResolver implements MethodResolver {
private final Map<Class<?>, Object> commentProcessors;
private final Map<Class<?>, Object> expressionFunctions;
private final Function<ReflectiveOperationException, TypedValue> onFail;
private final UnresolvedExpressionHandler onFail;

/**
* <p>Constructor for StandardMethodResolver.</p>
Expand All @@ -35,7 +34,7 @@ public class StandardMethodResolver implements MethodResolver {
public StandardMethodResolver(
Map<Class<?>, Object> commentProcessors,
Map<Class<?>, Object> expressionFunctions,
Function<ReflectiveOperationException, TypedValue> onResolutionFail
UnresolvedExpressionHandler onResolutionFail
) {
this.commentProcessors = commentProcessors;
this.expressionFunctions = expressionFunctions;
Expand Down

0 comments on commit 65cf573

Please sign in to comment.