net.phoenix.thrift.xml.ComplexBeanDefinitionParser.java Source code

Java tutorial

Introduction

Here is the source code for net.phoenix.thrift.xml.ComplexBeanDefinitionParser.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 net.phoenix.thrift.xml;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 *
 * @author Shamphone Lee<shamphone@gmail.com>
 *
 */
public class ComplexBeanDefinitionParser extends AbstractBeanDefinitionParser {
    @Override
    protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
        String parentName = getParentName(element);
        if (parentName != null) {
            builder.getRawBeanDefinition().setParentName(parentName);
        }
        Class<?> beanClass = getBeanClass(element);
        if (beanClass != null) {
            builder.getRawBeanDefinition().setBeanClass(beanClass);
        } else {
            String beanClassName = getBeanClassName(element);
            if (beanClassName != null) {
                builder.getRawBeanDefinition().setBeanClassName(beanClassName);
            }
        }
        builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
        if (parserContext.isNested()) {
            // Inner bean definition must receive same scope as containing bean.
            builder.setScope(parserContext.getContainingBeanDefinition().getScope());
        }
        if (parserContext.isDefaultLazyInit()) {
            // Default-lazy-init applies to custom bean definitions as well.
            builder.setLazyInit(true);
        }
        preParse(element, parserContext, builder);
        AbstractBeanDefinition target = builder.getBeanDefinition();
        postParse(element, parserContext, target);
        return target;
    }

    /**
     * BeanDefinition??
     * @param element
     * @param parserContext
     * @param builder
     */
    protected void preParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    }

    /**
     *BeanDefinition?
     * @param element
     * @param parserContext
     * @param builder
     */
    protected void postParse(Element element, ParserContext parserContext, AbstractBeanDefinition current) {
    }

    /**
     * convert string to boolean value;
     *
     * @param value
     * @return
     */
    protected boolean parseBoolean(String value) {
        if (StringUtils.isEmpty(value))
            return false;
        return value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("true");
    }

    /**
     *
     * @param element
     * @return
     */
    protected String getBeanClassName(Element element) {
        return null;
    }

    /**
     * Determine the name for the parent of the currently parsed bean, in case
     * of the current bean being defined as a child bean.
     * <p>
     * The default implementation returns {@code null}, indicating a root bean
     * definition.
     *
     * @param element
     *            the {@code Element} that is being parsed
     * @return the name of the parent bean for the currently parsed bean, or
     *         {@code null} if none
     */
    protected String getParentName(Element element) {
        return null;
    }

    /**
     * Determine the bean class corresponding to the supplied {@link Element}.
     * <p>Note that, for application classes, it is generally preferable to
     * override {@link #getBeanClassName} instead, in order to avoid a direct
     * dependence on the bean implementation class. The BeanDefinitionParser
     * and its NamespaceHandler can be used within an IDE plugin then, even
     * if the application classes are not available on the plugin's classpath.
     * @param element the {@code Element} that is being parsed
     * @return the {@link Class} of the bean that is being defined via parsing
     * the supplied {@code Element}, or {@code null} if none
     * @see #getBeanClassName
     */
    protected Class<?> getBeanClass(Element element) {
        return null;
    }

}