public Object init()

in velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java [155:266]


    public Object init(InternalContextAdapter context, Object data)
    throws TemplateInitException
    {
        super.init(context, data);

        strictEscape = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, false);
        strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
        lookupAlternateLiteral = rsvc.getBoolean(RuntimeConstants.VM_ENABLE_BC_MODE, false);

        /*
         *  the only thing we can do in init() is getRoot()
         *  as that is template based, not context based,
         *  so it's thread- and context-safe
         */

        rootString = rsvc.useStringInterning() ? getRoot().intern() : getRoot();
        if (lookupAlternateLiteral)
        {
            /* cache alternate null tring key */
            alternateNullStringKey = ".literal." + nullString;
        }

        numChildren = jjtGetNumChildren();

        // This is an expensive call, so get it now.
        literal();

        /*
         * and if appropriate...
         */
        if (numChildren > 0 )
        {
            Node lastNode = jjtGetChild(numChildren-1);
            if (lastNode instanceof ASTIndex)
            {
                /*
                 * only used in SetValue, where alternate value is forbidden
                 */
                astIndex = (ASTIndex) lastNode;
            }
            else if (lastNode instanceof ASTExpression)
            {
                astAlternateValue = (ASTExpression) lastNode;
                --numChildren;
            }
            else
            {
                identifier = lastNode.getFirstTokenImage();
            }
        }


        /*
         * make an uberinfo - saves new's later on
         */
        uberInfo = new Info(getTemplateName(), getLine(),getColumn());

        /*
         * track whether we log invalid references
         */
        logOnNull =
            rsvc.getBoolean(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);

        /*
         * whether to check for emptiness when evaluatingnumChildren
         */
        checkEmpty =
            rsvc.getBoolean(RuntimeConstants.CHECK_EMPTY_OBJECTS, true);

        /* invalid references special cases */

        warnInvalidQuietReferences =
            rsvc.getBoolean(RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES_QUIET, false);
        warnInvalidNullReferences =
            rsvc.getBoolean(RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES_NULL, false);
        warnInvalidTestedReferences =
            rsvc.getBoolean(RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES_TESTED, false);


        /*
         * In the case we are referencing a variable with #if($foo) or
         * #if( ! $foo) then we allow variables to be undefined and we
         * set strictRef to false so that if the variable is undefined
         * an exception is not thrown.
         */
        if (strictRef && numChildren == 0)
        {
            logOnNull = false; // Strict mode allows nulls

            Node node = this.jjtGetParent();
            if (node instanceof ASTNotNode     // #if( ! $foo)
             || node instanceof ASTExpression  // #if( $foo )
             || node instanceof ASTOrNode      // #if( $foo || ...
             || node instanceof ASTAndNode)    // #if( $foo && ...
            {
                // Now scan up tree to see if we are in an If statement
                while (node != null)
                {
                    if (node instanceof ASTIfStatement)
                    {
                       strictRef = false;
                       break;
                    }
                    node = node.jjtGetParent();
                }
            }
        }
        saveTokenImages();
        cleanupParserAndTokens();

        return data;
    }