All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.verron.officestamper.core.ObjectResolverRegistry Maven / Gradle / Ivy

Go to download

Office-stamper is a Java template engine for docx documents, forked from org.wickedsource.docx-stamper

There is a newer version: 2.6.0
Show newest version
package pro.verron.officestamper.core;

import org.docx4j.wml.R;
import org.springframework.lang.Nullable;
import pro.verron.officestamper.api.DocxPart;
import pro.verron.officestamper.api.ObjectResolver;
import pro.verron.officestamper.api.OfficeStamperException;
import pro.verron.officestamper.api.Placeholder;

import java.util.ArrayList;
import java.util.List;

/**
 * A registry for object resolvers. It allows registering and resolving object resolvers based on certain criteria.
 *
 * @author Joseph Verron
 * @version ${version}
 * @since 1.6.7
 */
public final class ObjectResolverRegistry {
    private final List resolvers = new ArrayList<>();

    /**
     * A registry for object resolvers. It allows registering and resolving object resolvers based on certain criteria.
     *
     * @param resolvers the ordered list of object resolvers to be registered in
     *                  the registry
     */
    public ObjectResolverRegistry(List resolvers) {
        this.resolvers.addAll(resolvers);
    }

    /**
     * Resolves the expression in the given document with the provided object.
     *
     * @param document   the WordprocessingMLPackage document in which to resolve the placeholder
     * @param placeholder the expression value to be replaced
     * @param object     the object to be used for resolving the expression
     * @return the resolved value for the expression
     * @throws OfficeStamperException if no resolver is found for the object
     */
    public R resolve(
            DocxPart document,
            Placeholder placeholder,
            @Nullable Object object
    ) {
        for (ObjectResolver resolver : resolvers)
            if (resolver.canResolve(object))
                return resolver.resolve(document, placeholder, object);
        throw new OfficeStamperException("No resolver for %s".formatted(object));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy