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

The following examples show how to use org.apache.velocity.app.Velocity#setProperty() . 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: StringResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp()
        throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    Velocity.reset();

    Velocity.setProperty(Velocity.RESOURCE_LOADERS, "string");
    Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");

    // Silence the logger.
    Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();
}
 
Example 2
Source File: MacroForwardDefineTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp()
    throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    // use Velocity.setProperty (instead of properties file) so that we can use actual instance of log
    Velocity.reset();
    Velocity.setProperty(RuntimeConstants.RESOURCE_LOADERS,"file");
    Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH );
    Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID,"true");

    // actual instance of logger
    logger.setEnabledLevel(TestLogger.LOG_LEVEL_DEBUG);
    Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, logger);
    Velocity.init();
}
 
Example 3
Source File: VelocityConfigFileWriter.java    From oodt with Apache License 2.0 6 votes vote down vote up
@Override
public File generateFile(String filePath, Metadata metadata, Logger logger,
    Object... args) throws IOException {
  File configFile = new File(filePath);
  VelocityMetadata velocityMetadata = new VelocityMetadata(metadata);
  // Velocity requires you to set a path of where to look for
  // templates. This path defaults to . if not set.
  int slashIndex = ((String) args[0]).lastIndexOf('/');
  String templatePath = ((String) args[0]).substring(0, slashIndex);
  Velocity.setProperty("file.resource.loader.path", templatePath);
  // Initialize Velocity and set context up
  Velocity.init();
  VelocityContext context = new VelocityContext();
  context.put("metadata", velocityMetadata);
  context.put("env", System.getenv());
  // Load template from templatePath
  String templateName = ((String) args[0]).substring(slashIndex);
  Template template = Velocity.getTemplate(templateName);
  // Fill out template and write to file
  StringWriter sw = new StringWriter();
  template.merge(context, sw);
  FileUtils.writeStringToFile(configFile, sw.toString());
  return configFile;
}
 
Example 4
Source File: MultipleFileResourcePathTestCase.java    From velocity-engine with Apache License 2.0 6 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_PATH1);

    Velocity.addProperty(
            Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH2);

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

    Velocity.init();
}
 
Example 5
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp() throws Exception
{
    Velocity.reset();
    Velocity.setProperty(Velocity.RESOURCE_LOADERS, "string");
    Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
    Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());
    Velocity.init();

    StringResourceRepository repo = getRepo(null, null);
    repo.putStringResource("foo", "This is $foo");
    repo.putStringResource("bar", "This is $bar");

    context = new VelocityContext();
    context.put("foo", "wonderful!");
    context.put("bar", "horrible!");
    context.put("woogie", "a woogie");
}
 
Example 6
Source File: ChainedUberspectorsTestCase.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.RUNTIME_LOG_INSTANCE, new TestLogger());
    Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.util.introspection.UberspectImpl");
    Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$ChainedUberspector");
    Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$LinkedUberspector");
 Velocity.init();
}
 
Example 7
Source File: AbstractVelocityExporter.java    From LicenseScout with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final void export(final OutputResult outputResult, final ReportConfiguration reportConfiguration)
        throws Exception {
    final File outputFile = reportConfiguration.getOutputFile();
    final Charset charset = ExporterUtil.getOutputCharset(reportConfiguration);
    try (final FileWriter fileWriter = new FileWriter(outputFile, charset);
            final BufferedWriter bw = new BufferedWriter(fileWriter)) {

        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,classpath");
        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER + ".file.class", FileResourceLoader.class.getName());
        /*
         * NOTE: setting the path to empty is necessary because the default is "." (current
         * directory) and then using absolute path names will not work.
         */
        Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER + ".classpath.class",
                ClasspathResourceLoader.class.getName());

        Velocity.init();

        final VelocityContext context = createVelocityContext(outputResult, reportConfiguration);
        final Template template = getTemplate(reportConfiguration);

        try (final StringWriter sw = new StringWriter()) {
            if (template != null) {
                template.merge(context, sw);
            }
            bw.write(sw.getBuffer().toString());
        }
    }
}
 
Example 8
Source File: VelTools66TestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{
    Velocity.setProperty(
            Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();
    System.setSecurityManager(new TestSecurityManager());

}
 
Example 9
Source File: ClasspathResourceTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    Velocity.reset();
    Velocity.setProperty(Velocity.RESOURCE_LOADERS, "classpath");

    /*
     * I don't think I should have to do this, these should
     * be in the default config file.
     */

    Velocity.addProperty(
            "classpath." + Velocity.RESOURCE_LOADER + ".class",
            "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

    Velocity.setProperty(
            "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");

    Velocity.setProperty(
            "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
            "2");

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

    Velocity.init();
}
 
Example 10
Source File: ContextSafetyTestCase.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.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();
}
 
Example 11
Source File: MethodInvocationExceptionTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{
    /*
     *  init() Runtime with defaults
     */

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

    Velocity.init();
}
 
Example 12
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 13
Source File: GeneratorUtil.java    From vaadinator with Apache License 2.0 5 votes vote down vote up
public static void runVelocity(BeanDescription desc, Map<String, Object> commonMap, String pckg, String modelPckg,
		String presenterPckg, String viewPckg, String profileName, String templateName, File outFile,
		boolean mandatory, String templatePackage, Log log) throws IOException {
	Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
	Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
	Template template;
	// Issue #6: for optional templates check whether it's there
	boolean runTemplate;
	if (mandatory) {
		runTemplate = true;
	} else {
		runTemplate = isTemplateExisting(templateName, templatePackage);
	}
	if (!runTemplate) {
		return;
	}
	template = Velocity.getTemplate(templatePackage + templateName);
	String className = desc != null ? desc.getClassName() : "no description found ";
	log.debug("Create file with template: "+ template.getName() + " for bean " + className + " in profile: " + profileName);
	VelocityContext context = new VelocityContext();
	context.put("bean", desc);
	context.put("common", commonMap);
	context.put("package", pckg);
	context.put("modelPackage", modelPckg);
	context.put("presenterPackage", presenterPckg);
	context.put("viewPackage", viewPckg);
	context.put("profileName", profileName);
	context.put("unicodeUtil", UnicodeUtil.SINGLETON);
	Writer writer = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
	template.merge(context, writer);
	writer.close();
	log.info("Written file: " + outFile);
}
 
Example 14
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 15
Source File: JpaMappingCodeGenerator.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generate the JPA Mapping of one BusinessModel in one outputFile
 *
 * @param model      BusinessModel
 * @param outputFile File
 */
public void generateJpaMapping(BusinessModel model, boolean isUpdatableMapping) {

	logger.trace("IN");

	Velocity.setProperty("file.resource.loader.path", getTemplateDir().getAbsolutePath());
	Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
	Velocity.setProperty("runtime.log.logsystem.log4j.category", "velocity");
	Velocity.setProperty("runtime.log.logsystem.log4j.logger", "velocity");

	JpaModel jpaModel = new JpaModel(model);
	generateBusinessTableMappings(jpaModel.getTables(), isUpdatableMapping);
	logger.info("Java files for tables of model [{}] succesfully created", model.getName());

	generateBusinessViewMappings(jpaModel.getViews(), isUpdatableMapping);
	logger.info("Java files for views of model [{}] succesfully created", model.getName());

	createLabelsFile(labelsTemplate, jpaModel);
	logger.info("Labels file for model [{}] succesfully created", model.getName());

	createPropertiesFile(propertiesTemplate, jpaModel);
	logger.info("Properties file for model [{}] succesfully created", model.getName());

	generatePersistenceUnitMapping(jpaModel);
	logger.info("Persistence unit for model [{}] succesfully created", model.getName());

	createCfieldsFile(cfieldsTemplate, jpaModel);
	logger.info("Calculated fields file for model [{}] succesfully created", model.getName());

	createRelationshipFile(relationshipsTemplate, jpaModel);
	logger.info("Relationships file for model [{}] succesfully created", model.getName());

	generateHierarchiesFile(hierarchiesTemplate, model);
	logger.info("Hierarchies file for model [{}] succesfully created", model.getName());

	logger.trace("OUT");
}
 
Example 16
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();
}
 
Example 17
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 18
Source File: WebServer.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public WebServer(int httpport, DataStore data, MetadataStore metadata) throws IOException, URISyntaxException {
    super(httpport);

    extractWebUI();

    this.data = data;
    this.metadata = metadata;

    // Initialize Velocity template engine
    try {
        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
        Velocity.setProperty("runtime.log.logsystem.log4j.logger", "edu.berkeley.xtrace.server.XTraceServer");
        Velocity.setProperty("file.resource.loader.path", webui + "/templates");
        Velocity.setProperty("file.resource.loader.cache", "true");
        Velocity.init();
    } catch (Exception e) {
        LOG.warn("Failed to initialize Velocity", e);
    }

    // Create Jetty server
    Context context = new Context(this, "/");

    // Create a CGI servlet for scripts in webui/cgi-bin
    ServletHolder cgiHolder = new ServletHolder(new CGI());
    cgiHolder.setInitParameter("cgibinResourceBase", webui + "/cgi-bin");

    // Pass any special PATH setting on to the execution environment
    if (System.getenv("PATH") != null)
        cgiHolder.setInitParameter("Path", System.getenv("PATH"));

    context.addServlet(cgiHolder, "*.cgi");
    context.addServlet(cgiHolder, "*.pl");
    context.addServlet(cgiHolder, "*.py");
    context.addServlet(cgiHolder, "*.rb");
    context.addServlet(cgiHolder, "*.tcl");
    context.addServlet(new ServletHolder(new GetReportsServlet()), "/reports/*");
    context.addServlet(new ServletHolder(new TagServlet()), "/tag/*");
    context.addServlet(new ServletHolder(new TitleServlet()), "/title/*");
    context.addServlet(new ServletHolder(new TitleLikeServlet()), "/titleLike/*");

    // JSON APIs for interactive visualization
    context.addServlet(new ServletHolder(new GetJSONReportsServlet()), "/interactive/reports/*");
    context.addServlet(new ServletHolder(new GetOverlappingTasksServlet()), "/interactive/overlapping/*");
    context.addServlet(new ServletHolder(new GetTagsForTaskServlet()), "/interactive/tags/*");
    context.addServlet(new ServletHolder(new GetTasksForTags()), "/interactive/taggedwith/*");

    context.setResourceBase(webui + "/html");
    context.addServlet(new ServletHolder(new IndexServlet()), "/");
}
 
Example 19
Source File: ClassMapTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public void setUp()
           throws Exception
   {
       Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());
Velocity.init();
   }
 
Example 20
Source File: Velocity580TestCase.java    From velocity-engine with Apache License 2.0 3 votes vote down vote up
public void setUp() throws Exception
{

    assureResultsDirectoryExists(RESULTS_DIR);

    Velocity.reset();

    Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH, TEMPLATE_DIR);

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

    Velocity.init();
}