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

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

Description

map Required Bean Ref Attributes

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*/*w w w  . j ava 2 s  .c om*/
 * 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 mapRequiredBeanRefAttributes(Element element, ParserContext parserContext,
            BeanDefinitionBuilder builder, String... attrs) {
        for (String attr : attrs) {
            String value = element.getAttribute(attr);
            if (!validateRequiredAttribute(element, parserContext, attr)) {
                return;
            }
            String propertyName = Conventions.attributeNameToPropertyName(attr);
            if (validateProperty(element, parserContext, propertyName, attr)) {
                builder.addPropertyReference(propertyName, value);
            }
        }
    }

    private static boolean validateRequiredAttribute(Element element, ParserContext parserContext, String attr) {
        if (!StringUtils.hasText(attr)) {
            parserContext.getReaderContext().error("The '" + attr + "' attribute is required.",
                    parserContext.extractSource(element));
            return false;
        }
        return true;
    }

    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. mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder, ParserContext parserContext, String... attrs)
  2. parseAttributeValuePairTags(Node parentNode)
  3. parseBoolean(String xmlAttributeValue, Boolean invalidValue)
  4. parseConfigAttr(NamedNodeMap attributes)
  5. parseElementAttributes(Element element)