Search in sources :

Example 1 with Context

use of org.apache.velocity.context.Context in project entando-core by entando.

the class DefaultVelocityRenderer method render.

@Override
public String render(Object object, String velocityTemplate) {
    String renderedObject = null;
    try {
        Context velocityContext = new VelocityContext();
        velocityContext.put(this.getWrapperContextName(), object);
        StringWriter stringWriter = new StringWriter();
        boolean isEvaluated = Velocity.evaluate(velocityContext, stringWriter, "render", velocityTemplate);
        if (!isEvaluated) {
            throw new ApsSystemException("Rendering error");
        }
        stringWriter.flush();
        renderedObject = stringWriter.toString();
    } catch (Throwable t) {
        _logger.error("Rendering error, class: {} - template: {}", object.getClass().getSimpleName(), velocityTemplate, t);
        // ApsSystemUtils.logThrowable(t, this, "render", "Rendering error");
        renderedObject = "";
    }
    return renderedObject;
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException)

Example 2 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class VelocityTemplateTest method testTemplates.

private void testTemplates(String dir, File file, List<Exception> exs) {
    String name = file.getName();
    if ("_content".equals(name)) {
        File[] templates = file.listFiles();
        for (File template : templates) {
            String templateName = template.getName();
            if (templateName.endsWith(".html")) {
                try {
                    String path = dir + templateName;
                    StringWriter writer = new StringWriter();
                    Context context = new VelocityContext();
                    Template veloTemplate = engine.getTemplate(path);
                    veloTemplate.merge(context, writer);
                    count++;
                } catch (Exception e) {
                    exs.add(e);
                }
            }
        }
    } else if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File child : files) {
            String subDir = dir + child.getName() + "/";
            testTemplates(subDir, child, exs);
        }
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) Template(org.apache.velocity.Template)

Example 3 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class EmailProperty method addFormItem.

/**
 * @see org.olat.user.propertyhandlers.UserPropertyHandler#addFormItem(java.util.Locale, org.olat.core.id.User, java.lang.String, boolean, org.olat.core.gui.components.form.flexible.FormItemContainer)
 */
@Override
public FormItem addFormItem(Locale locale, final User user, String usageIdentifyer, final boolean isAdministrativeUser, FormItemContainer formItemContainer) {
    org.olat.core.gui.components.form.flexible.elements.TextElement tElem = null;
    tElem = (org.olat.core.gui.components.form.flexible.elements.TextElement) super.addFormItem(locale, user, usageIdentifyer, isAdministrativeUser, formItemContainer);
    // to validate the input a special isValidValue is used.
    if (usageIdentifyer.equals(UserBulkChangeStep00.class.getCanonicalName())) {
        tElem.setItemValidatorProvider(new ItemValidatorProvider() {

            @Override
            public boolean isValidValue(String value, ValidationError validationError, Locale locale2) {
                UserBulkChangeManager ubcMan = CoreSpringFactory.getImpl(UserBulkChangeManager.class);
                Context vcContext = new VelocityContext();
                if (user == null) {
                    vcContext = ubcMan.getDemoContext(locale2);
                } else // should be used if user-argument !=null --> move to right place
                {
                    Long userKey = user.getKey();
                    Identity identity = BaseSecurityManager.getInstance().loadIdentityByKey(userKey);
                    ubcMan.setUserContext(identity, vcContext);
                }
                value = value.replace("$", "$!");
                String evaluatedValue = ubcMan.evaluateValueWithUserContext(value, vcContext);
                return EmailProperty.this.isValidValue(user, evaluatedValue, validationError, locale2);
            }
        });
    }
    return tElem;
}
Also used : Locale(java.util.Locale) VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) ItemValidatorProvider(org.olat.core.gui.components.form.flexible.impl.elements.ItemValidatorProvider) VelocityContext(org.apache.velocity.VelocityContext) UserBulkChangeStep00(org.olat.admin.user.bulkChange.UserBulkChangeStep00) UserBulkChangeManager(org.olat.admin.user.bulkChange.UserBulkChangeManager) ValidationError(org.olat.core.gui.components.form.ValidationError) Identity(org.olat.core.id.Identity)

Example 4 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class AssessmentObjectComponentRenderer method renderPositionObjectStage.

private void renderPositionObjectStage(AssessmentRenderer renderer, StringOutput sb, PositionObjectStage positionObjectStage, ResolvedAssessmentItem resolvedAssessmentItem, ItemSessionState itemSessionState, AssessmentObjectComponent component, URLBuilder ubu, Translator translator) {
    Context ctx = new VelocityContext();
    ctx.put("positionObjectStage", positionObjectStage);
    String page = getInteractionTemplate(positionObjectStage);
    renderVelocity(renderer, sb, positionObjectStage, ctx, page, resolvedAssessmentItem, itemSessionState, component, ubu, translator);
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) VelocityContext(org.apache.velocity.VelocityContext) AssessmentRenderFunctions.contentAsString(org.olat.ims.qti21.ui.components.AssessmentRenderFunctions.contentAsString)

Example 5 with Context

use of org.apache.velocity.context.Context in project openolat by klemens.

the class UserBulkChangeManager method getDemoContext.

public Context getDemoContext(Translator propertyTrans) {
    Context vcContext = new VelocityContext();
    List<UserPropertyHandler> userPropertyHandlers2 = userManager.getAllUserPropertyHandlers();
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers2) {
        String propertyName = userPropertyHandler.getName();
        String userValue = propertyTrans.translate("import.example." + userPropertyHandler.getName());
        vcContext.put(propertyName, userValue);
    }
    return vcContext;
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) VelocityContext(org.apache.velocity.VelocityContext) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Aggregations

Context (org.apache.velocity.context.Context)41 VelocityContext (org.apache.velocity.VelocityContext)33 StringWriter (java.io.StringWriter)9 IOException (java.io.IOException)8 Identity (org.olat.core.id.Identity)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 AssessmentRenderFunctions.contentAsString (org.olat.ims.qti21.ui.components.AssessmentRenderFunctions.contentAsString)6 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)4 StringReader (java.io.StringReader)4 Locale (java.util.Locale)4 Map (java.util.Map)4 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)4 ValidationError (org.olat.core.gui.components.form.ValidationError)4 MailContext (org.olat.core.util.mail.MailContext)4 I18nManagerWrapper (com.agiletec.aps.system.services.i18n.I18nManagerWrapper)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 Renderer (org.olat.core.gui.render.Renderer)3