Java XML Attribute Add addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName, Attr attribute, String defaultValue)

Here you can find the source of addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName, Attr attribute, String defaultValue)

Description

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

License

Apache License

Declaration

public static void addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName,
        Attr attribute, String defaultValue) 

Method Source Code

//package com.java2s;
/*/*from  www .j  a v  a  2 s. c  om*/
 * 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 addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName,
            Attr attribute) {

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

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

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

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

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

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

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

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

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

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

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

    /**
     * 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. addDomAttr(Document doc, String tagName, String tagNamespaceURI, String attrName, String attrValue)
  2. addDoubleAttributeAsInteger(Element element, String attName, double value)
  3. addElement(Node parent, String name, Attr[] attrs)
  4. addElementAndAttributes(Node parent, String name, String[] atts)
  5. addNewElementWithAttribute(Node node, String elementName, String elementValue, String attrName, String attrValue)
  6. addOrUpdateAttribute(Element element, String name, String value)
  7. addPropertyReferenceIfNeeded(BeanDefinitionBuilder bdb, Element element, String attributeName)
  8. addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element, String attributeName)
  9. addTextElement(Node parent, String name, String value, Attr[] attrs)