<commit_before>//* Generated By:JJTree: Do not edit this line. ASTReference.java */

package org.apache.velocity.runtime.parser;

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

import java.util.Map;

import org.apache.velocity.Context;
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.util.ClassUtils;

public class ASTReference extends SimpleNode
{
    private String nullString;
    private Object root;
    private Object value;

    public ASTReference(int id)
    {
        super(id);
    }

    public ASTReference(Parser p, int id)
    {
        super(p, id);
    }

    /** Accept the visitor. **/
    public Object jjtAccept(ParserVisitor visitor, Object data)
    {
        return visitor.visit(this, data);
    }

    public Object init(Context context, Object data) throws Exception
    {
        root = getVariableValue(context, getRoot());
        
        // An object has to be in the context for
        // subsequent introspection.

        if (root == null) return null;
        
        Class clazz = root.getClass();
        
        // All children here are either Identifier() nodes
        // or Method() nodes.
        
        int children = jjtGetNumChildren();
        for (int i = 0; i < children; i++)
            clazz = (Class) jjtGetChild(i).init(context, clazz);
    
        return data;
    }        
    
    public Object execute(Object o, Context context)
    {
        Object result = getVariableValue(context, getRoot());
        
        if (result == null)
            return null;
        
        int children = jjtGetNumChildren();
        
        for (int i = 0; i < children; i++)
            result = jjtGetChild(i).execute(result,context);
    
        return result;
    }

    public void render(Context context, Writer writer)
        throws IOException
    {
        value = execute(null, context);
        
        if (value == null)
            writer.write(NodeUtils
                .specialText(getFirstToken()) + 
                    nullString);
        else
            writer.write(NodeUtils
                .specialText(getFirstToken()) + 
                    value(context).toString());
    }

    public boolean evaluate(Context context)
    {
        value = execute(null, context);
        
        if (value == null)
            return false;
        else if (value instanceof Boolean)
        {
            if (((Boolean) value).booleanValue())
                return true;
            else
                return false;
        }
        else
            return true;
    }

    public Object value(Context context)
    {
        return execute(null, context);
    }

    public void setValue(Context context, Object value)
    {
        // The rootOfIntrospection is the object we will
        // retrieve from the Context. This is the base
        // object we will apply reflection to.
        String root = getRoot();
        Object result = getVariableValue(context, root);
        
        if (result == null)
        {
            Runtime.error("Reference error: " + root + " " +
                          "not defined in the context.");
        }                          
        
        // How many child nodes do we have?
        int children = jjtGetNumChildren();
        
        for (int i = 0; i < children - 1; i++)
            result = jjtGetChild(i).execute(result, context);

        Object[] args = { value };
        ClassUtils.invoke(result, "set" + jjtGetChild(children - 1)
            .getFirstToken().image, args);
    }

    private String getRoot()
    {
        Token t = getFirstToken();
        
        if (t.image.equals("$!"))
        {
            nullString = "";
            
            if (t.next.image.equals("{"))
                // $!{provider.Title} 
                return t.next.next.image;
            else
                // $!provider.Title
                return t.next.image;
        }
        else if (t.image.equals("${"))
        {
            // ${provider.Title}
            nullString = literal();
            return t.next.image;
        }            
        else
        {
            // $provider.Title
            nullString = literal();
            return t.image.substring(1);
        }            
    }

    /**
     * Return the literal string representation
     * of a reference. Used when a reference has
     * a null value.
     */
    public String literal()
    {
        Token t = getFirstToken();
        StringBuffer sb = new StringBuffer(t.image);
        
        while(t.next != null && t.next.last == false)
        {
            t = t.next;
            sb.append(t.image);
        }
        sb.append(getLastToken().image);
        
        return sb.toString();
    }

    public Object getVariableValue(Context context, String variable)
    {
        if (context.containsKey(variable))
            return context.get(variable);<commit_msg>- fixed a formatting error when a shorthand variable reference   evaluated to null i.e. when $foo was null. PR: Obtained from: Submitted by: Reviewed by:<commit_after>//* Generated By:JJTree: Do not edit this line. ASTReference.java */

package org.apache.velocity.runtime.parser;

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

import java.util.Map;

import org.apache.velocity.Context;
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.util.ClassUtils;

public class ASTReference extends SimpleNode
{
    private String nullString;
    private Object root;
    private Object value;

    public ASTReference(int id)
    {
        super(id);
    }

    public ASTReference(Parser p, int id)
    {
        super(p, id);
    }

    /** Accept the visitor. **/
    public Object jjtAccept(ParserVisitor visitor, Object data)
    {
        return visitor.visit(this, data);
    }

    public Object init(Context context, Object data) throws Exception
    {
        root = getVariableValue(context, getRoot());
        
        // An object has to be in the context for
        // subsequent introspection.

        if (root == null) return null;
        
        Class clazz = root.getClass();
        
        // All children here are either Identifier() nodes
        // or Method() nodes.
        
        int children = jjtGetNumChildren();
        for (int i = 0; i < children; i++)
            clazz = (Class) jjtGetChild(i).init(context, clazz);
    
        return data;
    }        
    
    public Object execute(Object o, Context context)
    {
        Object result = getVariableValue(context, getRoot());
        
        if (result == null)
            return null;
        
        int children = jjtGetNumChildren();
        
        for (int i = 0; i < children; i++)
            result = jjtGetChild(i).execute(result,context);
    
        return result;
    }

    public void render(Context context, Writer writer)
        throws IOException
    {
        value = execute(null, context);
        
        if (value == null)
            writer.write(NodeUtils
                .specialText(getFirstToken()) + 
                    nullString);
        else
            writer.write(NodeUtils
                .specialText(getFirstToken()) + 
                    value(context).toString());
    }

    public boolean evaluate(Context context)
    {
        value = execute(null, context);
        
        if (value == null)
            return false;
        else if (value instanceof Boolean)
        {
            if (((Boolean) value).booleanValue())
                return true;
            else
                return false;
        }
        else
            return true;
    }

    public Object value(Context context)
    {
        return execute(null, context);
    }

    public void setValue(Context context, Object value)
    {
        // The rootOfIntrospection is the object we will
        // retrieve from the Context. This is the base
        // object we will apply reflection to.
        String root = getRoot();
        Object result = getVariableValue(context, root);
        
        if (result == null)
        {
            Runtime.error("Reference error: " + root + " " +
                          "not defined in the context.");
        }                          
        
        // How many child nodes do we have?
        int children = jjtGetNumChildren();
        
        for (int i = 0; i < children - 1; i++)
            result = jjtGetChild(i).execute(result, context);

        Object[] args = { value };
        ClassUtils.invoke(result, "set" + jjtGetChild(children - 1)
            .getFirstToken().image, args);
    }

    private String getRoot()
    {
        Token t = getFirstToken();
        
        if (t.image.equals("$!"))
        {
            nullString = "";
            
            if (t.next.image.equals("{"))
                // $!{provider.Title} 
                return t.next.next.image;
            else
                // $!provider.Title
                return t.next.image;
        }
        else if (t.image.equals("${"))
        {
            // ${provider.Title}
            nullString = literal();
            return t.next.image;
        }            
        else
        {
            // $provider.Title
            nullString = literal();
            return t.image.substring(1);
        }            
    }

    /**
     * Return the literal string representation
     * of a reference. Used when a reference has
     * a null value.
     *
     * For a variable reference like $foo this isn't
     * working. hmmm.
     */
    public String literal()
    {
        Token t = getFirstToken();
        StringBuffer sb = new StringBuffer(t.image);
        
        // Check to see if there are any children. If
        // there aren't then we can return with the first
        // token becasue it's a shorthand variable reference
        // like $foo.
        if (children == null)
            return sb.toString();
        
        while(t.next != null && t.next.last == false)
        {
            t = t.next;
            sb.append(t.image);
        }
        sb.append(getLastToken().image);
        
        return sb.toString();
    }

    public Object getVariableValue(Context context, String variable)
    {
        if (context.containsKey(variable))
            return context.get(variable);
{'fraction_non_alphanumeric': 0.10054190052254693, 'fraction_numerical': 0.0011612154054577125, 'mean_word_length': 1.5355740922473013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4739035', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2658, 'n_words': 773}