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

fr.whimtrip.ext.jwhthtmltopojo.HtmlToPojoEngine Maven / Gradle / Ivy

Go to download

Fully featured highly pluggable and customizable Java html to pojo reflection converter

There is a newer version: 1.0.2
Show newest version




package fr.whimtrip.ext.jwhthtmltopojo;

import fr.whimtrip.ext.jwhthtmltopojo.adapter.DefaultHtmlAdapterImpl;
import fr.whimtrip.ext.jwhthtmltopojo.intrf.HtmlAdapter;
import fr.whimtrip.ext.jwhthtmltopojo.intrf.HtmlAdapterFactory;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Coordinates binding between HTML values and Java objects.
 *
 * Part of project jwht-htmltopojo
 *
 * @author Louis-wht
 * @since 1.0.0
 */
public class HtmlToPojoEngine {
    private final Map> adapterCache;
    private final HtmlAdapterFactory htmlAdapterFactory;

    /**
     * Creates a new HtmlToPojoEngine instance.
     * @return a new HtmlToPojoEngine instance
     */
    public static HtmlToPojoEngine create() {
        return new HtmlToPojoEngine(new DefaultHtmlAdapterFactory());
    }

    /**
     * Creates a new HtmlToPojoEngine instance.
     * @param htmlAdapterFactory the factory that will instanciate HtmlAdapters
     * @return a new HtmlToPojoEngine instance
     */
    public static HtmlToPojoEngine create(HtmlAdapterFactory htmlAdapterFactory) {
        return new HtmlToPojoEngine(htmlAdapterFactory);
    }

    private HtmlToPojoEngine(@NotNull final HtmlAdapterFactory htmlAdapterFactory) {

        this.htmlAdapterFactory = htmlAdapterFactory;
        this.adapterCache = new LinkedHashMap<>();
    }

    /**
     * Returns a HTML adapter for {@code clazz}, creating it if necessary.
     * @param clazz Class for creating objects
     * @param  Class for creating objects
     * @return {@link HtmlAdapter} instance
     */
    @SuppressWarnings("unchecked")
    public  HtmlAdapter adapter(Class clazz) {
        String key = clazz.getName();
        if (adapterCache.containsKey(key)) {
            return (DefaultHtmlAdapterImpl) adapterCache.get(key);
        } else {
            HtmlAdapter htmlAdapter = htmlAdapterFactory.instanciateAdapter(this, clazz);
            adapterCache.put(key, htmlAdapter);
            return htmlAdapter;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy