Java Code Examples for org.apache.velocity.app.Velocity#init()

The following examples show how to use org.apache.velocity.app.Velocity#init() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VelocityUtils.java    From EasyReport with Apache License 2.0 6 votes vote down vote up
/**
 * 替换的模板中的参数
 *
 * @param template
 * @param parameters
 * @param logTag
 * @return 替换后的文本
 */
public static String parse(final String template, final Map<String, Object> parameters, final String logTag) {
    try (StringWriter writer = new StringWriter()) {
        Velocity.init();
        final VelocityContext context = new VelocityContext();
        for (final Entry<String, Object> kvset : parameters.entrySet()) {
            context.put(kvset.getKey(), kvset.getValue());
        }
        context.put("Calendar", Calendar.getInstance());
        context.put("DateUtils", DateUtils.class);
        context.put("StringUtils", StringUtils.class);
        Velocity.evaluate(context, writer, logTag, template);
        return writer.toString();
    } catch (final Exception ex) {
        throw new TemplatePraseException(ex);
    }
}
 
Example 2
Source File: VelocityUsage.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void usage1(String inputFile) throws FileNotFoundException {
   Velocity.init();

    VelocityContext context = new VelocityContext();

    context.put("author", "Elliot A.");
    context.put("address", "217 E Broadway");
    context.put("phone", "555-1337");

    FileInputStream file = new FileInputStream(inputFile);

    //Evaluate
    StringWriter swOut = new StringWriter();
    Velocity.evaluate(context, swOut, "test", file);

    String result =  swOut.getBuffer().toString();
    System.out.println(result);
}
 
Example 3
Source File: MailUtil.java    From springboot-learn with MIT License 6 votes vote down vote up
/**
 * 获取velocity邮件模板内容
 *
 * @param map 模板中需替换的参数内容
 * @return
 */
public static String getTemplateText(String templatePath, String charset, Map<String, Object> map) {
    String templateText;
    // 设置velocity资源加载器
    Properties prop = new Properties();
    prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(prop);
    VelocityContext context = new VelocityContext(map);
    // 渲染模板
    StringWriter sw = new StringWriter();
    Template template = Velocity.getTemplate(templatePath, charset);
    template.merge(context, sw);
    try {
        templateText = sw.toString();
        sw.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("模板渲染失败");
    }
    return templateText;
}
 
Example 4
Source File: ResourceLoaderInstanceTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp()
        throws Exception
{

    ResourceLoader rl = new FileResourceLoader();

    // pass in an instance to Velocity
    Velocity.reset();
    Velocity.setProperty( "resource.loader", "testrl" );
    Velocity.setProperty( "testrl.resource.loader.instance", rl );
    Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );

    // actual instance of logger
    logger.on();
    Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, logger);
    Velocity.setProperty("runtime.log.logsystem.test.level", "debug");

    Velocity.init();
}
 
Example 5
Source File: VelocityGenerator.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static synchronized void initVelocity(boolean log) throws ToolException {
        if (initialized) {
            return;
        }
        initialized = true;
        try {
            Properties props = new Properties();
            String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
            props.put("resource.loaders", "class");
            props.put("resource.loader.class.class", clzName);
            props.put("runtime.log", getVelocityLogFile("velocity.log"));
//            if (!log) {
//                props.put(VelocityEngine.RUNTIME_LOG_INSTANCE,
//                          "org.apache.velocity.runtime.log.NullLogSystem");
//            }
            Velocity.init(props);
        } catch (Exception e) {
            Message msg = new Message("FAIL_TO_INITIALIZE_VELOCITY_ENGINE", LOG);
            LOG.log(Level.SEVERE, msg.toString());
            throw new ToolException(msg, e);
        }
    }
 
Example 6
Source File: CodeGenerator.java    From ueboot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getTemplateString(String templateName, VelocityContext context) {
    Properties p = new Properties();
    String path = Thread.currentThread().getContextClassLoader().getResource("velocity").getFile();
    p.setProperty(Velocity.RESOURCE_LOADER, "class");
    p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
    p.setProperty("class.resource.loader.path", path);
    p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
    p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
    Velocity.init(p);

    Template template = Velocity.getTemplate("velocity" + separator + "uebootv2" + separator + templateName, "UTF-8");
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return writer.toString();
}
 
Example 7
Source File: VelocityInitializer.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 初始化vm方法
 */
public static void initVelocity()
{
    Properties p = new Properties();
    try
    {
        // 加载classpath目录下的vm文件
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        // 定义字符集
        p.setProperty(Velocity.ENCODING_DEFAULT, Constants.UTF8);
        p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8);
        // 初始化Velocity引擎,指定配置Properties
        Velocity.init(p);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: Test2.java    From java-course-ee with MIT License 6 votes vote down vote up
public Test2() throws Exception {
    //init
    Velocity.init("Velocity/GS_Velocity_1/src/main/java/velocity.properties");
    // get Template
    Template template = Velocity.getTemplate("Test2.vm");
    // getContext
    Context context = new VelocityContext();

    String name = "Vova";

    context.put("name", name);
    // get Writer
    Writer writer = new StringWriter();
    // merge
    template.merge(context, writer);

    System.out.println(writer.toString());
}
 
Example 9
Source File: VelocityView.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
protected synchronized void __doViewInit(IWebMvc owner) {
    super.__doViewInit(owner);
    // 初始化Velocity模板引擎配置
    if (!__inited) {
        __velocityConfig.setProperty(Velocity.INPUT_ENCODING, DEFAULT_CHARSET);
        __velocityConfig.setProperty(Velocity.OUTPUT_ENCODING, DEFAULT_CHARSET);
        //
        if (__baseViewPath.startsWith("/WEB-INF")) {
            __velocityConfig.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, new File(RuntimeUtils.getRootPath(), StringUtils.substringAfter(__baseViewPath, "/WEB-INF/")).getPath());
        } else {
            __velocityConfig.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, __baseViewPath);
        }
        //
        Velocity.init(__velocityConfig);
        //
        __inited = true;
    }
}
 
Example 10
Source File: OrcasDbDoc.java    From orcas with Apache License 2.0 6 votes vote down vote up
public void execute( Parameters pParameters )
{
  try
  {
    ExtensionHandlerImpl lExtensionHandlerImpl = (ExtensionHandlerImpl) pParameters.getExtensionHandler();

    Properties lProperties = new Properties();
    lProperties.setProperty( RuntimeConstants.RESOURCE_LOADER, "classpath" );
    lProperties.setProperty( "classpath.resource.loader.class", ClasspathResourceLoader.class.getName() );

    Velocity.init( lProperties );

    Schema lSchema = new DbLoader().loadSchema( _diagram, lExtensionHandlerImpl.loadSyexModel(), _tableregistry );
    lSchema.mergeAssociations();

    Main.writeDiagramsRecursive( _diagram, _styles, lSchema, _outfolder, _tmpfolder, pParameters.getModelFile() + "/tables", _tableregistry, _svg, pParameters.getDbdocPlantuml() );
  }
  catch( Exception e )
  {
    throw new RuntimeException( e );
  }
}
 
Example 11
Source File: Test1.java    From java-course-ee with MIT License 6 votes vote down vote up
public Test1() throws Exception {

        //init
        Velocity.init("Velocity/GS_Velocity_1/src/main/java/velocity.properties");
        // get Template
        Template template = Velocity.getTemplate("Test1.vm");
        // getContext
        Context context = new VelocityContext();
        // get Writer
        Writer writer = new StringWriter();
        // merge
        template.merge(context, writer);

        System.out.println(writer.toString());

    }
 
Example 12
Source File: DefaultVelocityRenderer.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void init() throws Exception {
	try {
		Velocity.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
		Velocity.init();
	} catch (Throwable t) {
		_logger.error("Error initializing the VelocityEngine", t);
		//ApsSystemUtils.logThrowable(t, this, "init");
		throw new ApsSystemException("Error initializing the VelocityEngine", t);
	}
	_logger.debug("{} ready.", this.getName());
}
 
Example 13
Source File: VelocityTest.java    From easy-httpserver with Apache License 2.0 5 votes vote down vote up
@Test
	public void testVelocity() {
		Properties p =new Properties();
		p.setProperty("resource.loader", "class");
		p.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		
		Velocity.init(p);
		VelocityContext vc = new VelocityContext();
		vc.put("name", "guojing");
		StringWriter w = new StringWriter();
//		Velocity.mergeTemplate("org/eh/velocity/test.vm", "utf-8", vc, w);
		System.out.println(w);
	}
 
Example 14
Source File: EncodingTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{
    Velocity.reset();

    Velocity.setProperty(
            Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);

    Velocity.setProperty( Velocity.INPUT_ENCODING, "UTF-8" );

    Velocity.setProperty(
            Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();
}
 
Example 15
Source File: VelocimacroTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{
    /*
     *  setup local scope for templates
     */
    Velocity.reset();
    Velocity.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
    Velocity.setProperty( Velocity.VM_MAX_DEPTH, 5);
    Velocity.setProperty(
            Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());
    Velocity.init();
}
 
Example 16
Source File: TemplateService.java    From oxTrust with MIT License 5 votes vote down vote up
public void initTemplateEngine() {
	try {
		Velocity.init(getTemplateEngineConfiguration());
	} catch (Exception ex) {
		log.error("Failed to initialize Velocity", ex);
	}
}
 
Example 17
Source File: VelocityTemplateEngine.java    From kafka-connect-rest with Apache License 2.0 4 votes vote down vote up
public VelocityTemplateEngine() {
  Velocity.init();
}
 
Example 18
Source File: IncludeEventHandlingTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public void setUp()
        throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    Velocity.reset();
    Velocity.addProperty(
        Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);

    Velocity.setProperty(
            Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();


}
 
Example 19
Source File: VelocityHelper.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes the Velocity engine.
 */
public VelocityHelper()
{
    Velocity.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, true);
    Velocity.init();
}
 
Example 20
Source File: HtmlReport.java    From sahagin-java with Apache License 2.0 4 votes vote down vote up
public HtmlReport() {
    // stop generating velocity.log
    Velocity.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
            "org.apache.velocity.runtime.log.NullLogSystem");
    Velocity.init();
}