Search in sources :

Example 1 with WebappTemplateLoader

use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.

the class InitParamParser method createTemplateLoader.

static TemplateLoader createTemplateLoader(String templatePath, Configuration cfg, Class classLoaderClass, ServletContext srvCtx) throws IOException {
    final int settingAssignmentsStart = findTemplatePathSettingAssignmentsStart(templatePath);
    String pureTemplatePath = (settingAssignmentsStart == -1 ? templatePath : templatePath.substring(0, settingAssignmentsStart)).trim();
    final TemplateLoader templateLoader;
    if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_CLASS)) {
        String packagePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_CLASS.length());
        packagePath = normalizeToAbsolutePackagePath(packagePath);
        templateLoader = new ClassTemplateLoader(classLoaderClass, packagePath);
    } else if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_CLASSPATH)) {
        // To be similar to Spring resource paths, we don't require "//":
        String packagePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_CLASSPATH.length());
        packagePath = normalizeToAbsolutePackagePath(packagePath);
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader == null) {
            LOG.warn("No Thread Context Class Loader was found. Falling back to the class loader of " + classLoaderClass.getName() + ".");
            classLoader = classLoaderClass.getClassLoader();
        }
        templateLoader = new ClassTemplateLoader(classLoader, packagePath);
    } else if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_FILE)) {
        String filePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_FILE.length());
        templateLoader = new FileTemplateLoader(new File(filePath));
    } else if (pureTemplatePath.startsWith("[") && cfg.getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22) {
        if (!pureTemplatePath.endsWith("]")) {
            // B.C. constraint: Can't throw any checked exceptions.
            throw new TemplatePathParsingException("Failed to parse template path; closing \"]\" is missing.");
        }
        String commaSepItems = pureTemplatePath.substring(1, pureTemplatePath.length() - 1).trim();
        List listItems = parseCommaSeparatedTemplatePaths(commaSepItems);
        TemplateLoader[] templateLoaders = new TemplateLoader[listItems.size()];
        for (int i = 0; i < listItems.size(); i++) {
            String pathItem = (String) listItems.get(i);
            templateLoaders[i] = createTemplateLoader(pathItem, cfg, classLoaderClass, srvCtx);
        }
        templateLoader = new MultiTemplateLoader(templateLoaders);
    } else if (pureTemplatePath.startsWith("{") && cfg.getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22) {
        throw new TemplatePathParsingException("Template paths starting with \"{\" are reseved for future purposes");
    } else {
        templateLoader = new WebappTemplateLoader(srvCtx, pureTemplatePath);
    }
    if (settingAssignmentsStart != -1) {
        try {
            int nextPos = _ObjectBuilderSettingEvaluator.configureBean(templatePath, templatePath.indexOf('(', settingAssignmentsStart) + 1, templateLoader, _SettingEvaluationEnvironment.getCurrent());
            if (nextPos != templatePath.length()) {
                throw new TemplatePathParsingException("Template path should end after the setting list in: " + templatePath);
            }
        } catch (Exception e) {
            throw new TemplatePathParsingException("Failed to set properties in: " + templatePath, e);
        }
    }
    return templateLoader;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) List(java.util.List) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 2 with WebappTemplateLoader

use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.

the class TemplateNotFoundMessageTest method testMultiTemplateLoader.

@Test
public void testMultiTemplateLoader() throws IOException {
    final String errMsg = failWith(new MultiTemplateLoader(new TemplateLoader[] { new WebappTemplateLoader(new MockServletContext(), "WEB-INF/templates"), new ClassTemplateLoader(this.getClass(), "foo/bar") }));
    showErrorMessage(errMsg);
    assertThat(errMsg, containsString("MultiTemplateLoader"));
    assertThat(errMsg, containsString("WebappTemplateLoader"));
    assertThat(errMsg, containsString("MyApp"));
    assertThat(errMsg, containsString("WEB-INF/templates"));
    assertThat(errMsg, containsString("ClassTemplateLoader"));
    assertThat(errMsg, containsString("foo/bar"));
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) StringTemplateLoader(freemarker.cache.StringTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) Test(org.junit.Test)

Example 3 with WebappTemplateLoader

use of freemarker.cache.WebappTemplateLoader in project drill by axbaretto.

the class DrillRestServer method getFreemarkerConfiguration.

/**
 * Creates freemarker configuration settings,
 * default output format to trigger auto-escaping policy
 * and template loaders.
 *
 * @param servletContext servlet context
 * @return freemarker configuration settings
 */
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
    configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
    List<TemplateLoader> loaders = new ArrayList<>();
    loaders.add(new WebappTemplateLoader(servletContext));
    loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
    try {
        loaders.add(new FileTemplateLoader(new File("/")));
    } catch (IOException e) {
        logger.error("Could not set up file template loader.", e);
    }
    configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
    return configuration;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) Configuration(freemarker.template.Configuration) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Example 4 with WebappTemplateLoader

use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.

the class TemplateNotFoundMessageTest method testWebappTemplateLoader.

@Test
public void testWebappTemplateLoader() throws IOException {
    final String errMsg = failWith(new WebappTemplateLoader(new MockServletContext(), "WEB-INF/templates"));
    showErrorMessage(errMsg);
    assertThat(errMsg, containsString("WebappTemplateLoader"));
    assertThat(errMsg, containsString("MyApp"));
    assertThat(errMsg, containsString("WEB-INF/templates"));
}
Also used : WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) Test(org.junit.Test)

Example 5 with WebappTemplateLoader

use of freemarker.cache.WebappTemplateLoader in project drill by apache.

the class DrillRestServer method getFreemarkerConfiguration.

/**
 * Creates freemarker configuration settings,
 * default output format to trigger auto-escaping policy
 * and template loaders.
 *
 * @param servletContext servlet context
 * @return freemarker configuration settings
 */
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
    configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
    List<TemplateLoader> loaders = new ArrayList<>();
    loaders.add(new WebappTemplateLoader(servletContext));
    loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
    try {
        loaders.add(new FileTemplateLoader(new File("/")));
    } catch (IOException e) {
        logger.error("Could not set up file template loader.", e);
    }
    configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
    return configuration;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) Configuration(freemarker.template.Configuration) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Aggregations

WebappTemplateLoader (freemarker.cache.WebappTemplateLoader)6 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)5 TemplateLoader (freemarker.cache.TemplateLoader)5 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)4 FileTemplateLoader (freemarker.cache.FileTemplateLoader)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Configuration (freemarker.template.Configuration)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 ParseException (java.text.ParseException)1 List (java.util.List)1 DelegatingTemplateLoader (org.ambraproject.wombat.config.DelegatingTemplateLoader)1 Theme (org.ambraproject.wombat.config.theme.Theme)1