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

space.yizhu.record.template.Engine Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version


package space.yizhu.record.template;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import space.yizhu.kits.HashKit;
import space.yizhu.kits.StrKit;
import space.yizhu.kits.SyncWriteMap;
import space.yizhu.record.template.expr.ast.FieldGetter;
import space.yizhu.record.template.expr.ast.FieldKeyBuilder;
import space.yizhu.record.template.expr.ast.FieldKit;
import space.yizhu.record.template.expr.ast.MethodKit;
import space.yizhu.record.template.source.ClassPathSourceFactory;
import space.yizhu.record.template.source.ISource;
import space.yizhu.record.template.source.ISourceFactory;
import space.yizhu.record.template.source.StringSource;
import space.yizhu.record.template.stat.OutputDirectiveFactory;
import space.yizhu.record.template.stat.Parser;
import space.yizhu.record.template.stat.ast.Stat;


public class Engine {

    public static final String MAIN_ENGINE_NAME = "main";

    private static Engine MAIN_ENGINE;
    private static Map engineMap = new HashMap(64, 0.5F);

    
    static {
        MAIN_ENGINE = new Engine(MAIN_ENGINE_NAME);
        engineMap.put(MAIN_ENGINE_NAME, MAIN_ENGINE);
    }

    private String name;
    private boolean devMode = false;
    private EngineConfig config = new EngineConfig();
    private ISourceFactory sourceFactory = config.getSourceFactory();

    private Map templateCache = new SyncWriteMap(2048, 0.5F);

    
    public Engine() {
        this.name = "NO_NAME";
    }

    
    public Engine(String engineName) {
        this.name = engineName;
    }

    
    public static Engine use() {
        return MAIN_ENGINE;
    }

    
    public static Engine use(String engineName) {
        return engineMap.get(engineName);
    }

    
    public synchronized static Engine create(String engineName) {
        if (StrKit.isBlank(engineName)) {
            throw new IllegalArgumentException("Engine name can not be blank");
        }
        engineName = engineName.trim();
        if (engineMap.containsKey(engineName)) {
            throw new IllegalArgumentException("Engine already exists : " + engineName);
        }
        Engine newEngine = new Engine(engineName);
        engineMap.put(engineName, newEngine);
        return newEngine;
    }

    
    public synchronized static Engine remove(String engineName) {
        Engine removed = engineMap.remove(engineName);
        if (removed != null && MAIN_ENGINE_NAME.equals(removed.name)) {
            Engine.MAIN_ENGINE = null;
        }
        return removed;
    }

    
    public synchronized static void setMainEngine(Engine engine) {
        if (engine == null) {
            throw new IllegalArgumentException("Engine can not be null");
        }
        engine.name = Engine.MAIN_ENGINE_NAME;
        engineMap.put(Engine.MAIN_ENGINE_NAME, engine);
        Engine.MAIN_ENGINE = engine;
    }

    
    public Template getTemplate(String fileName) {
        if (fileName.charAt(0) != '/') {
            char[] arr = new char[fileName.length() + 1];
            fileName.getChars(0, fileName.length(), arr, 1);
            arr[0] = '/';
            fileName = new String(arr);
        }

        Template template = templateCache.get(fileName);
        if (template == null) {
            template = buildTemplateBySourceFactory(fileName);
            templateCache.put(fileName, template);
        } else if (devMode) {
            if (template.isModified()) {
                template = buildTemplateBySourceFactory(fileName);
                templateCache.put(fileName, template);
            }
        }
        return template;
    }

    private Template buildTemplateBySourceFactory(String fileName) {
        
        ISource source = sourceFactory.getSource(config.getBaseTemplatePath(), fileName, config.getEncoding());
        Env env = new Env(config);
        Parser parser = new Parser(env, source.getContent(), fileName);
        if (devMode) {
            env.addSource(source);
        }
        Stat stat = parser.parse();
        Template template = new Template(env, stat);
        return template;
    }

    
    public Template getTemplateByString(String content) {
        return getTemplateByString(content, false);
    }

    
    public Template getTemplateByString(String content, boolean cache) {
        if (!cache) {
            return buildTemplateBySource(new StringSource(content, cache));
        }

        String cacheKey = HashKit.md5(content);
        Template template = templateCache.get(cacheKey);
        if (template == null) {
            template = buildTemplateBySource(new StringSource(content, cache));
            templateCache.put(cacheKey, template);
        } else if (devMode) {
            if (template.isModified()) {
                template = buildTemplateBySource(new StringSource(content, cache));
                templateCache.put(cacheKey, template);
            }
        }
        return template;
    }

    
    public Template getTemplate(ISource source) {
        String cacheKey = source.getCacheKey();
        if (cacheKey == null) {    
            return buildTemplateBySource(source);
        }

        Template template = templateCache.get(cacheKey);
        if (template == null) {
            template = buildTemplateBySource(source);
            templateCache.put(cacheKey, template);
        } else if (devMode) {
            if (template.isModified()) {
                template = buildTemplateBySource(source);
                templateCache.put(cacheKey, template);
            }
        }
        return template;
    }

    private Template buildTemplateBySource(ISource source) {
        Env env = new Env(config);
        Parser parser = new Parser(env, source.getContent(), null);
        if (devMode) {
            env.addSource(source);
        }
        Stat stat = parser.parse();
        Template template = new Template(env, stat);
        return template;
    }

    
    public Engine addSharedFunction(String fileName) {
        config.addSharedFunction(fileName);
        return this;
    }

    
    public Engine addSharedFunction(ISource source) {
        config.addSharedFunction(source);
        return this;
    }

    
    public Engine addSharedFunction(String... fileNames) {
        config.addSharedFunction(fileNames);
        return this;
    }

    
    public Engine addSharedFunctionByString(String content) {
        config.addSharedFunctionByString(content);
        return this;
    }

    
    public Engine addSharedObject(String name, Object object) {
        config.addSharedObject(name, object);
        return this;
    }

    
    public Engine setOutputDirectiveFactory(OutputDirectiveFactory outputDirectiveFactory) {
        config.setOutputDirectiveFactory(outputDirectiveFactory);
        return this;
    }

    
    public Engine addDirective(String directiveName, Class directiveClass) {
        config.addDirective(directiveName, directiveClass);
        return this;
    }

    
    @Deprecated
    public Engine addDirective(String directiveName, Directive directive) {
        return addDirective(directiveName, directive.getClass());
    }

    
    public Engine removeDirective(String directiveName) {
        config.removeDirective(directiveName);
        return this;
    }

    
    public Engine addSharedMethod(Object sharedMethodFromObject) {
        config.addSharedMethod(sharedMethodFromObject);
        return this;
    }

    
    public Engine addSharedMethod(Class sharedMethodFromClass) {
        config.addSharedMethod(sharedMethodFromClass);
        return this;
    }

    
    public Engine addSharedStaticMethod(Class sharedStaticMethodFromClass) {
        config.addSharedStaticMethod(sharedStaticMethodFromClass);
        return this;
    }

    
    public Engine removeSharedMethod(String methodName) {
        config.removeSharedMethod(methodName);
        return this;
    }

    
    public Engine removeSharedMethod(Class clazz) {
        config.removeSharedMethod(clazz);
        return this;
    }

    
    public Engine removeSharedMethod(Method method) {
        config.removeSharedMethod(method);
        return this;
    }

    
    public void removeTemplateCache(String cacheKey) {
        templateCache.remove(cacheKey);
    }

    
    public void removeAllTemplateCache() {
        templateCache.clear();
    }

    public int getTemplateCacheSize() {
        return templateCache.size();
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return "Template Engine: " + name;
    }

    

    public EngineConfig getEngineConfig() {
        return config;
    }

    
    public Engine setDevMode(boolean devMode) {
        this.devMode = devMode;
        this.config.setDevMode(devMode);
        if (this.devMode) {
            removeAllTemplateCache();
        }
        return this;
    }

    public boolean getDevMode() {
        return devMode;
    }

    
    public Engine setSourceFactory(ISourceFactory sourceFactory) {
        this.config.setSourceFactory(sourceFactory);    
        this.sourceFactory = sourceFactory;
        return this;
    }

    
    public Engine setToClassPathSourceFactory() {
        return setSourceFactory(new ClassPathSourceFactory());
    }

    public ISourceFactory getSourceFactory() {
        return sourceFactory;
    }

    public Engine setBaseTemplatePath(String baseTemplatePath) {
        config.setBaseTemplatePath(baseTemplatePath);
        return this;
    }

    public String getBaseTemplatePath() {
        return config.getBaseTemplatePath();
    }

    public Engine setDatePattern(String datePattern) {
        config.setDatePattern(datePattern);
        return this;
    }

    public String getDatePattern() {
        return config.getDatePattern();
    }

    public Engine setEncoding(String encoding) {
        config.setEncoding(encoding);
        return this;
    }

    public String getEncoding() {
        return config.getEncoding();
    }

    public Engine setWriterBufferSize(int bufferSize) {
        config.setWriterBufferSize(bufferSize);
        return this;
    }

    
    public Engine setReloadModifiedSharedFunctionInDevMode(boolean reloadModifiedSharedFunctionInDevMode) {
        config.setReloadModifiedSharedFunctionInDevMode(reloadModifiedSharedFunctionInDevMode);
        return this;
    }

    public static void addExtensionMethod(Class targetClass, Object objectOfExtensionClass) {
        MethodKit.addExtensionMethod(targetClass, objectOfExtensionClass);
    }

    public static void addExtensionMethod(Class targetClass, Class extensionClass) {
        MethodKit.addExtensionMethod(targetClass, extensionClass);
    }

    public static void removeExtensionMethod(Class targetClass, Object objectOfExtensionClass) {
        MethodKit.removeExtensionMethod(targetClass, objectOfExtensionClass);
    }

    public static void removeExtensionMethod(Class targetClass, Class extensionClass) {
        MethodKit.removeExtensionMethod(targetClass, extensionClass);
    }

    
    public static void addFieldGetter(int index, FieldGetter fieldGetter) {
        FieldKit.addFieldGetter(index, fieldGetter);
    }

    public static void addFieldGetterToLast(FieldGetter fieldGetter) {
        FieldKit.addFieldGetterToLast(fieldGetter);
    }

    public static void addFieldGetterToFirst(FieldGetter fieldGetter) {
        FieldKit.addFieldGetterToFirst(fieldGetter);
    }

    public static void removeFieldGetter(Class fieldGetterClass) {
        FieldKit.removeFieldGetter(fieldGetterClass);
    }

    public static void setToFastFieldKeyBuilder() {
        FieldKeyBuilder.setToFastFieldKeyBuilder();
    }
}









© 2015 - 2025 Weber Informatics LLC | Privacy Policy