本文整理匯總了Java中org.apache.velocity.app.VelocityEngine類的典型用法代碼示例。如果您正苦於以下問題:Java VelocityEngine類的具體用法?Java VelocityEngine怎麽用?Java VelocityEngine使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
VelocityEngine類屬於org.apache.velocity.app包,在下文中一共展示了VelocityEngine類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendEmail
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
private void sendEmail(final String fromEmail, final IUser to, final String inSubject, final String inTemplate, final Map<String, Object> values) {
final Properties props = new Properties();
props.setProperty("resource.loader", "class");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
final VelocityEngine engine = new VelocityEngine(props);
final VelocityContext context = new VelocityContext();
engine.init();
for(final String key : values.keySet()) {
LOGGER.debug(() -> String.format("\t -- %s=%s", key, values.get(key)));
context.put(key, values.get(key));
}
final StringWriter writer = new StringWriter();
final Template template = engine.getTemplate("templates/" + inTemplate);
template.merge(context, writer);
final String inBody = writer.toString();
sendEmail(fromEmail, to.getEmail(), inSubject, inBody);
}
示例2: generate
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
/**
* 根據模板生成文件
* @param inputVmFilePath 模板路徑
* @param outputFilePath 輸出文件路徑
* @param context
* @throws Exception
*/
public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
try {
Properties properties = new Properties();
properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath));
Velocity.init(properties);
//VelocityEngine engine = new VelocityEngine();
Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8");
File outputFile = new File(outputFilePath);
FileWriterWithEncoding writer = new FileWriterWithEncoding(outputFile, "utf-8");
template.merge(context, writer);
writer.close();
} catch (Exception ex) {
throw ex;
}
}
示例3: testApp
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
@Test
@Ignore
public void testApp() throws IOException {
Properties props = new Properties();
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties"));
VelocityEngine ve = new VelocityEngine(props);
ve.init();
Template t = ve.getTemplate("/templates/registry.vm");
VelocityContext context = new VelocityContext();
context.put("username", "ricky");
context.put("url", "http://www.thymeleaf.org");
context.put("email", "[email protected]");
StringWriter writer = new StringWriter(1024);
t.merge(context, writer);
String output = writer.toString();
System.out.println(output);
}
示例4: buildStringWriter
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
private static StringWriter buildStringWriter(List<Node> nodes, List<Edge> edges, Map<EVMEnvironment, SymExecutor> executions) throws ReportException {
VelocityEngine velocityEngine = new VelocityEngine();
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init(p);
Template template = velocityEngine.getTemplate(TEMPLATE_FILE);
VelocityContext velocityContext = new VelocityContext();
ObjectMapper objectMapper = new ObjectMapper();
try {
velocityContext.put("nodes", objectMapper.writeValueAsString(nodes));
velocityContext.put("edges", objectMapper.writeValueAsString(edges));
velocityContext.put("executions", executions);
} catch (IOException e) {
logger.error("Error building the report: " + e);
throw new ReportException("Failed creating ReportItem");
}
StringWriter stringWriter = new StringWriter();
template.merge(velocityContext, stringWriter);
return stringWriter;
}
示例5: setUp
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
@Before
public void setUp() throws TikaException, IOException, SAXException {
VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
Templater templater = new Templater();
templater.setEngine(engine);
exporter = new HtmlExporter();
exporter.setTemplater(templater);
TikaProvider provider = new TikaProvider();
Tika tika = provider.tika();
transformer = new TikaTransformer();
transformer.setTika(tika);
}
示例6: setUp
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
@Before
public void setUp() throws TikaException, IOException, SAXException {
VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
Templater templater = new Templater();
templater.setEngine(engine);
exporter = new PdfExporter();
exporter.setTemplater(templater);
TikaProvider provider = new TikaProvider();
Tika tika = provider.tika();
transformer = new TikaTransformer();
transformer.setTika(tika);
}
示例7: getInternalEngine
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
protected VelocityEngine getInternalEngine() throws IOException{
VelocityEngine engine = new VelocityEngine();
Properties ps = new Properties();
ps.setProperty(";runtime.log", Docx4jProperties.getProperty("docx4j.velocity.runtime.log", "velocity.log"));
ps.setProperty(";runtime.log.logsystem.class", Docx4jProperties.getProperty("docx4j.velocity.runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"));
ps.setProperty("resource.loader", Docx4jProperties.getProperty("docx4j.velocity.resource.loader", "file"));
ps.setProperty("file.resource.loader.cache", Docx4jProperties.getProperty("docx4j.velocity.file.resource.loader.cache", "true"));
ps.setProperty("file.resource.loader.class ", Docx4jProperties.getProperty("docx4j.velocity.file.resource.loader.class", "Velocity.Runtime.Resource.Loader.FileResourceLoader") );
ps.setProperty(";resource.loader", Docx4jProperties.getProperty("docx4j.velocity.resource.loader", "webapp"));
ps.setProperty(";webapp.resource.loader.class", Docx4jProperties.getProperty("docx4j.velocity.webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader"));
ps.setProperty(";webapp.resource.loader.cache", Docx4jProperties.getProperty("docx4j.velocity.webapp.resource.loader.cache", "true"));
ps.setProperty(";webapp.resource.loader.modificationCheckInterval", Docx4jProperties.getProperty("docx4j.velocity.webapp.resource.loader.modificationCheckInterval", "3") );
ps.setProperty(";directive.foreach.counter.name", Docx4jProperties.getProperty("docx4j.velocity.directive.foreach.counter.name", "velocityCount"));
ps.setProperty(";directive.foreach.counter.initial.value", Docx4jProperties.getProperty("docx4j.velocity.directive.foreach.counter.initial.value", "1"));
ps.setProperty("file.resource.loader.path", this.getClass().getResource(Docx4jProperties.getProperty("docx4j.velocity.file.resource.loader.path", "/template")).getPath());
//模板輸入輸出編碼格式
String input_charset = Docx4jProperties.getProperty("docx4j.velocity.input.encoding", Docx4jConstants.DEFAULT_CHARSETNAME);
String output_charset = Docx4jProperties.getProperty("docx4j.velocity.output.encoding", Docx4jConstants.DEFAULT_CHARSETNAME );
ps.setProperty("input.encoding", input_charset);
ps.setProperty("output.encoding", output_charset);
engine.init(ps);
return engine;
}
示例8: generateHtmlChunks
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
private static List<String> generateHtmlChunks(List<ReportItem> reportItemList) {
List<String> htmlChunks = new ArrayList<>();
VelocityEngine velocityEngine = new VelocityEngine();
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init(p);
Template template = velocityEngine.getTemplate("template/report_template.html");
int maxItemsInReport = CliHelper.getMaxItemsInReport();
List<List<ReportItem>> reportItemsChunks = Lists.partition(reportItemList, maxItemsInReport);
for (List<ReportItem> reportItemsChunk : reportItemsChunks ) {
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("jarPath", CliHelper.getPathToAnalyze());
velocityContext.put("ruleName", reportItemsChunk.get(0).getRuleName());
velocityContext.put("reportItems", reportItemsChunk);
StringWriter stringWriter = new StringWriter();
template.merge(velocityContext, stringWriter);
htmlChunks.add(stringWriter.toString());
}
return htmlChunks;
}
示例9: postProcessVelocityEngine
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
/**
* Provides a ClasspathResourceLoader in addition to any default or user-defined
* loader in order to load the spring Velocity macros from the class path.
* @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
*/
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
velocityEngine.setProperty(
SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
velocityEngine.addProperty(
VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
velocityEngine.addProperty(
VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
if (logger.isInfoEnabled()) {
logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
"' added to configured VelocityEngine");
}
}
示例10: getMessageEncoder
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
/**
* Build the WebSSO handler for sending and receiving SAML2 messages.
*
* @param ctx the ctx
* @param spssoDescriptor the spsso descriptor
* @return the encoder instance
*/
private MessageEncoder getMessageEncoder(final SAML2MessageContext ctx) {
final Pac4jSAMLResponse adapter = ctx.getProfileRequestContextOutboundMessageTransportResponse();
if (SAMLConstants.SAML2_POST_BINDING_URI.equals(destinationBindingType)) {
final VelocityEngine velocityEngine = VelocityEngineFactory.getEngine();
final Pac4jHTTPPostEncoder encoder = new Pac4jHTTPPostEncoder(adapter);
encoder.setVelocityEngine(velocityEngine);
return encoder;
}
if (SAMLConstants.SAML2_REDIRECT_BINDING_URI.equals(destinationBindingType)) {
return new Pac4jHTTPRedirectDeflateEncoder(adapter, forceSignRedirectBindingAuthnRequest);
}
throw new UnsupportedOperationException("Binding type - "
+ destinationBindingType + " is not supported");
}
示例11: getEngine
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
public static VelocityEngine getEngine() {
try {
final Properties props =
new Properties();
props.putAll(net.shibboleth.utilities.java.support.velocity.VelocityEngine.getDefaultProperties());
props.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
props.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
props.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, SLF4JLogChute.class.getName());
final VelocityEngine velocityEngine =
net.shibboleth.utilities.java.support.velocity.VelocityEngine
.newVelocityEngine(props);
return velocityEngine;
} catch (final Exception e) {
throw new TechnicalException("Error configuring velocity", e);
}
}
示例12: format
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
public static String format(VelocityEngine ve,Reader reader, Object dataModel){
if(ve == null){
ve = VlocityFormat.ve;
}
Context context = createVelocityContext(dataModel);
//raw return
if(context == null){
return reader.toString();
}
StringWriter writer = new StringWriter();
ve.evaluate(context, writer, "logTag", reader);
return writer.toString();
}
示例13: renderNonWebAppTemplate
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
@Test
public void renderNonWebAppTemplate() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
VelocityAutoConfiguration.class);
try {
VelocityEngine velocity = context.getBean(VelocityEngine.class);
StringWriter writer = new StringWriter();
Template template = velocity.getTemplate("message.vm");
template.process();
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("greeting", "Hello World");
template.merge(velocityContext, writer);
assertThat(writer.toString()).contains("Hello World");
}
finally {
context.close();
}
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:19,代碼來源:VelocityAutoConfigurationTests.java
示例14: velocityEngineFactoryBeanWithVelocityProperties
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
@Test
public void velocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException {
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
Properties props = new Properties();
props.setProperty("myprop", "/mydir");
vefb.setVelocityProperties(props);
Object value = new Object();
Map<String, Object> map = new HashMap<>();
map.put("myentry", value);
vefb.setVelocityPropertiesMap(map);
vefb.afterPropertiesSet();
assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
VelocityEngine ve = vefb.getObject();
assertEquals("/mydir", ve.getProperty("myprop"));
assertEquals(value, ve.getProperty("myentry"));
}
示例15: renderSchema
import org.apache.velocity.app.VelocityEngine; //導入依賴的package包/類
private void renderSchema(PrismSchema schema, PrismContext prismContext, VelocityEngine velocityEngine, PathGenerator pathGenerator) throws IOException {
getLog().info("Processing schema: "+schema);
VelocityContext velocityContext = new VelocityContext();
populateVelocityContextBase(velocityContext, prismContext, pathGenerator, schema, "..");
Template template = velocityEngine.getTemplate(TEMPLATE_SCHEMA_NAME);
Writer writer = new FileWriter(pathGenerator.prepareSchemaOutputFile(schema));
template.merge(velocityContext, writer);
writer.close();
// Object Definitions
for (PrismObjectDefinition objectDefinition: schema.getObjectDefinitions()) {
renderObjectDefinition(objectDefinition, schema, prismContext, velocityEngine, pathGenerator);
}
// Types
for (ComplexTypeDefinition typeDefinition : schema.getComplexTypeDefinitions()) {
renderComplexTypeDefinition(typeDefinition, schema, prismContext, velocityEngine, pathGenerator);
}
}