Women in Technology

Hear us Roar



Article:
  Migrating to Velocity
Subject:   Addendum
Date:   2004-06-08 01:34:24
From:   jrbriggs
It's just been pointed out that if you want to compile the ExitDirective code, you need to be psychic. :-)


Therefore, in the interests of those who do not include paranormal extrasensory perception amongst their personal attributes:


ExitDirective.java


import java.io.IOException;
import java.io.Writer;


import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.directive.DirectiveConstants;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.runtime.parser.node.Node;


public class ExitDirective extends Directive {
private static final String EXIT = "exit";
private static final StopRenderingException STOP_RENDER_EXCEPTION = new StopRenderingException();

public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
throw STOP_RENDER_EXCEPTION;
}

public int getType() {
return DirectiveConstants.LINE;
}

public String getName() {
return EXIT;
}

}


StopRenderingException.java


import org.apache.velocity.exception.MethodInvocationException;


public class StopRenderingException extends MethodInvocationException {

public StopRenderingException() {
super("", null, "");
}

}


Plus you'll need to add the directive to your VelocityEngine with something like:


ve.setProperty("userdirective", "ExitDirective");


Hope that helps... and if it doesn't, please email me.


JRB