org.apache.velocity.runtime.directive.Directive Java Examples

The following examples show how to use org.apache.velocity.runtime.directive.Directive. 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: CodeGenerator.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public CodeGenerator(CodeGeneratorContext context, M model, String[] parameters, Writer writer, URL templateUrl, String templateResource, String templateContent, Set<Class<? extends Directive>> directives) {
    this.context = context != null ? context : new CodeGeneratorContext();
    this.model = model;
    this.parameters = parameters;
    this.writer = writer;
    this.templateResource = templateResource;
    this.templateUrl = templateUrl;
    this.templateContent = templateContent;
    this.directives = directives;

    StringResourceRepository repo = StringResourceLoader.getRepository();
    try {
        repo.putStringResource(TEMPLATE, templateContent != null ? templateContent : (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);
    this.context.getVelocityContext().put(PARAMETERS, parameters);
}
 
Example #2
Source File: IndentationFixer.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Object visit(ASTDirective node, Object data)
{
    String prefix = node.getPrefix();
    if (prefix.length() > 0)
    {
        if (extraIndentation == null)
        {
            fillExtraIndentation(prefix);
        }
        if (extraIndentation.length() > 0)
        {
            Matcher matcher = fix.matcher(prefix);
            node.setPrefix(matcher.replaceAll(parentIndentation));
            if (node.getDirectiveType() == Directive.BLOCK)
            {
                node.childrenAccept(this, null);
            }
        }
    }
    return null;
}
 
Example #3
Source File: CodeGenerator.java    From sundrio with Apache License 2.0 4 votes vote down vote up
public CodeGenerator(M model, String[] parameters, Writer writer, URL templateUrl, String templateResource, Set<Class<? extends Directive>> directives, String templateContent) {
    this(new CodeGeneratorContext(), model, parameters, writer, templateUrl, templateResource, templateContent, directives);
}
 
Example #4
Source File: CodeGenerator.java    From sundrio with Apache License 2.0 4 votes vote down vote up
public Set<Class<? extends Directive>> getDirectives() {
    return directives;
}
 
Example #5
Source File: VelocimacroFactory.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 *  actual factory: creates a Directive that will
 *  behave correctly wrt getting the framework to
 *  dig out the correct # of args
 * @param vmName Name of the Macro.
 * @param renderingTemplate destination template
 * @param sourceTemplate Source template from which the macro should be loaded.
 * @return A directive representing the Macro.
 */
 public Directive getVelocimacro(String vmName, Template renderingTemplate, Template sourceTemplate)
 {
    VelocimacroProxy vp = null;

    vp = vmManager.get(vmName, renderingTemplate, sourceTemplate);

    /*
     * if this exists, and autoload is on, we need to check where this VM came from
     */

    if (vp != null && autoReloadLibrary )
    {
        synchronized (this)
        {
            /*
             * see if this VM came from a library. Need to pass sourceTemplate in the event
             * namespaces are set, as it could be masked by local
             */

            String lib = vmManager.getLibraryName(vmName, sourceTemplate);

            if (lib != null)
            {
                try
                {
                    /*
                     * get the template from our map
                     */

                    Twonk tw = (Twonk) libModMap.get(lib);

                    if (tw != null)
                    {
                        Template template = tw.template;

                        /*
                         * now, compare the last modified time of the resource with the last
                         * modified time of the template if the file has changed, then reload.
                         * Otherwise, we should be ok.
                         */

                        long tt = tw.modificationTime;
                        long ft = template.getResourceLoader().getLastModified(template);

                        if (ft > tt)
                        {
                            log.debug("auto-reloading VMs from VM library: {}", lib);

                            /*
                             * when there are VMs in a library that invoke each other, there are
                             * calls into getVelocimacro() from the init() process of the VM
                             * directive. To stop the infinite loop we save the current time
                             * reported by the resource loader and then be honest when the
                             * reload is complete
                             */

                            tw.modificationTime = ft;

                            template = rsvc.getTemplate(lib);

                            /*
                             * and now we be honest
                             */

                            tw.template = template;
                            tw.modificationTime = template.getLastModified();

                            /*
                             * note that we don't need to put this twonk
                             * back into the map, as we can just use the
                             * same reference and this block is synchronized
                             */
                        }
                    }
                }
                catch (Exception e)
                {
                    String msg = "Velocimacro: Error using VM library: " + lib;
                    log.error(msg, e);
                    throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
                }

                vp = vmManager.get(vmName, sourceTemplate, renderingTemplate);
            }
        }
    }

    return vp;
}
 
Example #6
Source File: RuntimeInstance.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Programatically add a directive.
 * @param directive
 */
public synchronized void addDirective(Directive directive)
{
    runtimeDirectives.put(directive.getName(), directive);
    updateSharedDirectivesMap();
}
 
Example #7
Source File: ExceptionGeneratingDirective.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public int getType()
{
    return Directive.BLOCK;
}
 
Example #8
Source File: RuntimeSingleton.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the appropriate VelocimacroProxy object if strVMname
 * is a valid current Velocimacro.
 *
 * @param vmName  Name of velocimacro requested
 * @param renderingTemplate Template we are currently rendering. This
 *    information is needed when VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL setting is true
 *    and template contains a macro with the same name as the global macro library.
 * @param template current template
 *
 * @return VelocimacroProxy
 */
public static Directive getVelocimacro(String vmName, Template renderingTemplate, Template template)
{
    return ri.getVelocimacro(vmName, renderingTemplate, template);
}
 
Example #9
Source File: RuntimeServices.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the appropriate VelocimacroProxy object if strVMname
 * is a valid current Velocimacro.
 *
 * @param vmName  Name of velocimacro requested
 * @param renderingTemplate Template we are currently rendering. This
 *    information is needed when VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL setting is true
 *    and template contains a macro with the same name as the global macro library.
 * @param template current template
 *
 * @return VelocimacroProxy
 */
Directive getVelocimacro(String vmName, Template renderingTemplate, Template template);
 
Example #10
Source File: RuntimeServices.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve a previously instantiated directive.
 * @param name name of the directive
 * @return the directive with that name, if any
 * @since 1.6
 */
Directive getDirective(String name);
 
Example #11
Source File: RuntimeInstance.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve a previously instantiated directive.
 * @param name name of the directive
 * @return the {@link Directive} for that name
 */
public Directive getDirective(String name)
{
    return (Directive) runtimeDirectivesShared.get(name);
}
 
Example #12
Source File: RuntimeInstance.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the appropriate VelocimacroProxy object if vmName
 * is a valid current Velocimacro.
 *
 * @param vmName  Name of velocimacro requested
 * @param renderingTemplate Template we are currently rendering. This
 *    information is needed when VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL setting is true
 *    and template contains a macro with the same name as the global macro library.
 * @param template Template which acts as the host for the macro
 *
 * @return VelocimacroProxy
 */
public Directive getVelocimacro(String vmName, Template renderingTemplate, Template template)
{
    return vmFactory.getVelocimacro(vmName, renderingTemplate, template);
}
 
Example #13
Source File: StringUtils.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a string that formats the template filename with line number
 * and column of the given Directive. We use this routine to provide a consistent format for displaying
 * file errors.
 * @param directive currrent directive
 * @return formatted string
 */
public static String formatFileString(Directive directive)
{
    return formatFileString(directive.getTemplateName(), directive.getLine(), directive.getColumn());
}
 
Example #14
Source File: Parser.java    From velocity-engine with Apache License 2.0 votes vote down vote up
Directive getDirective(String directive);