org.apache.velocity.app.event.EventCartridge Java Examples

The following examples show how to use org.apache.velocity.app.event.EventCartridge. 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: CucumberITGeneratorByFeature.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 6 votes vote down vote up
private void writeContentFromTemplate(final Writer writer) {
    // to escape java
    final EventCartridge ec = new EventCartridge();
    ec.addEventHandler(new EscapeJavaReference());

    final VelocityContext context = new VelocityContext();
    context.attachEventCartridge(ec);
    context.put("strict", overriddenParameters.isStrict());
    context.put("featureFile", featureFileLocation);
    context.put("plugins", createPluginStrings());
    context.put("tags", overriddenParameters.getTags());
    context.put("monochrome", overriddenParameters.isMonochrome());
    context.put("cucumberOutputDir", normalizePathSeparator(config.getCucumberOutputDir()));
    context.put("glue", overriddenParameters.getGlue());
    context.put("className", FilenameUtils.removeExtension(outputFileName));
    context.put("packageName", config.getPackageName());
    context.put("feature",parsedFeature);

    velocityTemplate.merge(context, writer);
}
 
Example #2
Source File: EventHandlingTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testManualEventHandlers()
        throws Exception
{
    TestEventCartridge te = new TestEventCartridge();
    /**
     * Test attaching the event cartridge to the context.
     *  Make an event cartridge, register all the
     *  event handlers (at once) and attach it to the
     *  Context
     */

    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(te);
    ec.attachToContext(context);

    /*
     *  now wrap the event cartridge - we want to make sure that
     *  we can do this w/o harm
     */
    doTestReferenceInsertionEventHandler1();
    doTestReferenceInsertionEventHandler2();
    doTestMethodExceptionEventHandler1();
    doTestMethodExceptionEventHandler2();
}
 
Example #3
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Test reporting of invalid syntax
 * @throws Exception
 */
public void testReportInvalidReferences1() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    context.put("n1", null);
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar() $!c1 $n1 $!n1 #if($c1) nop #end");

    List errors = reporter.getInvalidReferences();
    assertEquals(2,errors.size());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
    assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());

    log("Caught invalid references (local configuration).");
}
 
Example #4
Source File: CucumberITGeneratorByScenario.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 6 votes vote down vote up
private void writeContentFromTemplate(final Writer writer) {
    // to escape java
    final EventCartridge ec = new EventCartridge();
    ec.addEventHandler(new EscapeJavaReference());

    final VelocityContext context = new VelocityContext();
    context.attachEventCartridge(ec);
    context.put("strict", overriddenParameters.isStrict());
    context.put("featureFile", featureFileLocation);
    context.put("plugins", createPluginStrings());
    context.put("tags", overriddenParameters.getTags());
    context.put("monochrome", overriddenParameters.isMonochrome());
    context.put("cucumberOutputDir", normalizePathSeparator(config.getCucumberOutputDir()));
    context.put("glue", overriddenParameters.getGlue());
    context.put("className", FilenameUtils.removeExtension(outputFileName));
    context.put("packageName", config.getPackageName());
    context.put("feature", parsedFeature);
    context.put("scenario", parsedScenario);

    velocityTemplate.merge(context, writer);
}
 
Example #5
Source File: ConfigurableLDAPResolver.java    From XACML with MIT License 5 votes vote down vote up
private String evaluateVelocityTemplate(String template,
		final Map<String,PIPRequest> templateParameters,
		final PIPEngine pipEngine,
		final PIPFinder pipFinder) 
				throws PIPException {
	StringWriter out = new StringWriter();
	VelocityContext vctx = new VelocityContext();
	EventCartridge vec = new EventCartridge();
	VelocityParameterWriter writer = new VelocityParameterWriter(
			pipEngine, pipFinder, templateParameters);
	vec.addEventHandler(writer);
	vec.attachToContext(vctx);

	try {
		Velocity.evaluate(vctx, out,
				"LdapResolver", template);
	}
	catch (ParseErrorException pex) {
		throw new PIPException(
				"Velocity template evaluation failed",pex);
	}
	catch (MethodInvocationException mix) {
		throw new PIPException(
				"Velocity template evaluation failed",mix);
	}
	catch (ResourceNotFoundException rnfx) {
		throw new PIPException(
				"Velocity template evaluation failed",rnfx);
	}

	this.logger.warn("(" + id + ") " + " template yields " + out.toString());

	return out.toString();
}
 
Example #6
Source File: InternalContextBase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
 */
public EventCartridge attachEventCartridge( EventCartridge ec )
{
    EventCartridge temp = eventCartridge;

    eventCartridge = ec;

    return temp;
}
 
Example #7
Source File: ConfigurableLDAPResolver.java    From XACML with MIT License 5 votes vote down vote up
private Set<String> prepareVelocityTemplate(String template)
		throws PIPException {
	VelocityContext vctx = new VelocityContext();
	EventCartridge vec = new EventCartridge();
	VelocityParameterReader reader = new VelocityParameterReader();
	vec.addEventHandler(reader);
	vec.attachToContext(vctx);

	try {
		Velocity.evaluate(vctx, new StringWriter(),
				"LdapResolver", template);
	}
	catch (ParseErrorException pex) {
		throw new PIPException(
				"Velocity template preparation failed",pex);
	}
	catch (MethodInvocationException mix) {
		throw new PIPException(
				"Velocity template preparation failed",mix);
	}
	catch (ResourceNotFoundException rnfx) {
		throw new PIPException(
				"Velocity template preparation failed",rnfx);
	}
	if (this.logger.isTraceEnabled()) {
		this.logger.trace("(" + id + ") " + template + " with parameters " + reader.parameters);
	}

	return reader.parameters;
}
 
Example #8
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test reporting of invalid syntax
 * @throws Exception
 */
public void testReportTestedInvalidReferences() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("event_handler.invalid_references.tested","true");
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    context.put("n1", null);
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar() $!c1 $n1 $!n1 #if($c1) nop #end");

    List errors = reporter.getInvalidReferences();
    assertEquals(3,errors.size());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
    assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(2)).getInvalidReference());

    log("Caught invalid references (local configuration).");
}
 
Example #9
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test reporting of invalid syntax
 * @throws Exception
 */
public void testReportNullQuietInvalidReferences() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("event_handler.invalid_references.quiet","true");
    ve.setProperty("event_handler.invalid_references.null","true");
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    context.put("n1", null);
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar() $!c1 $n1 $!n1 #if($c1) nop #end");

    List errors = reporter.getInvalidReferences();
    assertEquals(5,errors.size());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
    assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(2)).getInvalidReference());
    assertEquals("$n1",((InvalidReferenceInfo) errors.get(3)).getInvalidReference());
    assertEquals("$n1",((InvalidReferenceInfo) errors.get(4)).getInvalidReference());

    log("Caught invalid references (local configuration).");
}
 
Example #10
Source File: InternalContextAdapterImpl.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
 */
public EventCartridge attachEventCartridge( EventCartridge ec )
{
    if (iec != null)
    {
        return iec.attachEventCartridge( ec );
    }

    return null;
}
 
Example #11
Source File: InternalContextAdapterImpl.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
 */
public EventCartridge getEventCartridge()
{
    if ( iec != null)
    {
        return iec.getEventCartridge( );
    }

    return null;
}
 
Example #12
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test reporting of invalid syntax
 * @throws Exception
 */
public void testReportNullInvalidReferences() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("event_handler.invalid_references.null","true");
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    context.put("n1", null);
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar() $!c1 $n1 $!n1 #if($c1) nop #end");

    List errors = reporter.getInvalidReferences();
    assertEquals(3,errors.size());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
    assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());
    assertEquals("$n1",((InvalidReferenceInfo) errors.get(2)).getInvalidReference());

    log("Caught invalid references (local configuration).");
}
 
Example #13
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test reporting of invalid syntax
 * @throws Exception
 */
public void testReportQuietInvalidReferences() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("event_handler.invalid_references.quiet","true");
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    context.put("n1", null);
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar() $!c1 $n1 $!n1 #if($c1) nop #end");

    List errors = reporter.getInvalidReferences();
    assertEquals(3,errors.size());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
    assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());
    assertEquals("$c1",((InvalidReferenceInfo) errors.get(2)).getInvalidReference());

    log("Caught invalid references (local configuration).");
}
 
Example #14
Source File: BuiltInEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testReportInvalidReferences2() throws Exception
{
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("event_handler.invalid_references.exception","true");
    ReportInvalidReferences reporter = new ReportInvalidReferences();
    ve.init();

    VelocityContext context = new VelocityContext();
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(reporter);
    ec.attachToContext(context);

    context.put("a1","test");
    context.put("b1","test");
    Writer writer = new StringWriter();

    ve.evaluate(context,writer,"test","$a1 no problem");

    try {
        ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar()");
        fail ("Expected exception.");
    } catch (RuntimeException E) {}


    log("Caught invalid references (global configuration).");

}
 
Example #15
Source File: InvalidEventHandlerTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testManualEventHandlers()
throws Exception
{
    TestEventCartridge te = new TestEventCartridge();

    /**
     * Test attaching the event cartridge to the context
     */
    VelocityEngine ve = new VelocityEngine();
    ve.init();

    /*
     *  lets make a Context and add the event cartridge
     */

    VelocityContext inner = new VelocityContext();

    /*
     *  Now make an event cartridge, register all the
     *  event handlers (at once) and attach it to the
     *  Context
     */

    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(te);
    ec.attachToContext( inner );

    doTestInvalidReferenceEventHandler0(ve, inner);
    doTestInvalidReferenceEventHandler1(ve, inner);
    doTestInvalidReferenceEventHandler2(ve, inner);
    doTestInvalidReferenceEventHandler3(ve, inner);
    doTestInvalidReferenceEventHandler4(ve, inner);
}
 
Example #16
Source File: Velocity758TestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testOverrideNullArgumentForParse()
{
    String nullContent = "Parse arg was null";
    addTemplate("null.vm", nullContent);

    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(new Handler());
    ec.attachToContext(context);

    assertEvalEquals(nullContent, "#parse($foo)");
}
 
Example #17
Source File: EventHandler.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
public EventHandler(Context ctx) {
  EventCartridge ec = new EventCartridge();
  ec.addEventHandler(this);
  ec.attachToContext(ctx);
}
 
Example #18
Source File: IncludeEventHandlingTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Runs the test.
 */
public void testIncludeEventHandling ()
        throws Exception
{
    Template template1 = RuntimeSingleton.getTemplate(
        getFileName(null, "test1", TMPL_FILE_EXT));

    Template template2 = RuntimeSingleton.getTemplate(
        getFileName(null, "subdir/test2", TMPL_FILE_EXT));

    Template template3 = RuntimeSingleton.getTemplate(
        getFileName(null, "test3", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));

    FileOutputStream fos2 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));

    FileOutputStream fos3 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
    Writer writer3 = new BufferedWriter(new OutputStreamWriter(fos3));

    /*
     *  lets make a Context and add the event cartridge
     */

    Context context = new VelocityContext();

    /*
     *  Now make an event cartridge, register the
     *  input event handler and attach it to the
     *  Context
     */

    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(this);
    ec.attachToContext( context );


    // BEHAVIOR A: pass through #input and #parse with no change
    EventHandlerBehavior = PASS_THROUGH;

    template1.merge(context, writer1);
    writer1.flush();
    writer1.close();

    // BEHAVIOR B: pass through #input and #parse with using a relative path
    EventHandlerBehavior = RELATIVE_PATH;

    template2.merge(context, writer2);
    writer2.flush();
    writer2.close();

    // BEHAVIOR C: refuse to pass through #input and #parse
    EventHandlerBehavior = BLOCK;

    template3.merge(context, writer3);
    writer3.flush();
    writer3.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test1",
            RESULT_FILE_EXT, CMP_FILE_EXT) ||
        !isMatch(RESULTS_DIR, COMPARE_DIR, "test2",
            RESULT_FILE_EXT, CMP_FILE_EXT) ||
        !isMatch(RESULTS_DIR, COMPARE_DIR, "test3",
            RESULT_FILE_EXT, CMP_FILE_EXT)
            )
    {
        fail("Output incorrect.");
    }
}
 
Example #19
Source File: ChainedInternalContextAdapter.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
 */
public EventCartridge getEventCartridge()
{
    return wrappedContext.getEventCartridge();
}
 
Example #20
Source File: ChainedInternalContextAdapter.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
 */
public EventCartridge attachEventCartridge(EventCartridge ec)
{
    return wrappedContext.attachEventCartridge(ec);
}
 
Example #21
Source File: InternalContextBase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
 */
public EventCartridge getEventCartridge()
{
    return eventCartridge;
}
 
Example #22
Source File: RuntimeInstance.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the event handlers for the application.
 * @return The event handlers for the application.
 * @since 1.5
 */
public EventCartridge getApplicationEventCartridge()
{
    return eventCartridge;
}
 
Example #23
Source File: RuntimeServices.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the event handlers for the application.
 * @return The event handlers for the application.
 */
EventCartridge getApplicationEventCartridge();
 
Example #24
Source File: RuntimeSingleton.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
* Returns the event handlers for the application.
* @return The event handlers for the application.
* @see RuntimeInstance#getApplicationEventCartridge()
* @since 1.5
*/
public EventCartridge getEventCartridge()
{
    return ri.getApplicationEventCartridge();
}
 
Example #25
Source File: InternalEventContext.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * @return The current EventCartridge.
 */
EventCartridge getEventCartridge();
 
Example #26
Source File: InternalEventContext.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * @param ec
 * @return The old EventCartridge.
 */
EventCartridge attachEventCartridge(EventCartridge ec);