@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 static Resource getResource(String resourceName, int resourceType) { Resource resource = null; switch (resourceType) { case ResourceManager.RESOURCE_TEMPLATE: resource = new Template(); break; case ResourceManager.RESOURCE_CONTENT: resource = new ContentResource(); break; } return resource; } }
/** * @param resourceName * @param resourceType * @return The resource described by name and type. */ public static Resource getResource(String resourceName, int resourceType) { Resource resource = null; switch (resourceType) { case ResourceManager.RESOURCE_TEMPLATE: resource = new Template(); break; case ResourceManager.RESOURCE_CONTENT: resource = new ContentResource(); break; } return resource; } }
/** * @param resourceName * @param resourceType * @return The resource described by name and type. */ public static Resource getResource(String resourceName, int resourceType) { Resource resource = null; switch (resourceType) { case ResourceManager.RESOURCE_TEMPLATE: resource = new Template(); break; case ResourceManager.RESOURCE_CONTENT: resource = new ContentResource(); break; } return resource; } }
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); } }
/** * @param resourceName * @param resourceType * @return The resource described by name and type. */ public static Resource getResource(String resourceName, int resourceType) { Resource resource = null; switch (resourceType) { case ResourceManager.RESOURCE_TEMPLATE: resource = new Template(); break; case ResourceManager.RESOURCE_CONTENT: resource = new ContentResource(); break; default: throw new VelocityException("invalide resource type"); } return resource; } }
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; }
/** * Returns a Velocity Resource which is a Template for the given SQL. */ public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception { synchronized (templateCache) { Template resource = (Template) templateCache.get(resourceName); if (resource == null) { resource = new Template(); resource.setRuntimeServices(rsvc); resource.setResourceLoader(this); resource.setName(resourceName); resource.setEncoding(encoding); resource.process(); templateCache.put(resourceName, resource); } return resource; } }
/** * Returns a Velocity Resource which is a Template for the given SQL. */ public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception { synchronized (templateCache) { Template resource = templateCache.get(resourceName); if (resource == null) { resource = new Template(); resource.setRuntimeServices(rsvc); resource.setResourceLoader(this); resource.setName(resourceName); resource.setEncoding(encoding); resource.process(); templateCache.put(resourceName, resource); } return resource; } }
/** * Returns a Velocity Resource which is a Template for the given SQL. */ public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception { synchronized (templateCache) { Template resource = (Template) templateCache.get(resourceName); if (resource == null) { resource = new Template(); resource.setRuntimeServices(rsvc); resource.setResourceLoader(this); resource.setName(resourceName); resource.setEncoding(encoding); resource.process(); templateCache.put(resourceName, resource); } return resource; } }
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()); } } }
/** * Constructor based on a Velocity 'encoded' representation. * * @param templateRepresentation * The representation to 'decode'. * @param mediaType * The representation's media type. * @throws IOException * @throws ParseErrorException * @throws ResourceNotFoundException */ public TemplateRepresentation(Representation templateRepresentation, MediaType mediaType) throws ResourceNotFoundException, ParseErrorException, IOException { super(mediaType); this.engine = null; this.template = new Template(); CharacterSet charSet = (templateRepresentation.getCharacterSet() != null) ? templateRepresentation .getCharacterSet() : CharacterSet.DEFAULT; this.template.setEncoding(charSet.getName()); this.template.setLastModified((templateRepresentation .getModificationDate() == null) ? new Date().getTime() : templateRepresentation.getModificationDate().getTime()); this.template.setName("org.restlet.resource.representation"); this.template.setRuntimeServices(RuntimeSingleton.getRuntimeServices()); this.template.setResourceLoader(new RepresentationResourceLoader( templateRepresentation)); this.template.process(); this.templateName = null; }
/** * Compile a template * @param script template source * @return compiled template * @throws ScriptException */ public CompiledScript compile(Reader script) throws ScriptException { initVelocityEngine(null); ResourceLoader resourceLoader = new SingleResourceReader(script); Template template = new Template(); template.setRuntimeServices(velocityEngine); template.setResourceLoader(resourceLoader); try { template.process(); } catch(Exception e) { // CB TODO - exception may have line/col informations, that ScriptException can exploit throw new ScriptException(e); } return new VelocityCompiledScript(this, template); }