@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { // create the velocity context VelocityContext context = createVelocityContext(model); // merge the template try { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(templateContent); SimpleNode node = runtimeServices.parse(reader, "StringTemplate"); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); template.merge(context, writer); } catch (Exception e) { throw new PippoRuntimeException(e); } }
public String process(String templateText, Object context) { logger.log("DMAI0001", new Object[] { new Throwable().getStackTrace()[0].getClassName() + "@" + new Throwable().getStackTrace()[0].getMethodName() }); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); try { StringReader reader = new StringReader(templateText); SimpleNode node = runtimeServices.parse(reader, reader.toString()); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); Writer writer = new StringWriter(); template.merge((Context) context, writer); logger.log("DMAI0002", new Object[] { new Throwable().getStackTrace()[0].getClassName() + "@" + new Throwable().getStackTrace()[0].getMethodName() }); return writer.toString(); } catch (ResourceNotFoundException e) { throw new RuntimeException(e); } catch (ParseErrorException e) { throw new RuntimeException(e); } catch (MethodInvocationException e) { throw new RuntimeException(e); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
protected Template constructTemplate(String name, StringBuffer buffer) throws ParseException { Template template = new Template(); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(buffer.toString()); SimpleNode simpleNode = runtimeServices.parse(reader, name); template.setRuntimeServices(runtimeServices); template.setData(simpleNode); template.initDocument(); return template; }
StringReader reader = new StringReader(contents); vmtemplate.setRuntimeServices(runtimeServices); vmtemplate.setData(runtimeServices.parse(reader, template)); vmtemplate.initDocument(); vmtemplate.merge(context, tempresult);
public String generatePayloadFromTemplate(String template,SequenceGeneratorInput input,String vmId) throws ParseException { String payload = null; RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(template); SimpleNode node = runtimeServices.parse(reader,template); Template templateData = new Template(); templateData.setRuntimeServices(runtimeServices); templateData.setData(node); templateData.initDocument(); VelocityContext vc = new VelocityContext(); String identityUrl = input.getInventoryModel().getVnf().getIdentityUrl(); logger.debug("IdentityUrl:" + identityUrl); logger.debug("Vm-Id:" + vmId); vc.put("identity-url", identityUrl); vc.put("vm-id", vmId); StringWriter stringWriter = new StringWriter(); templateData.merge(vc, stringWriter); payload= stringWriter.toString(); logger.debug("Payload Data:" +stringWriter.toString()); return payload; } }
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { // create the velocity context VelocityContext context = createVelocityContext(model); // merge the template try { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(templateContent); SimpleNode node = runtimeServices.parse(reader, "StringTemplate"); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); template.merge(context, writer); } catch (Exception e) { throw new PippoRuntimeException(e); } }
/** * Gets a velocity template from the given String. * * @param templateAsString * the template as string * @return the template * @throws ParseException * the parse exception */ public static Template getTemplate(final String templateAsString) throws ParseException { final RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); final StringReader reader = new StringReader(templateAsString); final SimpleNode node = runtimeServices.parse(reader, "Template name"); final Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); template.setEncoding("UTF-8"); return template; }
/** * Builds {@link Template}s for each {@link Action} of {@link AlertAction} * type for a given {@link Rule} and inserts it into the internal * {@link Map} used for lookups. * * @param rule * @throws ParseException */ public void buildTemplateMapForActions(Rule rule) throws ParseException { if (rule.getActions() != null) { for (Action action : rule.getActions()) { if (action instanceof AlertAction) { AlertAction alertAction = (AlertAction) action; StringReader reader = new StringReader(alertAction.getBody()); SimpleNode node = runtimeServices.parse(reader, rule.getName()); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); templateMap.put(Utils.combineRuleActionId(rule.getRuleId(), action.getActionId()), template); } } } else { logger.log(Level.SEVERE, "Bad rule with no actions:" + rule.toString()); } }
/** * Builds {@link Template}s for each {@link Action} of {@link AlertAction} * type for a given {@link Rule} and inserts it into the internal * {@link Map} used for lookups. * * @param rule * @throws ParseException */ public void buildTemplateMapForActions(Rule rule) throws ParseException { if (rule.getActions() != null) { for (Action action : rule.getActions()) { if (action instanceof AlertAction) { AlertAction alertAction = (AlertAction) action; StringReader reader = new StringReader(alertAction.getBody()); SimpleNode node = runtimeServices.parse(reader, rule.getName()); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); templateMap.put(Utils.combineRuleActionId(rule.getRuleId(), action.getActionId()), template); } } } else { logger.log(Level.SEVERE, "Bad rule with no actions:" + rule.toString()); } }
/** * Builds {@link AlertTemplate} {@link Map} used for lookups. * * @param template * @throws ParseException */ public static void buildTemplateMap(RuntimeServices runtimeServices, Map<Short, VelocityAlertTemplate> templateMap, AlertTemplate template) throws ParseException { StringReader reader = new StringReader(template.getBody()); SimpleNode node = runtimeServices.parse(reader, String.valueOf(template.getTemplateId()) + "_body"); Template velocityTemplate = new Template(); velocityTemplate.setRuntimeServices(runtimeServices); velocityTemplate.setData(node); velocityTemplate.initDocument(); VelocityAlertTemplate alertTemplate = new VelocityAlertTemplate(template); alertTemplate.setVelocityBodyTemplate(velocityTemplate); if (template.getSubject() != null) { reader = new StringReader(template.getSubject()); node = runtimeServices.parse(reader, String.valueOf(template.getTemplateId()) + "_subject"); velocityTemplate = new Template(); velocityTemplate.setRuntimeServices(runtimeServices); velocityTemplate.setData(node); velocityTemplate.initDocument(); alertTemplate.setVelocitySubjectTemplate(velocityTemplate); } templateMap.put(template.getTemplateId(), alertTemplate); }
/** * Builds {@link AlertTemplate} {@link Map} used for lookups. * * @param template * @throws ParseException */ public static void buildTemplateMap(RuntimeServices runtimeServices, Map<Short, VelocityAlertTemplate> templateMap, AlertTemplate template) throws ParseException { StringReader reader = new StringReader(template.getBody()); SimpleNode node = runtimeServices.parse(reader, String.valueOf(template.getTemplateId()) + "_body"); Template velocityTemplate = new Template(); velocityTemplate.setRuntimeServices(runtimeServices); velocityTemplate.setData(node); velocityTemplate.initDocument(); VelocityAlertTemplate alertTemplate = new VelocityAlertTemplate(template); alertTemplate.setVelocityBodyTemplate(velocityTemplate); if (template.getSubject() != null) { reader = new StringReader(template.getSubject()); node = runtimeServices.parse(reader, String.valueOf(template.getTemplateId()) + "_subject"); velocityTemplate = new Template(); velocityTemplate.setRuntimeServices(runtimeServices); velocityTemplate.setData(node); velocityTemplate.initDocument(); alertTemplate.setVelocitySubjectTemplate(velocityTemplate); } templateMap.put(template.getTemplateId(), alertTemplate); }
@Override public void validate(Action value) throws ValidationException { if (value instanceof AlertAction) { String body = ((AlertAction) value).getBody(); try { StringReader reader = new StringReader(body); SimpleNode node = RuntimeSingleton.getRuntimeServices().parse(reader, "testAction"); Template template = new Template(); template.setRuntimeServices(RuntimeSingleton.getRuntimeServices()); template.setData(node); template.initDocument(); } catch (ParseException e) { throw new ValidationException("Invalid velocity template:"+e.getMessage()); } } }