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

The following examples show how to use org.apache.velocity.context.InternalContextAdapter#get() . 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: 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 2
Source File: MicroSqlReplace.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
		throws IOException, ResourceNotFoundException, ParseErrorException,
		MethodInvocationException {
	List placeList=(List) context.get("placeList");
	if(placeList==null){
		return true;
	}
	Object value=node.jjtGetChild(0).value(context);
	placeList.add(value);
	writer.write("?");
	return true;
}
 
Example 3
Source File: MicroSqlReplace.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
		throws IOException, ResourceNotFoundException, ParseErrorException,
		MethodInvocationException {
	List placeList=(List) context.get("placeList");
	if(placeList==null){
		return true;
	}
	Object value=node.jjtGetChild(0).value(context);
	placeList.add(value);
	writer.write("?");
	return true;
}
 
Example 4
Source File: ASTReference.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * This method helps to implement the "render literal if null" functionality.
 *
 * VelocimacroProxy saves references to macro arguments (AST nodes) so that if we have a macro
 * #foobar($a $b) then there is key "$a.literal" which points to the literal presentation of the
 * argument provided to variable $a. If the value of $a is null, we render the string that was
 * provided as the argument.
 *
 * @param context
 * @return
 */
private String getNullString(InternalContextAdapter context)
{
    String ret = nullString;

    if (lookupAlternateLiteral)
    {
        Deque<String> alternateLiteralsStack = (Deque<String>)context.get(alternateNullStringKey);
        if (alternateLiteralsStack != null && alternateLiteralsStack.size() > 0)
        {
            ret = alternateLiteralsStack.peekFirst();
        }
    }
    return ret;
}
 
Example 5
Source File: ASTReference.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @param context
 * @return The evaluated value of the variable.
 * @throws MethodInvocationException
 */
public Object getRootVariableValue(InternalContextAdapter context)
{
    Object obj = null;
    try
    {
        obj = context.get(rootString);
    }
    catch(RuntimeException e)
    {
        log.error("Exception calling reference ${} at {}",
                  rootString, StringUtils.formatFileString(uberInfo));
        throw e;
    }

    if (obj == null && strictRef && astAlternateValue == null)
    {
      if (!context.containsKey(rootString))
      {
          log.error("Variable ${} has not been set at {}",
                    rootString, StringUtils.formatFileString(uberInfo));
          throw new MethodInvocationException("Variable $" + rootString +
              " has not been set", null, rsvc.getLogContext().getStackTrace(), identifier,
              uberInfo.getTemplateName(), uberInfo.getLine(), uberInfo.getColumn());
      }
    }
    return obj;
}
 
Example 6
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 7
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 8
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 9
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;
}