Java XML Attribute Parse mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder, ParserContext parserContext, String... attrs)

Here you can find the source of mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder, ParserContext parserContext, String... attrs)

Description

map Optional Bean Ref Attributes

License

Apache License

Declaration

static void mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder,
            ParserContext parserContext, String... attrs) 

Method Source Code

//package com.java2s;
/*/*ww  w . ja  va2  s. c o  m*/
 * Copyright 2002-2011 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.
 */

import org.springframework.beans.factory.support.BeanDefinitionBuilder;

import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class Main {
    static void mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder,
            ParserContext parserContext, String... attrs) {
        for (String attr : attrs) {
            String value = element.getAttribute(attr);
            if (StringUtils.hasText(value)) {
                String propertyName = Conventions.attributeNameToPropertyName(attr);
                if (validateProperty(element, parserContext, propertyName, attr)) {
                    builder.addPropertyReference(propertyName, value);
                }
            }
        }
    }

    private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName,
            String attr) {
        if (!StringUtils.hasText(propertyName)) {
            parserContext.getReaderContext().error("Illegal property name trying to convert from attribute '" + attr
                    + "' : cannot be null or empty.", parserContext.extractSource(element));
            return false;
        }
        return true;
    }
}

Related

  1. mapRequiredBeanRefAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs)
  2. parseAttributeValuePairTags(Node parentNode)
  3. parseBoolean(String xmlAttributeValue, Boolean invalidValue)
  4. parseConfigAttr(NamedNodeMap attributes)