Search in sources :

Example 1 with InvalidReferenceException

use of freemarker.core.InvalidReferenceException in project freemarker by apache.

the class MistakenlyPublicImportAPIsTest method testImportCopying.

@Test
public void testImportCopying() throws IOException, TemplateException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    StringTemplateLoader tl = new StringTemplateLoader();
    tl.putTemplate("imp1", "<#macro m>1</#macro>");
    tl.putTemplate("imp2", "<#assign x = 2><#macro m>${x}</#macro>");
    cfg.setTemplateLoader(tl);
    Template t1 = new Template(null, "<#import 'imp1' as i1><#import 'imp2' as i2>", cfg);
    List<LibraryLoad> imports = t1.getImports();
    assertEquals(2, imports.size());
    {
        Template t2 = new Template(null, "<@i1.m/><@i2.m/>", cfg);
        for (LibraryLoad libLoad : imports) {
            t2.addImport(libLoad);
        }
        try {
            t2.process(null, NullWriter.INSTANCE);
            fail();
        } catch (InvalidReferenceException e) {
            // Apparenly, it has never worked like this...
            assertEquals("i1", e.getBlamedExpressionString());
        }
    }
    // It works this way, though it has nothing to do with the problematic API-s:
    Environment env = t1.createProcessingEnvironment(null, NullWriter.INSTANCE);
    env.process();
    TemplateModel i1 = env.getVariable("i1");
    assertThat(i1, instanceOf(Namespace.class));
    TemplateModel i2 = env.getVariable("i2");
    assertThat(i2, instanceOf(Namespace.class));
    {
        Template t2 = new Template(null, "<@i1.m/>", cfg);
        StringWriter sw = new StringWriter();
        env = t2.createProcessingEnvironment(null, sw);
        env.setVariable("i1", i1);
        env.process();
        assertEquals("1", sw.toString());
    }
    {
        Template t2 = new Template(null, "<@i2.m/>", cfg);
        StringWriter sw = new StringWriter();
        env = t2.createProcessingEnvironment(null, sw);
        env.setVariable("i2", i2);
        try {
            env.process();
            assertEquals("2", sw.toString());
        } catch (NullPointerException e) {
        // Expected on 2.3.x, because it won't find the namespace for the macro
        // [2.4] Fix this "bug"
        }
    }
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) StringWriter(java.io.StringWriter) Environment(freemarker.core.Environment) LibraryLoad(freemarker.core.LibraryLoad) InvalidReferenceException(freemarker.core.InvalidReferenceException) Namespace(freemarker.core.Environment.Namespace) Test(org.junit.Test)

Example 2 with InvalidReferenceException

use of freemarker.core.InvalidReferenceException in project connect-utils by jcustenborder.

the class StructTemplate method executeInternal.

private String executeInternal(String templateName, Object value) {
    Preconditions.checkNotNull(templateName, "templateName cannot be null.");
    Preconditions.checkNotNull(value, "values cannot be null.");
    Template template;
    try {
        template = this.configuration.getTemplate(templateName);
    } catch (IOException ex) {
        throw new DataException(String.format("Exception thrown while loading template '%s'", templateName), ex);
    }
    try (StringWriter writer = new StringWriter()) {
        template.process(value, writer);
        return writer.toString();
    } catch (IOException e) {
        throw new ConnectException("Exception while processing template", e);
    } catch (InvalidReferenceException e) {
        throw new DataException(String.format("Exception thrown while processing template. Offending expression '%s'", e.getBlamedExpressionString()), e);
    } catch (TemplateException e) {
        throw new ConnectException("Exception while processing template", e);
    }
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) InvalidReferenceException(freemarker.core.InvalidReferenceException) Template(freemarker.template.Template) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 3 with InvalidReferenceException

use of freemarker.core.InvalidReferenceException in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method compileTemplate.

private String compileTemplate(final String templateName, Locale locale, Map<String, Object> container) throws IOException, TemplateException {
    class NotificationTemplateExceptionHandler implements TemplateExceptionHandler {

        @Override
        public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out) throws TemplateException {
            if (te instanceof InvalidReferenceException) {
                // skip undefined values
                logger.info("Undefined value found in the TemplateMessage " + templateName + ".", te);
            } else {
                throw te;
            }
        }
    }
    this.configuration.setTemplateExceptionHandler(new NotificationTemplateExceptionHandler());
    StringWriter stringWriter = new StringWriter(4096);
    Template freeMarkerTemplate = null;
    try {
        freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + locale.getLanguage(), locale);
    } catch (FileNotFoundException ex) {
        if (!(locale.equals(DEFAULT_LOCALE))) {
            // if we do not know the language, try to send it at least in default locale
            freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + DEFAULT_LOCALE.getLanguage(), DEFAULT_LOCALE);
            logger.info("There is no message with template " + templateName + " in locale " + locale.getLanguage() + ", therefore the message will be sent in " + DEFAULT_LOCALE.getLanguage() + " locale.");
        } else {
            throw ex;
        }
    }
    freeMarkerTemplate.process(container, stringWriter);
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) FileNotFoundException(java.io.FileNotFoundException) Environment(freemarker.core.Environment) TemplateExceptionHandler(freemarker.template.TemplateExceptionHandler) StringWriter(java.io.StringWriter) InvalidReferenceException(freemarker.core.InvalidReferenceException) Template(freemarker.template.Template)

Aggregations

InvalidReferenceException (freemarker.core.InvalidReferenceException)3 StringWriter (java.io.StringWriter)3 Environment (freemarker.core.Environment)2 Template (freemarker.template.Template)2 TemplateException (freemarker.template.TemplateException)2 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 Namespace (freemarker.core.Environment.Namespace)1 LibraryLoad (freemarker.core.LibraryLoad)1 TemplateExceptionHandler (freemarker.template.TemplateExceptionHandler)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConnectException (org.apache.kafka.connect.errors.ConnectException)1 DataException (org.apache.kafka.connect.errors.DataException)1 Test (org.junit.Test)1