001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.templating;
019
020import com.hubspot.jinjava.interpret.JinjavaInterpreter;
021import com.hubspot.jinjava.loader.ResourceLocator;
022import com.hubspot.jinjava.loader.ResourceNotFoundException;
023import io.stallion.Context;
024import io.stallion.services.Log;
025import io.stallion.utils.Literals;
026import io.stallion.utils.ResourceHelpers;
027import org.apache.commons.io.FileUtils;
028
029import java.io.File;
030import java.io.IOException;
031import java.net.URL;
032import java.nio.charset.Charset;
033import java.util.HashMap;
034import java.util.Map;
035
036public class JinjaResourceLocator implements ResourceLocator {
037    private static Map<String, String> templateSourceCache;
038
039    private String targetDirectory;
040
041    public static void clearCache() {
042        if (templateSourceCache != null) {
043            templateSourceCache = new HashMap<>();
044        }
045    }
046
047    public JinjaResourceLocator(String targetDirectory, boolean devMode) {
048        if (!targetDirectory.endsWith("/")) {
049            targetDirectory = targetDirectory + "/";
050        }
051        targetDirectory = targetDirectory + "templates/";
052        this.targetDirectory = targetDirectory;
053
054        if (!devMode) {
055            templateSourceCache = new HashMap<>();
056        }
057    }
058
059    @Override
060    public String getString(String s, Charset charset, JinjavaInterpreter jinjavaInterpreter) throws IOException {
061        if (templateSourceCache == null) {
062            String source = getStringDirect(s, charset);
063            if (source == null) {
064                throw new ResourceNotFoundException("Template not found: " + s);
065            }
066            return source;
067        }
068        Log.finest("load template: {0}", s);
069        String cacheKey = "jinjaTemplate" + Literals.GSEP + s;
070        if (templateSourceCache.containsKey(cacheKey) && !Context.getSettings().getDevMode()) {
071            return templateSourceCache.get(cacheKey);
072        }
073
074
075        String source = getStringDirect(s, charset);
076        if (source == null) {
077            throw new ResourceNotFoundException("Template not found: " + s);
078        }
079        templateSourceCache.put(cacheKey, source);
080        return source;
081
082    }
083
084    private String getStringDirect(String s, Charset charset) throws IOException {
085
086        if (s.contains("jar:") || s.contains("file:/")) {
087            URL url = new URL(s);
088            return ResourceHelpers.loadResourceFromUrl(url);
089        } else if (s.contains(":")) {
090            String[] parts = s.split(":", 2);
091            String pluginName = parts[0];
092            String pathName = parts[1];
093            if (pathName.startsWith("/")) {
094                pathName = pathName.substring(1);
095            }
096            String resourcePath = "/templates/" + pathName;
097            return ResourceHelpers.loadAssetResource(pluginName, resourcePath);
098        }
099        if (s.startsWith("/")) {
100            s = s.substring(1);
101        }
102        String path = targetDirectory + s;
103        File file = new File(path);
104        Log.finer("Load jinja template from path: {0}", file.toString());
105        if (!file.exists() || !file.isFile()) {
106            Log.fine("Jinja template does not exist: {0}", file.toString());
107            return null;
108        }
109
110        String source = FileUtils.readFileToString(new File(path), charset.name());
111        return source;
112
113    }
114}