| 1 | package pro.verron.officestamper.core; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | import org.slf4j.LoggerFactory; | |
| 5 | import org.springframework.expression.EvaluationContext; | |
| 6 | import org.springframework.expression.ExpressionParser; | |
| 7 | import org.springframework.expression.spel.SpelEvaluationException; | |
| 8 | import org.springframework.expression.spel.SpelParseException; | |
| 9 | import pro.verron.officestamper.api.ExceptionResolver; | |
| 10 | import pro.verron.officestamper.api.Insert; | |
| 11 | import pro.verron.officestamper.api.OfficeStamperException; | |
| 12 | import pro.verron.officestamper.api.ProcessorContext; | |
| 13 | ||
| 14 | /// The core engine of OfficeStamper, responsible for processing expressions. | |
| 15 | public class Engine { | |
| 16 | private static final Logger log = LoggerFactory.getLogger(Engine.class); | |
| 17 | ||
| 18 | private final ExpressionParser expressionParser; | |
| 19 | private final ExceptionResolver exceptionResolver; | |
| 20 | private final ObjectResolverRegistry objectResolverRegistry; | |
| 21 | private final ProcessorContext processorContext; | |
| 22 | ||
| 23 | /// Constructs an Engine. | |
| 24 | /// | |
| 25 | /// @param expressionParser the expression parser. | |
| 26 | /// @param exceptionResolver the exception resolver. | |
| 27 | /// @param objectResolverRegistry the object resolver registry. | |
| 28 | /// @param processorContext the processor context. | |
| 29 | public Engine( | |
| 30 | ExpressionParser expressionParser, | |
| 31 | ExceptionResolver exceptionResolver, | |
| 32 | ObjectResolverRegistry objectResolverRegistry, | |
| 33 | ProcessorContext processorContext | |
| 34 | ) { | |
| 35 | this.expressionParser = expressionParser; | |
| 36 | this.exceptionResolver = exceptionResolver; | |
| 37 | this.objectResolverRegistry = objectResolverRegistry; | |
| 38 | this.processorContext = processorContext; | |
| 39 | } | |
| 40 | ||
| 41 | /// Processes the provided evaluation context against the expression defined in the processor context. | |
| 42 | /// | |
| 43 | /// The method attempts to resolve an expression using the given evaluation context. | |
| 44 | /// | |
| 45 | /// If successful, the process completes and logs a debug message. | |
| 46 | /// | |
| 47 | /// Otherwise, on failure ([SpelEvaluationException] or [SpelParseException]), it handles the exception by invoking | |
| 48 | /// the exceptionResolver and logs an error. | |
| 49 | /// | |
| 50 | /// @param evaluationContext the evaluation context for processing the expression. | |
| 51 | /// | |
| 52 | /// @return true if the processing was successful, otherwise false | |
| 53 | public boolean process(EvaluationContext evaluationContext) { | |
| 54 | var expression = processorContext.expression(); | |
| 55 | try { | |
| 56 | var parsedExpression = expressionParser.parseExpression(expression); | |
| 57 | parsedExpression.getValue(evaluationContext); | |
| 58 | log.debug("Processed '{}' successfully.", expression); | |
| 59 |
1
1. process : replaced boolean return with false for pro/verron/officestamper/core/Engine::process → KILLED |
return true; |
| 60 | } catch (SpelEvaluationException | SpelParseException e) { | |
| 61 | var message = "Processing '%s' failed.".formatted(expression); | |
| 62 | exceptionResolver.resolve(expression, message, e); | |
| 63 |
1
1. process : replaced boolean return with true for pro/verron/officestamper/core/Engine::process → TIMED_OUT |
return false; |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | /// Resolves an [Insert] object by processing the provided evaluation context using the current processor context. | |
| 68 | /// Combines the processor context's part and expression with various resolvers to achieve the resolution. | |
| 69 | /// | |
| 70 | /// @param evaluationContext the evaluation context for processing the expression. | |
| 71 | /// | |
| 72 | /// @return an [Insert] object representing the resolved result of the expression within the context. | |
| 73 | public Insert resolve(EvaluationContext evaluationContext) { | |
| 74 | var part = processorContext.part(); | |
| 75 | var expression = processorContext.expression(); | |
| 76 | try { | |
| 77 | ||
| 78 | var parsedExpression = expressionParser.parseExpression(expression); | |
| 79 | var resolution = parsedExpression.getValue(evaluationContext); | |
| 80 |
1
1. resolve : replaced return value with null for pro/verron/officestamper/core/Engine::resolve → TIMED_OUT |
return objectResolverRegistry.resolve(part, expression, resolution); |
| 81 | } catch (SpelEvaluationException | SpelParseException | OfficeStamperException e) { | |
| 82 | var msgTemplate = "Expression %s could not be resolved against context '%s'"; | |
| 83 | var message = msgTemplate.formatted(expression, evaluationContext); | |
| 84 |
1
1. resolve : replaced return value with null for pro/verron/officestamper/core/Engine::resolve → TIMED_OUT |
return exceptionResolver.resolve(expression, message, e); |
| 85 | } | |
| 86 | } | |
| 87 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 63 |
1.1 |
|
| 80 |
1.1 |
|
| 84 |
1.1 |