RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(bufferForYourTemplate); SimpleNode node = runtimeServices.parse(reader, "Template name")); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument();
Template getTemplate(String viewName) { return rtInstance.getTemplate(viewName + suffix); }
@Override public SimpleNode parse(Reader reader, String templateName, boolean dumpNamespace) throws ParseException { SimpleNode node = super.parse(reader, templateName, dumpNamespace); if (interpolationHack) { node = traversNode(node); } return node; }
/** 初始化resource loader. */ @Override public void init(ExtendedProperties configuration) { rsvc.getLog().info(getLogID() + " : initialization starting."); springLoader = assertNotNull((ResourceLoader) rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_KEY), SPRING_RESOURCE_LOADER_KEY); path = normalizeAbsolutePath(configuration.getString("path"), true); assertTrue(!isEmpty(path), "path"); path += "/"; rsvc.getLog().info(getLogID() + " : set path '" + path + "'"); rsvc.getLog().info(getLogID() + " : initialization complete."); }
@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); } }
/** 初始化resource loader. */ @Override public void init(ExtendedProperties configuration) { rsvc.getLog().info(getLogID() + " : initialization starting."); preloadedResources = createTreeMap(); @SuppressWarnings("unchecked") Map<String, Resource> resources = assertNotNull( (Map<String, Resource>) configuration.getProperty(PRELOADED_RESOURCES_KEY), PRELOADED_RESOURCES_KEY); for (Map.Entry<String, Resource> entry : resources.entrySet()) { String templateName = normalizeTemplateName(entry.getKey()); Resource resource = entry.getValue(); preloadedResources.put(templateName, resource); } rsvc.getLog().info(getLogID() + " : preloaded resources: " + new MapBuilder().appendAll(preloadedResources)); rsvc.getLog().info(getLogID() + " : initialization complete."); }
@Inject public VelocityViewFactory(Argo argo) { String viewFolder = viewFolderPath(argo); Properties ps = new Properties(); ps.setProperty("resource.loader", "file"); ps.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); ps.setProperty("file.resource.loader.path", viewFolder); ps.setProperty("file.resource.loader.cache", "false"); ps.setProperty("file.resource.loader.modificationCheckInterval", "2"); ps.setProperty("input.encoding", "UTF-8"); ps.setProperty("output.encoding", "UTF-8"); ps.setProperty("default.contentType", "text/html; charset=UTF-8"); ps.setProperty("velocimarco.library.autoreload", "true"); ps.setProperty("runtime.log.error.stacktrace", "false"); ps.setProperty("runtime.log.warn.stacktrace", "false"); ps.setProperty("runtime.log.info.stacktrace", "false"); ps.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); ps.setProperty("runtime.log.logsystem.log4j.category", "velocity_log"); rtInstance = new RuntimeInstance(); try { rtInstance.init(ps); } catch (Exception e) { throw ArgoException.raise(e); } }
@Override public synchronized void init() { super.init(); interpolationHack = getConfiguration().getBoolean(INTERPOLATION_HACK_KEY, INTERPOLATION_HACK_DEFAULT); }
private void execute0() throws IOException, MojoExecutionException { velocity = new RuntimeInstance(); final ExtProperties p = new ExtProperties(); velocity.setConfiguration(p);
public void setRuntimeServices(RuntimeServices rs) { rs.getConfiguration().setProperty(RUNTIME_SERVICES_KEY, rs); } }
/** * Apply velocity to the input. */ private String filterVelocity(TemplateFile f, String template, TemplateOptions options) { final VelocityContext ctx = new VelocityContext(); ctx.put("TemplateOptions", options); ctx.put("true", true); ctx.put("templateOnly", false); ctx.put("false", false); StringWriter sw = new StringWriter(); velocity.evaluate(ctx, sw, f.getFileName(), template); return sw.toString(); }
/** * Set a Velocity Runtime property. * * @param key * @param value */ public void setProperty(String key, Object value) { ri.setProperty(key, value); }
/** * Get a Velocity Runtime property. * * @param key property to retrieve * @return property value or null if the property * not currently set */ public Object getProperty( String key ) { return ri.getProperty( key ); }
/** * Clear a Velocity Runtime property. * * @param key of property to clear */ public static void clearProperty(String key) { RuntimeSingleton.clearProperty(key); }
/** * Remove a directive. * * @param name name of the directive. */ public void removeDirective(String name) { RuntimeSingleton.removeDirective(name); }
/** 初始化resource loader. */ @Override public void init(ExtendedProperties configuration) { rsvc.getLog().info(getLogID() + " : initialization starting."); springLoader = assertNotNull((ResourceLoader) rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_KEY), SPRING_RESOURCE_LOADER_KEY); path = normalizeAbsolutePath(configuration.getString("path"), true); assertTrue(!isEmpty(path), "path"); path += "/"; rsvc.getLog().info(getLogID() + " : set path '" + path + "'"); rsvc.getLog().info(getLogID() + " : initialization complete."); }
/** 初始化resource loader. */ @Override public void init(ExtendedProperties configuration) { rsvc.getLog().info(getLogID() + " : initialization starting."); preloadedResources = createTreeMap(); @SuppressWarnings("unchecked") Map<String, Resource> resources = assertNotNull( (Map<String, Resource>) configuration.getProperty(PRELOADED_RESOURCES_KEY), PRELOADED_RESOURCES_KEY); for (Map.Entry<String, Resource> entry : resources.entrySet()) { String templateName = normalizeTemplateName(entry.getKey()); Resource resource = entry.getValue(); preloadedResources.put(templateName, resource); } rsvc.getLog().info(getLogID() + " : preloaded resources: " + new MapBuilder().appendAll(preloadedResources)); rsvc.getLog().info(getLogID() + " : initialization complete."); }
@Override public SimpleNode parse(Reader reader, String templateName, boolean dumpNamespace) throws ParseException { SimpleNode node = super.parse(reader, templateName, dumpNamespace); if (interpolationHack) { node = traversNode(node); } return node; }
@Override public synchronized void init() { super.init(); interpolationHack = getConfiguration().getBoolean(INTERPOLATION_HACK_KEY, INTERPOLATION_HACK_DEFAULT); }
public void setRuntimeServices(RuntimeServices rs) { rs.getConfiguration().setProperty(RUNTIME_SERVICES_KEY, rs); } }