Example usage for org.springframework.util.xml DomUtils nodeNameEquals

List of usage examples for org.springframework.util.xml DomUtils nodeNameEquals

Introduction

In this page you can find the example usage for org.springframework.util.xml DomUtils nodeNameEquals.

Prototype

public static boolean nodeNameEquals(Node node, String desiredName) 

Source Link

Document

Namespace-aware equals comparison.

Usage

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse a map element.//from ww  w .  ja v a 2s .  co m
 */
public Map<?, ?> parseMapElement(Element mapEle, BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(BeanDefinitionParserDelegate.KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle,
            BeanDefinitionParserDelegate.ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
        // Should only have one value child element: ref, value, list, etc.
        // Optionally, there might be a key child element.
        NodeList entrySubNodes = entryEle.getChildNodes();
        Element keyEle = null;
        Element valueEle = null;
        for (int j = 0; j < entrySubNodes.getLength(); j++) {
            Node node = entrySubNodes.item(j);
            if (node instanceof Element) {
                Element candidateEle = (Element) node;
                if (DomUtils.nodeNameEquals(candidateEle, BeanDefinitionParserDelegate.KEY_ELEMENT)) {
                    if (keyEle != null) {
                        error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
                    } else {
                        keyEle = candidateEle;
                    }
                } else {
                    // Child element is what we're looking for.
                    if (valueEle != null) {
                        error("<entry> element must not contain more than one value sub-element", entryEle);
                    } else {
                        valueEle = candidateEle;
                    }
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key = null;
        boolean hasKeyAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE);
        boolean hasKeyRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
        if ((hasKeyAttribute && hasKeyRefAttribute)
                || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
        }
        if (hasKeyAttribute) {
            key = buildTypedStringValueForMap(entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE),
                    defaultKeyType, entryEle);
        } else if (hasKeyRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'key-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            key = ref;
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle, bd, defaultKeyType);
        } else {
            error("<entry> element must specify a key", entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value = null;
        boolean hasValueAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
        boolean hasValueRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
        if ((hasValueAttribute && hasValueRefAttribute)
                || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
        }
        if (hasValueAttribute) {
            value = buildTypedStringValueForMap(
                    entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE), defaultValueType,
                    entryEle);
        } else if (hasValueRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'value-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            value = ref;
        } else if (valueEle != null) {
            value = parsePropertySubElement(valueEle, bd, defaultValueType);
        } else {
            error("<entry> element must specify a value", entryEle);
        }

        // Add final key and value to the Map.
        map.put(key, value);
    }

    return map;
}

From source file:org.springframework.ide.eclipse.osgi.blueprint.internal.BlueprintParser.java

/**
 * Parse a value, ref or collection sub-element of a property or
 * constructor-arg element. This method is called from several places to
 * handle reusable elements such as idref, ref, null, value and so on.
 * //from  w ww.j  a  va2s .c  o m
 * In fact, this method is the main reason why the
 * BeanDefinitionParserDelegate is not used in full since the element
 * namespace becomes important as mixed rfc124/bean content can coexist.
 * 
 * @param ele
 *            subelement of property element; we don't know which yet
 * @param defaultValueType
 *            the default type (class name) for any
 *            <code>&lt;value&gt;</code> tag that might be created
 */
private Object parsePropertySubElement(Element ele, BeanDefinition bd, String defaultValueType) {
    // skip other namespace
    String namespaceUri = ele.getNamespaceURI();

    // check Spring own namespace
    if (parserContext.getDelegate().isDefaultNamespace(namespaceUri)) {
        return parserContext.getDelegate().parsePropertySubElement(ele, bd);
    }
    // let the delegate handle other ns
    else if (!NAMESPACE_URI.equals(namespaceUri)) {
        return parserContext.getDelegate().parseCustomElement(ele);
    }

    //
    else {
        if (DomUtils.nodeNameEquals(ele, BEAN)) {
            BeanDefinitionHolder bdHolder = parseComponentDefinitionElement(ele, bd);
            if (bdHolder != null) {
                bdHolder = ParsingUtils.decorateBeanDefinitionIfRequired(ele, bdHolder, parserContext);
            }
            return bdHolder;
        }

        if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.REF_ELEMENT)) {
            return parseRefElement(ele);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.IDREF_ELEMENT)) {
            return parseIdRefElement(ele);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.VALUE_ELEMENT)) {
            return parseValueElement(ele, defaultValueType);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.NULL_ELEMENT)) {
            // It's a distinguished null value. Let's wrap it in a
            // TypedStringValue
            // object in order to preserve the source location.
            TypedStringValue nullHolder = new TypedStringValue(null);
            nullHolder.setSource(parserContext.extractSource(ele));
            return nullHolder;
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.ARRAY_ELEMENT)) {
            return parseArrayElement(ele, bd);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.LIST_ELEMENT)) {
            return parseListElement(ele, bd);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.SET_ELEMENT)) {
            return parseSetElement(ele, bd);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.MAP_ELEMENT)) {
            return parseMapElement(ele, bd);
        } else if (DomUtils.nodeNameEquals(ele, BeanDefinitionParserDelegate.PROPS_ELEMENT)) {
            return parsePropsElement(ele);
        }

        // maybe it's a nested service/reference/ref-list/ref-set
        return parserContext.getDelegate().parseCustomElement(ele, bd);
    }
}