All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.template.velocity.impl.parser.ASTStringLiteralEnhanced Maven / Gradle / Ivy

The newest version!
package org.zodiac.template.velocity.impl.parser;

import java.lang.reflect.Field;
import java.util.List;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.template.velocity.support.InterpolationUtil;

public class ASTStringLiteralEnhanced extends ASTStringLiteral {

    private static final Field[] FIELDS;

    private boolean interpolate;

    static {
        List fieldList = CollUtil.linkedList();

        for (Class c = ASTStringLiteral.class; c != null && c != Object.class; c = c.getSuperclass()) {
            Field[] fields = c.getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);
                fieldList.add(field);
            }
        }

        FIELDS = fieldList.toArray(new Field[fieldList.size()]);
    }

    public ASTStringLiteralEnhanced(ASTStringLiteral src) {
        super(-1);

        for (Field field : FIELDS) {
            try {
                Object value = field.get(src);

                if ("interpolate".equals(field.getName()) && value instanceof Boolean) {
                    interpolate = (Boolean)value;
                }

                field.set(this, value);
            } catch (Exception e) {
                throw new RuntimeException("Could not copy ASTStringLiteral", e);
            }
        }
    }

    @Override
    public Object value(InternalContextAdapter context) {
        if (interpolate) {
            Object savedInterpolate = context.put(InterpolationUtil.INTERPOLATE_KEY, Boolean.TRUE);

            try {
                return super.value(context);
            } finally {
                if (savedInterpolate == null) {
                    context.remove(InterpolationUtil.INTERPOLATE_KEY);
                } else {
                    context.put(InterpolationUtil.INTERPOLATE_KEY, savedInterpolate);
                }
            }
        }

        return super.value(context);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy