Search in sources :

Example 6 with DefaultMustacheFactory

use of com.github.mustachejava.DefaultMustacheFactory in project webprotege by protegeproject.

the class PasswordResetMailer_IT method setUp.

@Before
public void setUp() throws Exception {
    when(placeUrl.getApplicationUrl()).thenReturn(applicationUrl);
    when(appNameProvider.get()).thenReturn(theAppName);
    OverridableFile overridableFile = new OverridableFile(TEMPLATE_PATH, new File("/tmp/data"));
    FileContents templateFile = new FileContents(overridableFile);
    TemplateEngine templateEngine = new TemplateEngine(DefaultMustacheFactory::new);
    ResetPasswordMailer mailer = new ResetPasswordMailer(sendMailImpl, templateEngine, templateFile, placeUrl, appNameProvider);
    mailer.sendEmail(userId, emailAddress, theNewPassword, e -> {
    });
    verify(sendMailImpl, times(1)).sendMail(eq(singletonList(emailAddress)), eq("Your password has been reset"), bodyCaptor.capture(), any(MessagingExceptionHandler.class));
}
Also used : TemplateEngine(edu.stanford.bmir.protege.web.server.templates.TemplateEngine) FileContents(edu.stanford.bmir.protege.web.server.filemanager.FileContents) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MessagingExceptionHandler(edu.stanford.bmir.protege.web.server.mail.MessagingExceptionHandler) File(java.io.File) OverridableFile(edu.stanford.bmir.protege.web.server.inject.OverridableFile) OverridableFile(edu.stanford.bmir.protege.web.server.inject.OverridableFile) Before(org.junit.Before)

Example 7 with DefaultMustacheFactory

use of com.github.mustachejava.DefaultMustacheFactory in project qpp-conversion-tool by CMSgov.

the class ErrorCodeDocumentationGenerator method main.

public static void main(String... args) throws IOException {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mdTemplate = mf.compile("error-code/error-code-tempate.md");
    try (FileWriter fw = new FileWriter("./ERROR_MESSAGES.md")) {
        List<ErrorCode> errorCodes = Arrays.asList(ErrorCode.values());
        mdTemplate.execute(fw, errorCodes).flush();
        fw.flush();
    }
}
Also used : DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) FileWriter(java.io.FileWriter) Mustache(com.github.mustachejava.Mustache) ErrorCode(gov.cms.qpp.conversion.model.error.ErrorCode) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 8 with DefaultMustacheFactory

use of com.github.mustachejava.DefaultMustacheFactory in project bndtools by bndtools.

the class MustacheTemplateEngine method getTemplateParameters.

@Override
public Map<String, String> getTemplateParameters(ResourceMap inputs, IProgressMonitor monitor) throws Exception {
    final Map<String, String> params = new HashMap<>();
    final Properties defaults = readDefaults(inputs);
    TemplateSettings settings = readSettings(inputs);
    DefaultMustacheFactory factory = new DefaultMustacheFactory();
    AccumulateNamesObjectHandler namesAccumulator = new AccumulateNamesObjectHandler(factory.getObjectHandler());
    factory.setObjectHandler(namesAccumulator);
    int counter = 0;
    for (Entry<String, Resource> entry : inputs.entries()) {
        String inputPath = entry.getKey();
        factory.compile(new StringReader(inputPath), "mapping", settings.leftDelim, settings.rightDelim).execute(new StringWriter(), Collections.emptyMap());
        Resource source = entry.getValue();
        if (settings.ignore == null || !settings.ignore.matches(inputPath)) {
            if (source.getType() == ResourceType.File && settings.preprocessMatch.matches(inputPath)) {
                InputStreamReader reader = new InputStreamReader(source.getContent(), source.getTextEncoding());
                factory.compile(reader, "temp" + (counter++), settings.leftDelim, settings.rightDelim).execute(new StringWriter(), Collections.emptyMap()).toString();
            }
        }
    }
    for (String param : namesAccumulator.getNames()) {
        params.put(param, defaults.getProperty(param));
    }
    return params;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

Example 9 with DefaultMustacheFactory

use of com.github.mustachejava.DefaultMustacheFactory in project bndtools by bndtools.

the class MustacheTemplateEngine method generateOutputs.

@Override
public ResourceMap generateOutputs(ResourceMap inputs, Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    TemplateSettings settings = readSettings(inputs);
    Properties defaults = readDefaults(inputs);
    ResourceMap outputs = new ResourceMap();
    final Map<String, Object> flattenedParams = flattenParameters(parameters);
    applyDefaults(defaults, flattenedParams);
    DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
    mustacheFactory.setObjectHandler(new CheckMissingObjectHandler(mustacheFactory.getObjectHandler()));
    for (Entry<String, Resource> entry : inputs.entries()) {
        String inputPath = entry.getKey();
        Resource source = entry.getValue();
        StringWriter writer = new StringWriter();
        mustacheFactory.compile(new StringReader(inputPath), "mapping", settings.leftDelim, settings.rightDelim).execute(writer, flattenedParams);
        String outputPath = writer.toString();
        if (settings.ignore == null || !settings.ignore.matches(inputPath)) {
            Resource output;
            switch(source.getType()) {
                case Folder:
                    output = source;
                    break;
                case File:
                    if (settings.preprocessMatch.matches(inputPath)) {
                        // This file should be processed with the template engine
                        InputStreamReader reader = new InputStreamReader(source.getContent(), source.getTextEncoding());
                        StringWriter rendered = new StringWriter();
                        mustacheFactory.compile(reader, outputPath, settings.leftDelim, settings.rightDelim).execute(rendered, flattenedParams);
                        output = new StringResource(rendered.toString());
                    } else {
                        // This file should be directly copied
                        output = source;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unknown resource type " + source.getType());
            }
            outputs.put(outputPath, output);
        }
    }
    return outputs;
}
Also used : InputStreamReader(java.io.InputStreamReader) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties) ResourceMap(org.bndtools.templating.ResourceMap) StringResource(org.bndtools.templating.StringResource) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

Example 10 with DefaultMustacheFactory

use of com.github.mustachejava.DefaultMustacheFactory in project wcomponents by BorderTech.

the class TemplateRenderInterceptor method paint.

/**
 * {@inheritDoc}
 */
@Override
public void paint(final RenderContext renderContext) {
    UIContext uic = UIContextHolder.getCurrent();
    // Generate the HTML
    StringWriter outputBuffer = new StringWriter();
    PrintWriter outputWriter = new PrintWriter(outputBuffer);
    WebXmlRenderContext outputContext = new WebXmlRenderContext(outputWriter, uic.getLocale());
    super.paint(outputContext);
    String html = outputBuffer.toString();
    // Only process TEMPLATE if has I18N brackets
    if (html.contains("{{#i18n")) {
        // Create a new instance of factory to avoid caching the page.
        // https://github.com/spullara/mustache.java/issues/117
        final MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader(html), UUID.randomUUID().toString());
        StringWriter templateWriter = new StringWriter();
        mustache.execute(templateWriter, new I18NContext(getResourceBundle()));
        html = templateWriter.toString();
    }
    // Get the OUTPUT writer
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    PrintWriter writer = webRenderContext.getWriter();
    writer.print(html);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringReader(java.io.StringReader) Mustache(com.github.mustachejava.Mustache) PrintWriter(java.io.PrintWriter) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Aggregations

DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)12 Mustache (com.github.mustachejava.Mustache)7 MustacheFactory (com.github.mustachejava.MustacheFactory)6 StringWriter (java.io.StringWriter)6 StringReader (java.io.StringReader)4 HashMap (java.util.HashMap)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Resource (org.bndtools.templating.Resource)2 StringResource (org.bndtools.templating.StringResource)2 UIContext (com.github.bordertech.wcomponents.UIContext)1 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)1 Binding (com.github.mustachejava.Binding)1 Code (com.github.mustachejava.Code)1 TemplateContext (com.github.mustachejava.TemplateContext)1