public Object init()

in velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java [78:173]


    public Object init(InternalContextAdapter context, Object data)
            throws TemplateInitException
    {
        /*
         * simple habit... we prollie don't have an AST beneath us
         */

        super.init(context, data);

        /*
         * the stringlit is set at template parse time, so we can do this here
         * for now. if things change and we can somehow create stringlits at
         * runtime, this must move to the runtime execution path
         *
         * so, only if interpolation is turned on AND it starts with a " AND it
         * has a directive or reference, then we can interpolate. Otherwise,
         * don't bother.
         */

        interpolate = rsvc.getBoolean(
                RuntimeConstants.INTERPOLATE_STRINGLITERALS, true)
                && getFirstToken().image.startsWith("\"")
                && ((getFirstToken().image.indexOf(rsvc.getParserConfiguration().getDollarChar()) != -1) || (getFirstToken().image
                        .indexOf(rsvc.getParserConfiguration().getHashChar()) != -1));

        /*
         * get the contents of the string, minus the '/" at each end
         */
        String img = getFirstToken().image;

        /*
          the literal string *should* contain enclosing quotes
         */
        literal = img;

        image = img.substring(1, img.length() - 1);

        if (img.startsWith("\""))
        {
            image = unescape(image);
        }
        if (img.charAt(0) == '"' || img.charAt(0) == '\'' )
        {
            // replace double-double quotes like "" with a single double quote "
            // replace double single quotes '' with a single quote '
            image = replaceQuotes(image, img.charAt(0));
        }

        if (interpolate)
        {
            /*
             * parse and init the nodeTree
             */
            StringReader br = new StringReader(image);

            /*
             * it's possible to not have an initialization context - or we don't
             * want to trust the caller - so have a fallback value if so
             *
             * Also, do *not* dump the VM namespace for this template
             */

            Template template = null;
            if (context != null)
            {
                template = (Template)context.getCurrentResource();
            }
            if (template == null)
            {
                template = new Template();
                template.setName("StringLiteral");
            }
            try
            {
                nodeTree = rsvc.parse(br, template);
            }
            catch (ParseException e)
            {
                String msg = "Failed to parse String literal at "+
                    StringUtils.formatFileString(template.getName(), getLine(), getColumn());
                throw new TemplateInitException(msg, e, rsvc.getLogContext().getStackTrace(), template.getName(), getColumn(), getLine());
            }

            adjTokenLineNums(nodeTree);

            /*
             * init with context. It won't modify anything
             */

            nodeTree.init(context, rsvc);
        }

        cleanupParserAndTokens();

        return data;
    }