net.hasor.rsf.spring.parser.RsfDefinitionParser.java Source code

Java tutorial

Introduction

Here is the source code for net.hasor.rsf.spring.parser.RsfDefinitionParser.java

Source

/*
 * Copyright 2008-2009 the original author or authors.
 *
 * 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 net.hasor.rsf.spring.parser;

import net.hasor.rsf.utils.StringUtils;
import net.hasor.rsf.InterAddress;
import net.hasor.rsf.spring.RsfAddressPropertyEditor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.SpringVersion;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @version : 2016-11-08
 * @author (zyc@hasor.net)
 */
public class RsfDefinitionParser implements BeanDefinitionParser {
    /** ? */
    protected String revertProperty(NamedNodeMap attributes, String attName) {
        Node attNode = attributes.getNamedItem(attName);
        return (attNode != null) ? attNode.getNodeValue() : null;
    }

    /** ?Xml  */
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        // Spring 
        String version = SpringVersion.getVersion();
        version = StringUtils.isBlank(version) ? "?" : version;
        Map customEditors = null;
        if (version.charAt(0) == '4' || version.charAt(0) == '5') {
            customEditors = new HashMap<Class<?>, Class<? extends java.beans.PropertyEditor>>();
            customEditors.put(InterAddress.class, RsfAddressPropertyEditor.class);
        } else {
            customEditors = new HashMap();
            customEditors.put("net.hasor.rsf.InterAddress", new RsfAddressPropertyEditor());
        }
        //
        // . Bean 
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
        builder.getRawBeanDefinition().setBeanClass(CustomEditorConfigurer.class);
        builder.setScope(BeanDefinition.SCOPE_SINGLETON);//?
        builder.addPropertyValue("customEditors", customEditors);
        //
        //  .,BeanID net.hasor.rsf.spring.RsfAddressPropertyEditor
        AbstractBeanDefinition propEditors = builder.getBeanDefinition();
        String beanID = RsfAddressPropertyEditor.class.getName();
        BeanDefinitionHolder holder = new BeanDefinitionHolder(propEditors, beanID);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
        parserContext.registerComponent(new BeanComponentDefinition(holder));
        //
        //
        NamedNodeMap attributes = element.getAttributes();
        //
        return null;
    }
}