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

com.ovea.markup.mvel.MVEL2TemplateCompiler Maven / Gradle / Ivy

/**
 * Copyright (C) 2011 Ovea 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ovea.markup.mvel;

import com.ovea.markup.Template;
import com.ovea.markup.TemplateCompiler;
import com.ovea.markup.TemplateCompilerException;
import com.ovea.markup.util.Resource;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateRuntime;
import org.mvel2.templates.res.Node;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class MVEL2TemplateCompiler implements TemplateCompiler {

    @Override
    public Template compile(Resource location) throws TemplateCompilerException {
        try {
            return load(location);
        } catch (Exception e) {
            throw new TemplateCompilerException("Unable to compile template " + location + " : " + e.getMessage(), e);
        }
    }

    private Template load(final Resource location) throws IOException {
        final CompiledTemplate compiledTemplate = location.isFile() ?
            buildCompiledTemplate(location.toFile()) :
            buildCompiledTemplate(location.read());

        return new Template(location) {
            @Override
            public String merge(Object context, TemplateMergingCallback callback) {
                if (location.isURL()) {
                    try {
                        CompiledImportNode.setCurrentCallBack(callback);
                        CompiledImportNode.clearStack();
                        if (location.isFile()) {
                            CompiledImportNode.push(location.toFile().getParentFile().getPath());
                        } else {
                            String path = location.toURL().getPath();
                            path = path.substring(path.indexOf("!/") + 1);
                            CompiledImportNode.push("classpath:" + new File(path).getParentFile().getPath().replace('\\', '/'));
                        }
                        return TemplateRuntime.execute(compiledTemplate, context, new HashMap()).toString();
                    } finally {
                        CompiledImportNode.clearCurrentCallBack();
                        CompiledImportNode.clearStack();
                    }
                }
                return TemplateRuntime.execute(compiledTemplate, context).toString();
            }
        };
    }

    private CompiledTemplate buildCompiledTemplate(InputStream in) throws IOException {
        try {
            return org.mvel2.templates.TemplateCompiler.compileTemplate(in, customNodes());
        } finally {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }

    private CompiledTemplate buildCompiledTemplate(File file) throws IOException {
        return org.mvel2.templates.TemplateCompiler.compileTemplate(file, customNodes());
    }

    static Map> customNodes() {
        Map> map = new HashMap>();
        map.put("import", CompiledImportNode.class);
        return map;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy