/** * instantiates and loads the directive with some basic checks * * @param directiveClass * classname of directive to load */ private void loadDirective(String directiveClass, String caption) { try { Object o = Class.forName(directiveClass).newInstance(); if (o instanceof Directive) { Directive directive = (Directive) o; fDirectives.put(directive.getName(), directive); } else { // error(caption + " Directive " + directiveClass + " is not org.apache.velocity.runtime.directive.Directive." + " Ignoring. "); } } catch (Exception e) { // error("Exception Loading " + caption + " Directive: " + directiveClass + " : " + e); } }
public CodeGenerator(CodeGeneratorContext context, M model, Writer writer, URL templateUrl, String templateResource, Set<Class<? extends Directive>> directives) { this.context = context != null ? context : new CodeGeneratorContext(); this.model = model; this.writer = writer; this.templateResource = templateResource; this.templateUrl = templateUrl; this.directives = directives; StringResourceRepository repo = StringResourceLoader.getRepository(); try { repo.putStringResource(TEMPLATE, templateUrl != null ? loadResource(templateUrl) : loadResource(templateResource)); } catch (Exception e) { throw new RuntimeException(TEMPLATE_READER_FAILURE, e); } for (Class<? extends Directive> directive : directives) { context.getVelocityEngine().loadDirective(directive.getCanonicalName()); } this.template = this.context.getVelocityEngine().getTemplate(TEMPLATE); this.context.getVelocityContext().put(MODEL, model); }
/** * Initialise Velocity * * @param templatePath the path to templates */ public static final void initialise(String templatePath) { // Setup Velocity Properties velocityProperties = new Properties(); // create a list of custom directives we're using List<Class<? extends Directive>> userDirectiveClasses = new ArrayList<Class<? extends Directive>>(); userDirectiveClasses.add(Decorate.class); // build the user directive string StringBuilder userDirectiveStr = new StringBuilder(); for (Class<? extends Directive> clz : userDirectiveClasses) { if (userDirectiveStr.length() > 0) { userDirectiveStr.append(","); } userDirectiveStr.append(clz.getName()); } // wire in our custom directives velocityProperties.setProperty("userdirective", userDirectiveStr.toString()); velocityProperties.setProperty("velocimacro.library", ""); // wire in our custom resource loader velocityProperties.setProperty("resource.loader", "relativecp"); velocityProperties.setProperty("relativecp.resource.loader.description", "Relative CP Loader"); velocityProperties.setProperty("relativecp.resource.loader.class", RelativeClasspathResourceLoader.class.getName()); velocityProperties.setProperty(RelativeClasspathResourceLoader.RELATIVE_PATH, templatePath); // initialise Velocity.init(velocityProperties); }
/** * Initialise Velocity * * @param templatePath the path to templates */ public static final void initialise(String templatePath) { // Setup Velocity Properties velocityProperties = new Properties(); // create a list of custom directives we're using List<Class<? extends Directive>> userDirectiveClasses = new ArrayList<Class<? extends Directive>>(); userDirectiveClasses.add(Decorate.class); // build the user directive string StringBuilder userDirectiveStr = new StringBuilder(); for (Class<? extends Directive> clz : userDirectiveClasses) { if (userDirectiveStr.length() > 0) { userDirectiveStr.append(","); } userDirectiveStr.append(clz.getName()); } // wire in our custom directives velocityProperties.setProperty("userdirective", userDirectiveStr.toString()); velocityProperties.setProperty("velocimacro.library", ""); // file resource loader velocityProperties.setProperty("resource.loader", "file"); velocityProperties.setProperty("file.resource.loader.path", templatePath); Velocity.init(velocityProperties); // initialise Velocity.init(velocityProperties); }
public CodeGenerator(M model, Writer writer, URL templateUrl, String templateResource, Set<Class<? extends Directive>> directives) { this(new CodeGeneratorContext(), model, writer, templateUrl, templateResource, directives); }
public Set<Class<? extends Directive>> getDirectives() { return directives; }