Example usage for org.springframework.beans.factory.support BeanDefinitionBuilder setParentName

List of usage examples for org.springframework.beans.factory.support BeanDefinitionBuilder setParentName

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support BeanDefinitionBuilder setParentName.

Prototype

public BeanDefinitionBuilder setParentName(String parentName) 

Source Link

Document

Set the name of the parent definition of this bean definition.

Usage

From source file:org.kuali.rice.krad.datadictionary.parse.CustomSchemaParser.java

/**
 * Parses the xml bean into a standard bean definition format and fills the information in the passed in definition
 * builder/*www .  j av a 2 s .c o  m*/
 *
 * @param element - The xml bean being parsed.
 * @param parserContext - Provided information and functionality regarding current bean set.
 * @param bean - A definition builder used to build a new spring bean from the information it is filled with.
 */
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder bean) {
    // Retrieve custom schema information build from the annotations
    Map<String, Map<String, BeanTagAttributeInfo>> attributeProperties = CustomTagAnnotations
            .getAttributeProperties();
    Map<String, BeanTagAttributeInfo> entries = attributeProperties.get(element.getLocalName());

    // Log error if there are no attributes found for the bean tag
    if (entries == null) {
        LOG.error("Bean Tag not found " + element.getLocalName());
    }

    if (element.getTagName().equals(INC_TAG)) {
        String parentId = element.getAttribute("compId");
        bean.setParentName(parentId);

        return;
    }

    if (element.getTagName().equals("content")) {
        bean.setParentName("Uif-Content");

        String markup = nodesToString(element.getChildNodes());
        bean.addPropertyValue("markup", markup);

        return;
    }

    // Retrieve the information for the new bean tag and fill in the default parent if needed
    BeanTagInfo tagInfo = CustomTagAnnotations.getBeanTags().get(element.getLocalName());

    String elementParent = element.getAttribute("parent");
    if (StringUtils.isNotBlank(elementParent) && !StringUtils.equals(elementParent, tagInfo.getParent())) {
        bean.setParentName(elementParent);
    } else if (StringUtils.isNotBlank(tagInfo.getParent())) {
        bean.setParentName(tagInfo.getParent());
    }

    // Create the map for the attributes found in the tag and process them in to the definition builder.
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        processSingleValue(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue(), entries, bean);
    }

    ArrayList<Element> children = (ArrayList<Element>) DomUtils.getChildElements(element);

    // Process the children found in the xml tag
    for (int i = 0; i < children.size(); i++) {
        String tag = children.get(i).getLocalName();
        BeanTagAttributeInfo info = entries.get(tag);

        if (children.get(i).getTagName().equals("spring:property")
                || children.get(i).getTagName().equals("property")) {
            BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
            delegate.parsePropertyElement(children.get(i), bean.getBeanDefinition());

            continue;
        }

        // Sets the property name to be used when adding the property value
        String propertyName;
        BeanTagAttribute.AttributeType type = null;
        if (info == null) {
            propertyName = CustomTagAnnotations.findPropertyByType(element.getLocalName(), tag);

            if (StringUtils.isNotBlank(propertyName)) {
                bean.addPropertyValue(propertyName, parseBean(children.get(i), bean, parserContext));

                continue;
            } else {
                // If the tag is not in the schema map let spring handle the value by forwarding the tag as the
                // propertyName
                propertyName = tag;
                type = findBeanType(children.get(i));
            }
        } else {
            // If the tag is found in the schema map use the connected name stored in the attribute information
            propertyName = info.getPropertyName();
            type = info.getType();
        }

        // Process the information stored in the child bean
        ArrayList<Element> grandChildren = (ArrayList<Element>) DomUtils.getChildElements(children.get(i));

        if (type == BeanTagAttribute.AttributeType.SINGLEVALUE) {
            String propertyValue = DomUtils.getTextValue(children.get(i));
            bean.addPropertyValue(propertyName, propertyValue);
        } else if (type == BeanTagAttribute.AttributeType.ANY) {
            String propertyValue = nodesToString(children.get(i).getChildNodes());
            bean.addPropertyValue(propertyName, propertyValue);
        } else if ((type == BeanTagAttribute.AttributeType.DIRECT)
                || (type == BeanTagAttribute.AttributeType.DIRECTORBYTYPE)) {
            boolean isPropertyTag = false;
            if ((children.get(i).getAttributes().getLength() == 0) && (grandChildren.size() == 1)) {
                String grandChildTag = grandChildren.get(0).getLocalName();

                Class<?> valueClass = info.getValueType();
                if (valueClass.isInterface()) {
                    try {
                        valueClass = Class.forName(valueClass.getName() + "Base");
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("Unable to find impl class for interface", e);
                    }
                }

                Set<String> validTagNames = CustomTagAnnotations.getBeanTagsByClass(valueClass);
                if (validTagNames.contains(grandChildTag)) {
                    isPropertyTag = true;
                }
            }

            if (isPropertyTag) {
                bean.addPropertyValue(propertyName, parseBean(grandChildren.get(0), bean, parserContext));
            } else {
                bean.addPropertyValue(propertyName, parseBean(children.get(i), bean, parserContext));
            }
        } else if ((type == BeanTagAttribute.AttributeType.SINGLEBEAN)
                || (type == BeanTagAttribute.AttributeType.BYTYPE)) {
            bean.addPropertyValue(propertyName, parseBean(grandChildren.get(0), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.LISTBEAN) {
            bean.addPropertyValue(propertyName, parseList(grandChildren, children.get(i), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.LISTVALUE) {
            bean.addPropertyValue(propertyName, parseList(grandChildren, children.get(i), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.MAPVALUE) {
            bean.addPropertyValue(propertyName, parseMap(grandChildren, children.get(i), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.MAPBEAN) {
            bean.addPropertyValue(propertyName, parseMap(grandChildren, children.get(i), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.SETVALUE) {
            bean.addPropertyValue(propertyName, parseSet(grandChildren, children.get(i), bean, parserContext));
        } else if (type == BeanTagAttribute.AttributeType.SETBEAN) {
            bean.addPropertyValue(propertyName, parseSet(grandChildren, children.get(i), bean, parserContext));
        }
    }
}

From source file:org.kuali.rice.krad.datadictionary.parse.CustomSchemaParser.java

/**
 * Adds the property value to the bean definition based on the name and value of the attribute.
 *
 * @param name - The name of the attribute.
 * @param value - The value of the attribute.
 * @param entries - The property entries for the over all tag.
 * @param bean - The bean definition being created.
 *///  w ww  . j  a v  a 2  s.  c om
protected void processSingleValue(String name, String value, Map<String, BeanTagAttributeInfo> entries,
        BeanDefinitionBuilder bean) {

    if (name.toLowerCase().compareTo("parent") == 0) {
        // If attribute is defining the parent set it in the bean builder.
        bean.setParentName(value);
    } else if (name.toLowerCase().compareTo("abstract") == 0) {
        // If the attribute is defining the parent as  abstract set it in the bean builder.
        bean.setAbstract(Boolean.valueOf(value));
    } else if (name.toLowerCase().compareTo("id") == 0) {
        //nothing - insures that its erased
    } else {
        // If the attribute is not a reserved case find the property name form the connected map and add the new
        // property value.

        if (name.contains("-ref")) {
            bean.addPropertyValue(name.substring(0, name.length() - 4), new RuntimeBeanReference(value));
        } else {
            BeanTagAttributeInfo info = entries.get(name);
            String propertyName;

            if (info == null) {
                propertyName = name;
            } else {
                propertyName = info.getName();
            }
            bean.addPropertyValue(propertyName, value);
        }
    }
}