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

pl.chilldev.web.spring.config.TextFormatterBeanDefinitionParser Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
/**
 * This file is part of the ChillDev-Web.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2015 © by Rafał Wrzeszcz - Wrzasq.pl.
 */

package pl.chilldev.web.spring.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;

import org.springframework.util.xml.DomUtils;

import org.w3c.dom.Element;

/**
 * `<text-formatter>` element parser.
 */
public class TextFormatterBeanDefinitionParser
    implements
        BeanDefinitionParser
{
    /**
     * Text formatters property name.
     */
    public static final String PROPERTY_FORMATTERS = "formatters";

    /**
     * `<bean>` element name.
     */
    public static final String ELEMENT_BEAN = "bean";

    /**
     * `format=""` attribute name.
     */
    public static final String ATTRIBUTE_FORMAT = "format";

    /**
     * Logger.
     */
    private Logger logger = LoggerFactory.getLogger(TextFormatterBeanDefinitionParser.class);

    /**
     * Text format handlers map.
     */
    private ManagedMap formatters = new ManagedMap<>();

    /**
     * Initializes bean parser.
     *
     * @param formatterFactoryBean Formatter factory.
     */
    public TextFormatterBeanDefinitionParser(BeanDefinition formatterFactoryBean)
    {
        formatterFactoryBean.getPropertyValues().addPropertyValue(
            TextFormatterBeanDefinitionParser.PROPERTY_FORMATTERS,
            this.formatters
        );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext)
    {
        String format = element.getAttribute(TextFormatterBeanDefinitionParser.ATTRIBUTE_FORMAT);

        Element bean = DomUtils.getChildElementByTagName(element, TextFormatterBeanDefinitionParser.ELEMENT_BEAN);
        this.logger.info("Setting format handler for \"{}\".", format);
        this.formatters.put(
            format,
            parserContext.getDelegate().parseBeanDefinitionElement(bean).getBeanDefinition()
        );

        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy