Java Code Examples for org.apache.velocity.context.InternalContextAdapter#put()

The following examples show how to use org.apache.velocity.context.InternalContextAdapter#put() . 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: Foreach.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
protected void clean(InternalContextAdapter context, Object o)
{
    /*
     *  restores element key if exists
     *  otherwise just removes
     */
    if (o != null)
    {
        context.put(elementKey, o);
    }
    else
    {
        context.remove(elementKey);
    }

    // clean up after the ForeachScope
    postRender(context);
}
 
Example 2
Source File: For.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
  throws IOException
{
  Object c = context.get(counterName);
  context.put(counterName, counterInitialValue);
  try
  {
    return super.render(context, writer, node);
  }
  finally
  {
    if (c != null)
    {
      context.put(counterName, c);
    }
    else
    {
      context.remove(counterName);
    }
  }
}
 
Example 3
Source File: XPathDirective.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node ) throws IOException {
    String variableName = node.jjtGetChild(0).getFirstToken().image.substring(1);
    String xmlDocument = String.valueOf(node.jjtGetChild(1).value(context));
    String xpathString = String.valueOf(node.jjtGetChild(2).value(context));

    XPath xpath = XPathFactory.newInstance().newXPath();
    try {
        String evaluatedValue = xpath.evaluate(xpathString, new InputSource(new StringReader(xmlDocument)));
        context.put(variableName, evaluatedValue);
    } catch (XPathExpressionException e) {
        throw new IOException("cannot evaluate xpath: " + e.getMessage(), e);
    }
    return true;
}
 
Example 4
Source File: Directive.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * This creates and places the scope control for this directive
 * into the context (if scope provision is turned on).
 * @param context
 */
protected void preRender(InternalContextAdapter context)
{
    if (isScopeProvided())
    {
        String name = getScopeName();
        Object previous = context.get(name);
        context.put(name, makeScope(previous));
    }
}
 
Example 5
Source File: Directive.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * This cleans up any scope control for this directive after rendering,
 * assuming the scope control was turned on.
 * @param context
 */
protected void postRender(InternalContextAdapter context)
{
    if (isScopeProvided())
    {
        String name = getScopeName();
        Object obj = context.get(name);

        try
        {
            Scope scope = (Scope)obj;
            if (scope.getParent() != null)
            {
                context.put(name, scope.getParent());
            }
            else if (scope.getReplaced() != null)
            {
                context.put(name, scope.getReplaced());
            }
            else
            {
                context.remove(name);
            }
        }
        catch (ClassCastException cce)
        {
            // the user can override the scope with a #set,
            // since that means they don't care about a replaced value
            // and obviously aren't too keen on their scope control,
            // and especially since #set is meant to be handled globally,
            // we'll assume they know what they're doing and not worry
            // about replacing anything superseded by this directive's scope
        }
    }
}
 
Example 6
Source File: For.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void renderBlock(InternalContextAdapter context, Writer writer, Node node)
  throws IOException
{
  Object count = context.get(counterName);
  if (count instanceof Number)
  {
    context.put(counterName, ((Number)count).intValue() + 1);
  }
  super.renderBlock(context, writer, node);
}
 
Example 7
Source File: Define.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * directive.render() simply makes an instance of the Block inner class
 * and places it into the context as indicated.
 */
public boolean render(InternalContextAdapter context, Writer writer, Node node)
{
    /* put a Block.Reference instance into the context,
     * using the user-defined key, for later inline rendering.
     */
    context.put(key, new Reference(context, this));
    return true;
}
 
Example 8
Source File: Foreach.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 *  renders the #foreach() block
 * @param context
 * @param writer
 * @param node
 * @return True if the directive rendered successfully.
 * @throws IOException
 */
public boolean render(InternalContextAdapter context, Writer writer, Node node)
    throws IOException
{
    // Get the block ast tree which is always the last child ...
    Node block = node.jjtGetChild(node.jjtGetNumChildren()-1);

    // ... except if there is an #else claude
    Node elseBlock = null;
    Node previous = node.jjtGetChild(node.jjtGetNumChildren()-2);
    if (previous instanceof ASTBlock)
    {
        elseBlock = block;
        block = previous;
    }

    Node iterableNode = node.jjtGetChild(2);
    Object iterable = iterableNode.value(context);
    Iterator i = getIterator(iterable, iterableNode);
    if (i == null || !i.hasNext())
    {
        if (elseBlock != null)
        {
            renderBlock(context, writer, elseBlock);
        }
        return false;
    }

    /*
     * save the element key if there is one
     */
    Object o = context.get(elementKey);

    /*
     * roll our own scope class instead of using preRender(ctx)'s
     */
    ForeachScope foreach = null;
    if (isScopeProvided())
    {
        String name = getScopeName();
        foreach = new ForeachScope(this, context.get(name));
        context.put(name, foreach);
    }

    int count = 1;
    while (count <= maxNbrLoops && i.hasNext())
    {
        count++;

        put(context, elementKey, i.next());
        if (isScopeProvided())
        {
            // update the scope control
            foreach.index++;
            foreach.hasNext = i.hasNext();
        }

        try
        {
            renderBlock(context, writer, block);
        }
        catch (StopCommand stop)
        {
            if (stop.isFor(this))
            {
                break;
            }
            else
            {
                // clean up first
                clean(context, o);
                throw stop;
            }
        }
    }
    clean(context, o);
    /*
     * closes the iterator if it implements the Closeable interface
     */
    if (i != null && i instanceof Closeable && i != iterable) /* except if the iterable is the iterator itself */
    {
        ((Closeable)i).close();
    }
    return true;
}
 
Example 9
Source File: Foreach.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Extension hook to allow subclasses to control whether loop vars
 * are set locally or not. So, those in favor of VELOCITY-285, can
 * make that happen easily by overriding this and having it use
 * context.localPut(k,v). See VELOCITY-630 for more on this.
 * @param context
 * @param key
 * @param value
 */
protected void put(InternalContextAdapter context, String key, Object value)
{
    context.put(key, value);
}