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;
}
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);
}
}
}
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;
}
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);
}
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;
}
Aggregations