Example usage for org.springframework.beans.factory.xml BeanDefinitionParser parse

List of usage examples for org.springframework.beans.factory.xml BeanDefinitionParser parse

Introduction

In this page you can find the example usage for org.springframework.beans.factory.xml BeanDefinitionParser parse.

Prototype

@Nullable
BeanDefinition parse(Element element, ParserContext parserContext);

Source Link

Document

Parse the specified Element and register the resulting BeanDefinition BeanDefinition(s) with the org.springframework.beans.factory.xml.ParserContext#getRegistry() BeanDefinitionRegistry embedded in the supplied ParserContext .

Usage

From source file:org.apache.camel.spring.handler.CamelNamespaceHandler.java

/**
 * Used for auto registering producer and consumer templates if not already defined in XML.
 *///from  w ww  .  j a v a2s.c  o  m
protected void registerTemplates(Element element, ParserContext parserContext, String contextId) {
    boolean template = false;
    boolean consumerTemplate = false;

    NodeList list = element.getChildNodes();
    int size = list.getLength();
    for (int i = 0; i < size; i++) {
        Node child = list.item(i);
        if (child instanceof Element) {
            Element childElement = (Element) child;
            String localName = childElement.getLocalName();
            if ("template".equals(localName)) {
                template = true;
            } else if ("consumerTemplate".equals(localName)) {
                consumerTemplate = true;
            }
        }
    }

    if (!template) {
        // either we have not used template before or we have auto registered it already and therefore we
        // need it to allow to do it so it can remove the existing auto registered as there is now a clash id
        // since we have multiple camel contexts
        boolean existing = autoRegisterMap.get("template") != null;
        boolean inUse = false;
        try {
            inUse = parserContext.getRegistry().isBeanNameInUse("template");
        } catch (BeanCreationException e) {
            // Spring Eclipse Tooling may throw an exception when you edit the Spring XML online in Eclipse
            // when the isBeanNameInUse method is invoked, so ignore this and continue (CAMEL-2739)
            LOG.debug("Error checking isBeanNameInUse(template). This exception will be ignored", e);
        }
        if (!inUse || existing) {
            String id = "template";
            // auto create a template
            Element templateElement = element.getOwnerDocument().createElement("template");
            templateElement.setAttribute("id", id);
            BeanDefinitionParser parser = parserMap.get("template");
            BeanDefinition definition = parser.parse(templateElement, parserContext);

            // auto register it
            autoRegisterBeanDefinition(id, definition, parserContext, contextId);
        }
    }

    if (!consumerTemplate) {
        // either we have not used template before or we have auto registered it already and therefore we
        // need it to allow to do it so it can remove the existing auto registered as there is now a clash id
        // since we have multiple camel contexts
        boolean existing = autoRegisterMap.get("consumerTemplate") != null;
        boolean inUse = false;
        try {
            inUse = parserContext.getRegistry().isBeanNameInUse("consumerTemplate");
        } catch (BeanCreationException e) {
            // Spring Eclipse Tooling may throw an exception when you edit the Spring XML online in Eclipse
            // when the isBeanNameInUse method is invoked, so ignore this and continue (CAMEL-2739)
            LOG.debug("Error checking isBeanNameInUse(consumerTemplate). This exception will be ignored", e);
        }
        if (!inUse || existing) {
            String id = "consumerTemplate";
            // auto create a template
            Element templateElement = element.getOwnerDocument().createElement("consumerTemplate");
            templateElement.setAttribute("id", id);
            BeanDefinitionParser parser = parserMap.get("consumerTemplate");
            BeanDefinition definition = parser.parse(templateElement, parserContext);

            // auto register it
            autoRegisterBeanDefinition(id, definition, parserContext, contextId);
        }
    }

}

From source file:org.springframework.security.config.SecurityNamespaceHandler.java

public BeanDefinition parse(Element element, ParserContext pc) {
    if (!namespaceMatchesVersion(element)) {
        pc.getReaderContext().fatal(//from w w  w  .java2 s  .  c  o  m
                "You cannot use a spring-security-2.0.xsd or spring-security-3.0.xsd or spring-security-3.1.xsd schema or spring-security-3.2.xsd schema or spring-security-4.0.xsd schema "
                        + "with Spring Security 4.2. Please update your schema declarations to the 4.2 schema.",
                element);
    }
    String name = pc.getDelegate().getLocalName(element);
    BeanDefinitionParser parser = parsers.get(name);

    if (parser == null) {
        // SEC-1455. Load parsers when required, not just on init().
        loadParsers();
    }

    if (parser == null) {
        if (Elements.HTTP.equals(name) || Elements.FILTER_SECURITY_METADATA_SOURCE.equals(name)
                || Elements.FILTER_CHAIN_MAP.equals(name) || Elements.FILTER_CHAIN.equals(name)) {
            reportMissingWebClasses(name, pc, element);
        } else {
            reportUnsupportedNodeType(name, pc, element);
        }

        return null;
    }

    return parser.parse(element, pc);
}