Java XML Attribute Add addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element, String attributeName)

Here you can find the source of addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element, String attributeName)

Description

Convenience method delegating to #addProperty(BeanDefinitionBuilder,String,String,String,boolean,boolean) .

License

Apache License

Declaration

public static void addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element,
        String attributeName) 

Method Source Code

//package com.java2s;
/*/*  www .j a  v  a2 s  .  co  m*/
 * Copyright 2013-2016 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.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

public class Main {
    /**
     * Convenience method delegating to
     * {@link #addProperty(BeanDefinitionBuilder, String, String, String, boolean, boolean)}.
     */
    public static void addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName,
            Attr attribute) {

        addProperty(builder, propertyName, attribute.getValue(), null, true, false);
    }

    /**
     * Convenience method delegating to
     * {@link #addProperty(BeanDefinitionBuilder, String, String, String, boolean, boolean)}.
     */
    public static void addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element,
            String attributeName) {

        addProperty(builder, propertyName, element.getAttribute(attributeName), null, true, false);
    }

    /**
     * Convenience method delegating to
     * {@link #addProperty(BeanDefinitionBuilder, String, String, String, boolean, boolean)}.
     */
    public static void addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, String value) {

        addProperty(builder, propertyName, value, null, true, false);
    }

    /**
     * Adds the named property and value, or reference to the given {@link BeanDefinitionBuilder} with an optional
     * default value if the value has not been specified.
     * <p/>
     * If <code>required</code> is <code>false</code>, <code>value</code> is null or empty,
     * and <code>defaultValue</code> is null or empty, then no property is added to the bean definition
     * and this method silently returns.
     *
     * @param builder {@link BeanDefinitionBuilder} used to build the {@link BeanDefinition}.
     * @param propertyName name of the property to add.
     * @param value value for the property being added.
     * @param defaultValue default value for the property if value is null or empty.
     * @param required If <code>true</code>, then <code>value</code> must not be null or empty. If <code>false</code>,
     * then <code>value</code> may be null and the <code>defaultValue</code> will be used. If <code>required</code> is
     * <code>false</code>, <code>value</code> is null or empty, and <code>defaultValue</code> is null or empty,
     * then no property is added to the bean definition and this method silently returns.
     * @param reference If <code>true</code>, then the <code>value</code>value for the named property is
     * considered a reference to another bean in the Spring context.
     * @return the given {@link BeanDefinitionBuilder}.
     * @throws IllegalArgumentException if either the {@link BeanDefinitionBuilder} is null
     * or the <code>propertyName</code> has not been specified.
     * @see BeanDefinitionBuilder#addPropertyReference(String, String)
     * @see BeanDefinitionBuilder#addPropertyValue(String, Object)
     */
    public static BeanDefinitionBuilder addProperty(BeanDefinitionBuilder builder, String propertyName,
            String value, String defaultValue, boolean required, boolean reference) {

        Assert.notNull(builder, "BeanDefinitionBuilder must not be null");
        Assert.hasText(propertyName, "Property name must not be null");

        if (!StringUtils.hasText(value)) {
            if (required) {
                throw new IllegalArgumentException(String.format(
                        "value required for property %1$s[%2$s] on class [%3$s]", reference ? "reference " : "",
                        propertyName, builder.getRawBeanDefinition().getBeanClassName()));
            } else {
                value = defaultValue;
            }
        }

        if (StringUtils.hasText(value)) {
            if (reference) {
                builder.addPropertyReference(propertyName, value);
            } else {
                builder.addPropertyValue(propertyName, value);
            }
        }

        return builder;
    }
}

Related

  1. addElementAndAttributes(Node parent, String name, String[] atts)
  2. addNewElementWithAttribute(Node node, String elementName, String elementValue, String attrName, String attrValue)
  3. addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName, Attr attribute, String defaultValue)
  4. addOrUpdateAttribute(Element element, String name, String value)
  5. addPropertyReferenceIfNeeded(BeanDefinitionBuilder bdb, Element element, String attributeName)
  6. addTextElement(Node parent, String name, String value, Attr[] attrs)
  7. addTypeAttribute(Element element, String type)
  8. readDoubleAttr(Element element, String attributeName, double defaultValue)