Search in sources :

Example 11 with Template

use of org.apache.velocity.Template in project Ebselen by Ardesco.

the class IDEToEbselen method generateJavaFile.

/**
     * Writes Sky Selenium format test code into a Java file ready for tests to be run
     *
     * @param name - Name of the test
     * @throws Exception
     */
public void generateJavaFile(String name) throws Exception {
    Properties props = new Properties();
    props.setProperty("resource.loader", "class");
    props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    props.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
    VelocityEngine ve = new VelocityEngine(props);
    VelocityContext context = new VelocityContext();
    context.put("template", name);
    context.put("templateclass", name + ".class");
    context.put("testname", name);
    context.put("testdata", testCode);
    Template ebselenTemplate = ve.getTemplate(ebselenTestTemplate);
    FileHandler convertedFile = new FileHandler(conversionLocation + File.separator + name + ".java", true);
    StringWriter writer = new StringWriter();
    ebselenTemplate.merge(context, writer);
    convertedFile.write(writer.toString());
    convertedFile.close();
    LOGGER.info("Selenium IDE test converted and saved as '" + convertedFile.getFilePath() + convertedFile.getFileName() + "'");
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Properties(java.util.Properties) CleanerProperties(org.htmlcleaner.CleanerProperties) Template(org.apache.velocity.Template) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 12 with Template

use of org.apache.velocity.Template in project Asqatasun by Asqatasun.

the class CodeGeneratorMojo method generate.

/**
     *
     * @param ve
     * @param records
     * @throws java.io.IOException
     */
public void generate(VelocityEngine ve, Iterable<CSVRecord> records) throws IOException, ResourceNotFoundException, ParseErrorException, Exception {
    // Getting the Template
    Template ruleTemplate = ve.getTemplate(templateRule.getPath());
    Template pomTemplate = ve.getTemplate(pom.getPath());
    Template webappBeansTemplate = ve.getTemplate(templateBeansWebapp.getPath());
    Template webappBeansExpressionTemplate = ve.getTemplate(templateBeansExpression.getPath());
    Template auditResultConsoleTemplate = ve.getTemplate(templateAuditResultConsole.getPath());
    Template auditSetUpFormTemplate = ve.getTemplate(templateAuditSetUpForm.getPath());
    Template testCaseTemplate = ve.getTemplate(templateTestCase.getPath());
    Template descriptorTemplate = ve.getTemplate(templateDescriptor.getPath());
    Template installTemplate = ve.getTemplate(templateInstallSh.getPath());
    Template unitTestTemplate = ve.getTemplate(templateUnitTest.getPath());
    Template ruleImplementationTestCaseTemplate = ve.getTemplate(templateRuleImplementationTestCase.getPath());
    // Create a context and add data to the templateRule placeholder
    VelocityContext context = new VelocityContext();
    // Fetch templateRule into a StringWriter
    FileGenerator fg = new FileGenerator(referential, referentialLabel, destinationFolder, refDescriptor, isCriterionPresent);
    fg.createI18NFiles(langSet);
    // using the i18n keys.
    for (CSVRecord record : records) {
        String testLabelDefault = record.get(TEST_LABEL_COLUMN_NAME + langSet.first());
        String test = record.get(TEST_CODE_COLUMN_NAME);
        for (String lang : langSet) {
            writeToI18NFile(fg, record, lang);
        }
        IS_I18N_REFERENTIAL_CREATED = true;
        if (!isCriterionPresent) {
            test = test.concat("-1");
        }
        context = fg.getContextRuleClassFile(referential, PACKAGE_NAME, test, testLabelDefault, context);
        fg.writeFileCodeGenerate(context, ruleTemplate, getClassNameFromCsvColumn(record));
        fg.writeUnitTestGenerate(context, unitTestTemplate, testLabelDefault, getClassNameFromCsvColumn(record));
        String[] testsCasesState = { "Passed", "Failed", "NMI", "NA" };
        for (int i = 0; i < testsCasesState.length; i++) {
            context.put("state", testsCasesState[i]);
            fg.writeTestCaseGenerate(context, testCaseTemplate, getClassNameFromCsvColumn(record), String.valueOf(i + 1));
        }
        addLevelAndScopeToList(record);
    }
    fg.createSqlReference();
    fg.createSqlTheme();
    fg.createSqlCritere();
    fg.createSqlTest(levelList, scopeList);
    fg.createSqlParameters();
    fg.writeAuditSetUpFormBeanGenerate(context, auditSetUpFormTemplate);
    fg.writeAuditResultConsoleBeanGenerate(context, auditResultConsoleTemplate);
    fg.writeWebappBeansGenerate(context, webappBeansTemplate);
    fg.writeWebappBeansExpressionGenerate(context, webappBeansExpressionTemplate);
    fg.writeInstallGenerate(context, installTemplate);
    fg.writeDescriptorGenerate(context, descriptorTemplate);
    fg.writeRuleImplementationTestCaseGenerate(context, ruleImplementationTestCaseTemplate);
    fg.adaptPom(context, pomTemplate);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) CSVRecord(org.apache.commons.csv.CSVRecord) Template(org.apache.velocity.Template)

Example 13 with Template

use of org.apache.velocity.Template in project jena by apache.

the class SimpleVelocity method process.

/** Process a template */
public static void process(String base, String path, Writer out, VelocityContext context) {
    VelocityEngine velocity = new VelocityEngine();
    // Turn off logging - catch exceptions and log ourselves
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute);
    velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
    velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base);
    velocity.init();
    try {
        Template temp = velocity.getTemplate(path);
        temp.merge(context, out);
        out.flush();
    } catch (ResourceNotFoundException ex) {
        velocityLog.error("Resource not found: " + ex.getMessage());
    } catch (ParseErrorException ex) {
        velocityLog.error("Parse error (" + path + ") : " + ex.getMessage());
    } catch (MethodInvocationException ex) {
        velocityLog.error("Method invocation exception (" + path + ") : " + ex.getMessage());
    } catch (IOException ex) {
        velocityLog.warn("IOException", ex);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 14 with Template

use of org.apache.velocity.Template in project logging-log4j2 by apache.

the class VelocityTest method testVelocity.

@Test
public void testVelocity() {
    Velocity.init();
    final VelocityContext vContext = new VelocityContext();
    vContext.put("name", new String("Velocity"));
    final Template template = Velocity.getTemplate("target/test-classes/hello.vm");
    final StringWriter sw = new StringWriter();
    template.merge(vContext, sw);
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Test(org.junit.Test)

Example 15 with Template

use of org.apache.velocity.Template in project maven-plugins by apache.

the class AnnouncementMojo method processTemplate.

/**
     * Create the velocity template
     *
     * @param context velocity context that has the parameter values
     * @param outputDirectory directory where the file will be generated
     * @param template velocity template which will the context be merged
     * @param announcementFile The file name of the generated announcement
     * @throws VelocityException in case of errors.
     * @throws MojoExecutionException in case of errors.
     */
public void processTemplate(Context context, File outputDirectory, String template, String announcementFile) throws VelocityException, MojoExecutionException {
    File f;
    // Use the name of the template as a default value
    if (StringUtils.isEmpty(announcementFile)) {
        announcementFile = template;
    }
    try {
        f = new File(outputDirectory, announcementFile);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        VelocityEngine engine = velocity.getEngine();
        engine.setApplicationAttribute("baseDirectory", basedir);
        if (StringUtils.isEmpty(templateEncoding)) {
            templateEncoding = ReaderFactory.FILE_ENCODING;
            getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding + ", i.e. build is platform dependent!");
        }
        Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding);
        Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding);
        velocityTemplate.merge(context, writer);
        writer.flush();
        writer.close();
        getLog().info("Created template " + f);
    } catch (ResourceNotFoundException rnfe) {
        throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
    } catch (VelocityException ve) {
        throw new VelocityException(ve.toString());
    } catch (Exception e) {
        if (e.getCause() != null) {
            getLog().warn(e.getCause());
        }
        throw new MojoExecutionException(e.toString(), e.getCause());
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileOutputStream(java.io.FileOutputStream) VelocityException(org.apache.velocity.exception.VelocityException) OutputStreamWriter(java.io.OutputStreamWriter) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) VelocityException(org.apache.velocity.exception.VelocityException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)33 VelocityContext (org.apache.velocity.VelocityContext)20 StringWriter (java.io.StringWriter)11 IOException (java.io.IOException)9 VelocityEngine (org.apache.velocity.app.VelocityEngine)9 File (java.io.File)6 Writer (java.io.Writer)6 Properties (java.util.Properties)5 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)5 FileWriter (java.io.FileWriter)4 FileOutputStream (java.io.FileOutputStream)3 PrintWriter (java.io.PrintWriter)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringReader (java.io.StringReader)2 Map (java.util.Map)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 VelocityException (org.apache.velocity.exception.VelocityException)2