VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); VelocityContext context = new VelocityContext(); context.put("date", getMyTimestampFunction()); Template t = ve.getTemplate( "templates/email_html_new.vm" ); StringWriter writer = new StringWriter(); t.merge( context, writer );
private static String applyTemplate(String templateName, VelocityContext context) { VelocityEngine velocity = new VelocityEngine(); Properties properties = new Properties(); properties.setProperty("resource.loader", "class"); properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocity.init(properties); Writer writer = new StringWriter(); velocity.mergeTemplate(templateName, "UTF-8", context, writer); try { writer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return writer.toString(); }
/** * 新建Velocity模板引擎 * * @param templateDir 模板所在目录,绝对路径 * @param charset 编码 * @return VelocityEngine */ public static VelocityEngine newEngine(String templateDir, String charset) { final VelocityEngine ve = new VelocityEngine(); ve.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true); // 使用缓存 ve.init(_newInitedProp(templateDir, charset)); return ve; }
public StatsServlet(StatsPlugin statsPlugin) throws UnavailableException { this.statsPlugin = statsPlugin; this.velocityEngine = new VelocityEngine(); // These two properties tell Velocity to use its own classpath-based loader velocityEngine.addProperty("resource.loader", "class"); velocityEngine.addProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocityEngine.setProperty("runtime.references.strict", true); String logChuteName = "org.apache.velocity.runtime.log.NullLogChute"; velocityEngine.setProperty("runtime.log.logsystem.class", logChuteName); }
/** * Creates a new Velocity Engine. * * @return a velocity engine */ private VelocityEngine createVelocityEngine() { final VelocityEngine velocity = new VelocityEngine(); // Logging redirection for Velocity - Required by Jenkins and other server applications velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, VelocityLoggerRedirect.class.getName()); return velocity; }
public static String merge( String resource, Properties properties ) { ClasspathResourceLoader loader = new ClasspathResourceLoader(); loader.getResourceStream( resource ); VelocityEngine engine = new VelocityEngine(); Properties config = new Properties(); config.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem" ); config.setProperty( RuntimeConstants.RESOURCE_LOADER, "classpath" ); config.setProperty( "classpath.resource.loader.class", ClasspathResourceLoader.class.getName() ); engine.init( config ); VelocityContext context = new VelocityContext( properties ); Template template = engine.getTemplate( resource ); StringWriter writer = new StringWriter(); template.merge( context, writer ); return writer.toString(); }
private static void initEventsModule() { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); maintenancesManager = new MaintenancesManager(dataManager); notificationManager = new NotificationManager(dataManager); notificatorManager = new NotificatorManager(); Properties velocityProperties = new Properties(); velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/"); velocityProperties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute"); String address; try { address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); } catch (UnknownHostException e) { address = "localhost"; } String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); webUrl = Context.getConfig().getString("web.url", webUrl); velocityProperties.setProperty("web.url", webUrl); velocityEngine = new VelocityEngine(); velocityEngine.init(velocityProperties); motionEventHandler = new MotionEventHandler(tripsConfig); overspeedEventHandler = new OverspeedEventHandler( Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, Context.getConfig().getBoolean("event.overspeed.notRepeat"), Context.getConfig().getBoolean("event.overspeed.preferLowest")); }
Properties p = new Properties(); try { p.load(fhirContext.getVersion().getFhirVersionPropertiesFile()); } catch (IOException e) { throw new MojoFailureException("Failed to load version property file", e); for(Object next : p.keySet()) { keys.add((String) next); ctx.put("versionCapitalized", capitalize); VelocityEngine v = new VelocityEngine(); v.setProperty("resource.loader", "cp"); v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); v.setProperty("runtime.references.strict", Boolean.TRUE); targetResourceDirectory.mkdirs(); File f = new File(targetResourceDirectory, targetResourceSpringBeansFile); OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8"); v.evaluate(ctx, w, "", templateReader); w.close(); templateReader = new InputStreamReader(templateIs); f = new File(configPackageDirectoryBase, "BaseJavaConfig" + capitalize + ".java"); w = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8"); v.evaluate(ctx, w, "", templateReader); w.close();
@Override public VelocityTemplateEngine init(ConfigBuilder configBuilder) { super.init(configBuilder); if (null == velocityEngine) { Properties p = new Properties(); p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE); p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY); p.setProperty(Velocity.ENCODING_DEFAULT, ConstVal.UTF8); p.setProperty(Velocity.INPUT_ENCODING, ConstVal.UTF8); p.setProperty("file.resource.loader.unicode", StringPool.TRUE); velocityEngine = new VelocityEngine(p); } return this; }
private VelocityEngine getEngine() throws IOException { if (velocityEngine == null) { Properties properties = new Properties(); properties.load(getClass().getResourceAsStream(PROPERTIES)); properties.put("velocimacro.library", Joiner.on(",").join(macroFiles)); velocityEngine = new VelocityEngine(properties); } return velocityEngine; } }
@Override public void afterPropertiesSet() { Properties properties = new Properties(); // this is setup so that file takes preference over classpath, so that we can reload stuff in dev environment // and fallback to spring classloader in prod environment properties.setProperty(RESOURCE_LOADER, "file," + SpringResourceLoader.NAME); properties.setProperty(FILE_RESOURCE_LOADER_CACHE, "true"); properties.setProperty(EVENTHANDLER_REFERENCEINSERTION, org.apache.velocity.app.event.implement.EscapeHtmlReference.class.getName()); properties.setProperty(FILE_RESOURCE_LOADER_PATH, resourceBasePaths); properties.setProperty("file.resource.loader.modificationCheckInterval", String.valueOf(modificationCheckInterval)); properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName()); properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true"); if (modificationCheckInterval != 0) { properties.setProperty(VM_LIBRARY_AUTORELOAD, "true"); } properties.setProperty(VM_LIBRARY, "macros/urls.vm"); this.velocityEngine = new VelocityEngine(properties); this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER, resourceLoader); this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceBasePaths); }
private String generateSourceFromTemplate(String templateFullName, VelocityContext context) { StringWriter writer = new StringWriter(); velocityEngine.getTemplate(templateFullName).merge(context, writer); return writer.toString(); }
public static VelocityEngine configureVelocityEngine (File templateFile, String velocityPath, String propertyFile) throws IOException { VelocityEngine result = new VelocityEngine(); boolean haveResourceLoader = false; boolean haveRuntimeReferences = false; if (propFile.exists() && propFile.isFile() && propFile.canRead()) { InputStream propsIn = new FileInputStream(propFile); Properties props = new Properties(); props.load(propsIn); propsIn.close(); for (Entry<?,?> entry : props.entrySet()) { String key = (String)entry.getKey(); result.setProperty(key, entry.getValue()); if (RuntimeConstants.RESOURCE_LOADER.equals(key)) { haveResourceLoader = true; result.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); result.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); if (velocityPath != null) { result.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, velocityPath); } else { String path = templateFile.getCanonicalFile().getParent(); result.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); result.setProperty("resource.loader", "cp"); result.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); result.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
@Override public String render(ModelAndView modelAndView) { Template template = velocityEngine.getTemplate(provider.layout(), "utf-8"); Object model = modelAndView.getModel(); if (model == null) { model = Collections.emptyMap(); } if (model instanceof Map) { VelocityContext context = initialContextProvider.getVelocityContext((Map) model, controller, modelAndView.getViewName()); StringWriter writer = new StringWriter(); template.merge(context, writer); Object meta = context.get("meta"); context.put("meta", GSON.toJson(meta)); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } }
private static VelocityEngine getVelocityEngine() throws Exception { if (velocityEngine == null) { velocityEngine = new VelocityEngine(); Properties vProps = new Properties(); if (FileHelper.fileExistsOnClasspath(VELOCITY_PROP_FILE)) { vProps.load(FileHelper.fileInputStreamFromClasspath(VELOCITY_PROP_FILE)); } velocityEngine.init(vProps); } return velocityEngine; }
final VelocityEngine ve = new VelocityEngine(); final Properties p = new Properties(); p.setProperty(RuntimeConstants.RESOURCE_LOADER, fileResourceLoader + "," + classpathResourceLoader); setVelocityResourceLoaderProperty(p, p.toString())); ve.init(p); final Context context = new VelocityContext(); context.put("catalog", catalog); context.put("identifiers", identifiers); templateLocation, templateEncoding)); final Template template = ve.getTemplate(templateLocation, templateEncoding); template.merge(context, writer);
private static String applyToVelocityTemplate(String appMapURL, List<JenkinsAMChart> strategyCharts) { VelocityEngine ve = new VelocityEngine(); StringWriter writer = new StringWriter(); ve.setProperty("resource.loader", "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); Template t = ve.getTemplate("amHistogramChartsReport.vm"); VelocityContext context = new VelocityContext(); context.put("strategyCharts", strategyCharts); context.put("url", appMapURL); t.merge(context, writer); return writer.toString(); }
VelocityEngine engine = new VelocityEngine(); Properties props = new Properties(); props.put("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); props.put("runtime.log.logsystem.log4j.category", "velocity"); props.put("runtime.log.logsystem.log4j.logger", "velocity"); engine.init(props);
@Override public String render(TemplateRenderRequest request) { VelocityEngine velocity = new VelocityEngine(); velocity.init(); VelocityContext context = new VelocityContext(request.getContext()); StringWriter writer = new StringWriter(); velocity.evaluate(context, writer, "render-template", request.getTemplate()); return writer.toString(); }
public String process(Map<String, Object> context, TemplateIdentifier templateIdentifier) { StringWriter stringWriter = new StringWriter(); VelocityContext velocityContext = new VelocityContext(context); try { velocityEngine.mergeTemplate(templateIdentifier.getFileName(), "UTF-8", velocityContext, stringWriter); return stringWriter.toString(); } catch (ResourceNotFoundException e) { LOGGER.error("", e); } catch (ParseErrorException e) { LOGGER.error("", e); } catch (MethodInvocationException e) { LOGGER.error("", e); } catch (Exception e) { LOGGER.error("", e); } return ""; }