cherry.foundation.code.CodeTagSupport.java Source code

Java tutorial

Introduction

Here is the source code for cherry.foundation.code.CodeTagSupport.java

Source

/*
 * Copyright 2015 agwlvssainokuni
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cherry.foundation.code;

import javax.el.VariableMapper;
import javax.servlet.jsp.PageContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

@SuppressWarnings("serial")
public abstract class CodeTagSupport<T> extends RequestContextAwareTag {

    private String var;

    private String beanName;

    private String codeName;

    private String value;

    private Boolean plainLabel = Boolean.TRUE;

    public void setVar(String var) {
        this.var = var;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setPlainLabel(Boolean plainLabel) {
        this.plainLabel = plainLabel;
    }

    @Override
    protected int doStartTagInternal() throws Exception {
        CodeManager codeManager = getCodeManager();
        T object = getObject(codeManager, codeName, value, plainLabel);
        if (StringUtils.isNotEmpty(var)) {
            VariableMapper vm = pageContext.getELContext().getVariableMapper();
            if (vm != null) {
                vm.setVariable(var, null);
            }
            pageContext.setAttribute(var, object, PageContext.PAGE_SCOPE);
        }
        if (doesEvalBody(codeManager, codeName, value, plainLabel)) {
            return EVAL_BODY_INCLUDE;
        } else {
            return SKIP_BODY;
        }
    }

    @Override
    public void release() {
        super.release();
        var = null;
        beanName = null;
        codeName = null;
        value = null;
        plainLabel = Boolean.TRUE;
    }

    private CodeManager getCodeManager() {
        if (StringUtils.isEmpty(beanName)) {
            return getRequestContext().getWebApplicationContext().getBean(CodeManager.class);
        } else {
            return getRequestContext().getWebApplicationContext().getBean(beanName, CodeManager.class);
        }
    }

    protected abstract T getObject(CodeManager codeManager, String codeName, String value, Boolean plainLabel);

    protected boolean doesEvalBody(CodeManager codeManager, String codeName, String value, Boolean plainLabel) {
        return false;
    }

}