/** * Render it. * @param folder Template folder * @param template Page template * @param params Params for velocity * @return Page body * @throws IOException If fails */ private static InputStream render(final String folder, final InputStream template, final Map<CharSequence, Object> params) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Writer writer = new Utf8OutputStreamWriter(baos); final VelocityEngine engine = new VelocityEngine(); engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute() ); engine.setProperty( "file.resource.loader.path", folder ); engine.evaluate( new VelocityContext(params), writer, "", new Utf8InputStreamReader(template) ); writer.close(); return new ByteArrayInputStream(baos.toByteArray()); }
private void copyTemplateTo(final File targetDir, final String filename) throws Exception { final File file = new File(targetDir, filename); if (file.exists()) { return; } // don't break apps using Velocity facade final VelocityEngine engine = new VelocityEngine(); engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute()); engine.setProperty(Velocity.RESOURCE_LOADER, "class"); engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader"); engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); engine.init(); final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename); final VelocityContext context = new VelocityContext(); context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort())); context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort())); final Writer writer = new FileWriter(file); template.merge(context, writer); writer.flush(); writer.close(); }
/** * Constructs a new {@link TemplateEngine} object, and initializes it. This * constructor will throw a {@link RuntimeException} if initializing fails. * * @param path The path containing all the templates. * @param executor */ public TemplateEngine(Path path, ExecutorService executor) { this.executor = executor; try { this.path = path; this.modificationTimes = Maps.newHashMap(); this.engine = new VelocityEngine(); engine.setProperty("resource.loader", "string"); engine.setProperty("runtime.log.logsystem.class", getPath(NullLogChute.class)); engine.setProperty("string.resource.loader.class", getPath(StringResourceLoader.class)); engine.setProperty("string.resource.loader.description", "Velocity StringResource loader"); engine.setProperty("string.resource.loader.repository.class", getPath(StringResourceRepositoryImpl.class)); engine.init(); updateTemplates(); } catch (Exception e) { LOG.error(e.getLocalizedMessage(), e); throw new DevHubException("Could not initialize the TemplateEngine", e); } }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext=applicationContext; ve = new VelocityEngine(); ve.setProperty(Velocity.RESOURCE_LOADER, "class"); ve.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,new NullLogChute()); ve.init(); }
public VelocityImpl(){ Properties props = new Properties(); props.setProperty(Velocity.INPUT_ENCODING, "UTF-8");// 设置输入字符集 props.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");// 设置输出字符集 props.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"); props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName()); props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "templates/"); engine.init(props); }
@Autowired public ResponseFactory(final DateTimeProvider dateTimeProvider) { this.dateTimeProvider = dateTimeProvider; velocityEngine = new VelocityEngine(); velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new NullLogChute()); velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); velocityEngine.init(); }
public TemplateProcessImpl() { ve = new VelocityEngine(); // 关闭错误日志输出 ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute()); }