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

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

Introduction

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

Prototype

public static List<Element> getChildElementsByTagName(Element ele, String childEleName) 

Source Link

Document

Retrieves all child elements of the given DOM element that match the given element name.

Usage

From source file:org.springframework.integration.kafka.config.xml.KafkaProducerContextParser.java

private void parseProducerConfigurations(Element topics, ParserContext parserContext,
        BeanDefinitionBuilder builder, Element parentElem) {
    Map<String, BeanMetadataElement> producerConfigurationsMap = new ManagedMap<String, BeanMetadataElement>();

    for (Element producerConfiguration : DomUtils.getChildElementsByTagName(topics, "producer-configuration")) {

        BeanDefinitionBuilder producerMetadataBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(ProducerMetadata.class);
        producerMetadataBuilder.addConstructorArgValue(producerConfiguration.getAttribute("topic"));
        producerMetadataBuilder.addConstructorArgValue(producerConfiguration.getAttribute("key-class-type"));
        producerMetadataBuilder.addConstructorArgValue(producerConfiguration.getAttribute("value-class-type"));

        String keySerializer = producerConfiguration.getAttribute("key-serializer");
        String keyEncoder = producerConfiguration.getAttribute("key-encoder");
        Assert.isTrue((StringUtils.hasText(keySerializer) ^ StringUtils.hasText(keyEncoder)),
                "Exactly one of 'key-serializer' or 'key-encoder' must be specified");
        if (StringUtils.hasText(keyEncoder)) {
            if (log.isWarnEnabled()) {
                log.warn("'key-encoder' is a deprecated option, use 'key-serializer' instead.");
            }//from   w  w  w.  j  a v  a2 s  .c o  m
            BeanDefinitionBuilder encoderAdaptingSerializerBean = BeanDefinitionBuilder
                    .genericBeanDefinition(EncoderAdaptingSerializer.class);
            encoderAdaptingSerializerBean.addConstructorArgReference(keyEncoder);
            producerMetadataBuilder.addConstructorArgValue(encoderAdaptingSerializerBean.getBeanDefinition());
        } else {
            producerMetadataBuilder.addConstructorArgReference(keySerializer);
        }

        String valueSerializer = producerConfiguration.getAttribute("value-serializer");
        String valueEncoder = producerConfiguration.getAttribute("value-encoder");
        Assert.isTrue((StringUtils.hasText(valueSerializer) ^ StringUtils.hasText(valueEncoder)),
                "Exactly one of 'value-serializer' or 'value-encoder' must be specified");
        if (StringUtils.hasText(valueEncoder)) {
            if (log.isWarnEnabled()) {
                log.warn("'value-encoder' is a deprecated option, use 'value-serializer' instead.");
            }
            BeanDefinitionBuilder encoderAdaptingSerializerBean = BeanDefinitionBuilder
                    .genericBeanDefinition(EncoderAdaptingSerializer.class);
            encoderAdaptingSerializerBean.addConstructorArgReference(valueEncoder);
            producerMetadataBuilder.addConstructorArgValue(encoderAdaptingSerializerBean.getBeanDefinition());
        } else {
            producerMetadataBuilder.addConstructorArgReference(valueSerializer);
        }

        IntegrationNamespaceUtils.setReferenceIfAttributeDefined(producerMetadataBuilder, producerConfiguration,
                "partitioner");
        if (StringUtils.hasText(producerConfiguration.getAttribute("partitioner"))) {
            if (log.isWarnEnabled()) {
                log.warn("'partitioner' is a deprecated option. Use the 'kafka_partitionId' message header or "
                        + "the partition argument in the send() or convertAndSend() methods");
            }
        }
        IntegrationNamespaceUtils.setValueIfAttributeDefined(producerMetadataBuilder, producerConfiguration,
                "compression-type");
        IntegrationNamespaceUtils.setValueIfAttributeDefined(producerMetadataBuilder, producerConfiguration,
                "batch-bytes");
        AbstractBeanDefinition producerMetadataBeanDefinition = producerMetadataBuilder.getBeanDefinition();

        String producerPropertiesBean = parentElem.getAttribute("producer-properties");

        BeanDefinitionBuilder producerFactoryBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(ProducerFactoryBean.class);
        producerFactoryBuilder.addConstructorArgValue(producerMetadataBeanDefinition);

        final String brokerList = producerConfiguration.getAttribute("broker-list");
        if (StringUtils.hasText(brokerList)) {
            producerFactoryBuilder.addConstructorArgValue(producerConfiguration.getAttribute("broker-list"));
        }

        if (StringUtils.hasText(producerPropertiesBean)) {
            producerFactoryBuilder.addConstructorArgReference(producerPropertiesBean);
        }

        AbstractBeanDefinition producerFactoryBeanDefinition = producerFactoryBuilder.getBeanDefinition();

        BeanDefinitionBuilder producerConfigurationBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(ProducerConfiguration.class)
                .addConstructorArgValue(producerMetadataBeanDefinition)
                .addConstructorArgValue(producerFactoryBeanDefinition);
        IntegrationNamespaceUtils.setReferenceIfAttributeDefined(producerConfigurationBuilder,
                producerConfiguration, "conversion-service");
        producerConfigurationsMap.put(producerConfiguration.getAttribute("topic"),
                producerConfigurationBuilder.getBeanDefinition());
    }

    builder.addPropertyValue("producerConfigurations", producerConfigurationsMap);
}

From source file:org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.java

/**
 * Parse the {@code property} XML elements.
 *///from   w w w.  j a v  a2  s . co  m
protected void parseProperties(Element persistenceUnit, SpringPersistenceUnitInfo unitInfo) {
    Element propRoot = DomUtils.getChildElementByTagName(persistenceUnit, PROPERTIES);
    if (propRoot == null) {
        return;
    }
    List<Element> properties = DomUtils.getChildElementsByTagName(propRoot, "property");
    for (Element property : properties) {
        String name = property.getAttribute("name");
        String value = property.getAttribute("value");
        unitInfo.addProperty(name, value);
    }
}

From source file:org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.java

/**
 * Parse the {@code class} XML elements.
 *//*  w w w .  ja  v a 2s .  c o  m*/
protected void parseManagedClasses(Element persistenceUnit, SpringPersistenceUnitInfo unitInfo) {
    List<Element> classes = DomUtils.getChildElementsByTagName(persistenceUnit, MANAGED_CLASS_NAME);
    for (Element element : classes) {
        String value = DomUtils.getTextValue(element).trim();
        if (StringUtils.hasText(value))
            unitInfo.addManagedClassName(value);
    }
}

From source file:org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.java

/**
 * Parse the {@code mapping-file} XML elements.
 *///  www  .  j a v a  2 s .  c  o m
protected void parseMappingFiles(Element persistenceUnit, SpringPersistenceUnitInfo unitInfo) {
    List<Element> files = DomUtils.getChildElementsByTagName(persistenceUnit, MAPPING_FILE_NAME);
    for (Element element : files) {
        String value = DomUtils.getTextValue(element).trim();
        if (StringUtils.hasText(value)) {
            unitInfo.addMappingFileName(value);
        }
    }
}

From source file:org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.java

/**
 * Parse the {@code jar-file} XML elements.
 *//*from ww w. j a  v a  2 s  .  c o m*/
protected void parseJarFiles(Element persistenceUnit, SpringPersistenceUnitInfo unitInfo) throws IOException {
    List<Element> jars = DomUtils.getChildElementsByTagName(persistenceUnit, JAR_FILE_URL);
    for (Element element : jars) {
        String value = DomUtils.getTextValue(element).trim();
        if (StringUtils.hasText(value)) {
            Resource[] resources = this.resourcePatternResolver.getResources(value);
            boolean found = false;
            for (Resource resource : resources) {
                if (resource.exists()) {
                    found = true;
                    unitInfo.addJarFileUrl(resource.getURL());
                }
            }
            if (!found) {
                // relative to the persistence unit root, according to the JPA spec
                URL rootUrl = unitInfo.getPersistenceUnitRootUrl();
                if (rootUrl != null) {
                    unitInfo.addJarFileUrl(new URL(rootUrl, value));
                } else {
                    logger.warn("Cannot resolve jar-file entry [" + value + "] in persistence unit '"
                            + unitInfo.getPersistenceUnitName() + "' without root URL");
                }
            }
        }
    }
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

void createOpenIDLoginFilter(BeanReference sessionStrategy, BeanReference authManager) {
    Element openIDLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.OPENID_LOGIN);
    RootBeanDefinition openIDFilter = null;

    if (openIDLoginElt != null) {
        FormLoginBeanDefinitionParser parser = new FormLoginBeanDefinitionParser("/login/openid", null,
                OPEN_ID_AUTHENTICATION_PROCESSING_FILTER_CLASS, requestCache, sessionStrategy,
                allowSessionCreation, portMapper, portResolver);

        parser.parse(openIDLoginElt, pc);
        openIDFilter = parser.getFilterBean();
        openIDEntryPoint = parser.getEntryPointBean();
        openidLoginProcessingUrl = parser.getLoginProcessingUrl();
        openIDLoginPage = parser.getLoginPage();

        List<Element> attrExElts = DomUtils.getChildElementsByTagName(openIDLoginElt,
                Elements.OPENID_ATTRIBUTE_EXCHANGE);

        if (!attrExElts.isEmpty()) {
            // Set up the consumer with the required attribute list
            BeanDefinitionBuilder consumerBldr = BeanDefinitionBuilder
                    .rootBeanDefinition(OPEN_ID_CONSUMER_CLASS);
            BeanDefinitionBuilder axFactory = BeanDefinitionBuilder
                    .rootBeanDefinition(OPEN_ID_ATTRIBUTE_FACTORY_CLASS);
            ManagedMap<String, ManagedList<BeanDefinition>> axMap = new ManagedMap<String, ManagedList<BeanDefinition>>();

            for (Element attrExElt : attrExElts) {
                String identifierMatch = attrExElt.getAttribute("identifier-match");

                if (!StringUtils.hasText(identifierMatch)) {
                    if (attrExElts.size() > 1) {
                        pc.getReaderContext()
                                .error("You must supply an identifier-match attribute if using more"
                                        + " than one " + Elements.OPENID_ATTRIBUTE_EXCHANGE + " element",
                                        attrExElt);
                    }//from ww  w . j a v  a 2s .c  o  m
                    // Match anything
                    identifierMatch = ".*";
                }

                axMap.put(identifierMatch, parseOpenIDAttributes(attrExElt));
            }
            axFactory.addConstructorArgValue(axMap);

            consumerBldr.addConstructorArgValue(axFactory.getBeanDefinition());
            openIDFilter.getPropertyValues().addPropertyValue("consumer", consumerBldr.getBeanDefinition());
        }
    }

    if (openIDFilter != null) {
        openIDFilter.getPropertyValues().addPropertyValue("allowSessionCreation", allowSessionCreation);
        openIDFilter.getPropertyValues().addPropertyValue("authenticationManager", authManager);
        // Required by login page filter
        openIDFilterId = pc.getReaderContext().generateBeanName(openIDFilter);
        pc.registerBeanComponent(new BeanComponentDefinition(openIDFilter, openIDFilterId));
        injectRememberMeServicesRef(openIDFilter, rememberMeServicesId);

        createOpenIDProvider();
    }
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

private ManagedList<BeanDefinition> parseOpenIDAttributes(Element attrExElt) {
    ManagedList<BeanDefinition> attributes = new ManagedList<>();
    for (Element attElt : DomUtils.getChildElementsByTagName(attrExElt, Elements.OPENID_ATTRIBUTE)) {
        String name = attElt.getAttribute("name");
        String type = attElt.getAttribute("type");
        String required = attElt.getAttribute("required");
        String count = attElt.getAttribute("count");
        BeanDefinitionBuilder attrBldr = BeanDefinitionBuilder.rootBeanDefinition(OPEN_ID_ATTRIBUTE_CLASS);
        attrBldr.addConstructorArgValue(name);
        attrBldr.addConstructorArgValue(type);
        if (StringUtils.hasLength(required)) {
            attrBldr.addPropertyValue("required", Boolean.valueOf(required));
        }/*www.j  ava 2s.com*/

        if (StringUtils.hasLength(count)) {
            attrBldr.addPropertyValue("count", Integer.parseInt(count));
        }
        attributes.add(attrBldr.getBeanDefinition());
    }

    return attributes;
}

From source file:org.springframework.security.config.http.FilterInvocationSecurityMetadataSourceParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    List<Element> interceptUrls = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_URL);

    // Check for attributes that aren't allowed in this context
    for (Element elt : interceptUrls) {
        if (StringUtils.hasLength(elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_REQUIRES_CHANNEL))) {
            parserContext.getReaderContext().error("The attribute '"
                    + HttpSecurityBeanDefinitionParser.ATT_REQUIRES_CHANNEL + "' isn't allowed here.", elt);
        }/*from w w  w  . j  a v  a 2 s  .co  m*/

        if (StringUtils.hasLength(elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS))) {
            parserContext.getReaderContext().error(
                    "The attribute '" + HttpSecurityBeanDefinitionParser.ATT_FILTERS + "' isn't allowed here.",
                    elt);
        }

        if (StringUtils.hasLength(elt.getAttribute(ATT_SERVLET_PATH))) {
            parserContext.getReaderContext()
                    .error("The attribute '" + ATT_SERVLET_PATH + "' isn't allowed here.", elt);
        }
    }

    BeanDefinition mds = createSecurityMetadataSource(interceptUrls, false, element, parserContext);

    String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);

    if (StringUtils.hasText(id)) {
        parserContext.registerComponent(new BeanComponentDefinition(mds, id));
        parserContext.getRegistry().registerBeanDefinition(id, mds);
    }

    return mds;
}

From source file:org.springframework.security.config.http.HttpSecurityBeanDefinitionParser.java

List<OrderDecorator> buildCustomFilterList(Element element, ParserContext pc) {
    List<Element> customFilterElts = DomUtils.getChildElementsByTagName(element, Elements.CUSTOM_FILTER);
    List<OrderDecorator> customFilters = new ArrayList<>();

    final String ATT_AFTER = "after";
    final String ATT_BEFORE = "before";
    final String ATT_POSITION = "position";

    for (Element elt : customFilterElts) {
        String after = elt.getAttribute(ATT_AFTER);
        String before = elt.getAttribute(ATT_BEFORE);
        String position = elt.getAttribute(ATT_POSITION);

        String ref = elt.getAttribute(ATT_REF);

        if (!StringUtils.hasText(ref)) {
            pc.getReaderContext().error("The '" + ATT_REF + "' attribute must be supplied",
                    pc.extractSource(elt));
        }/*from   w w w .  ja v  a2  s  .  co  m*/

        RuntimeBeanReference bean = new RuntimeBeanReference(ref);

        if (WebConfigUtils.countNonEmpty(new String[] { after, before, position }) != 1) {
            pc.getReaderContext().error("A single '" + ATT_AFTER + "', '" + ATT_BEFORE + "', or '"
                    + ATT_POSITION + "' attribute must be supplied", pc.extractSource(elt));
        }

        if (StringUtils.hasText(position)) {
            customFilters.add(new OrderDecorator(bean, SecurityFilters.valueOf(position)));
        } else if (StringUtils.hasText(after)) {
            SecurityFilters order = SecurityFilters.valueOf(after);
            if (order == SecurityFilters.LAST) {
                customFilters.add(new OrderDecorator(bean, SecurityFilters.LAST));
            } else {
                customFilters.add(new OrderDecorator(bean, order.getOrder() + 1));
            }
        } else if (StringUtils.hasText(before)) {
            SecurityFilters order = SecurityFilters.valueOf(before);
            if (order == SecurityFilters.FIRST) {
                customFilters.add(new OrderDecorator(bean, SecurityFilters.FIRST));
            } else {
                customFilters.add(new OrderDecorator(bean, order.getOrder() - 1));
            }
        }
    }

    return customFilters;
}

From source file:org.springframework.security.config.message.MessageSecurityBeanDefinitionParser.java

/**
 * @param element//www  .j  a va 2  s. com
 * @param parserContext
 * @return
 */
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    XmlReaderContext context = parserContext.getReaderContext();

    ManagedMap<BeanDefinition, String> matcherToExpression = new ManagedMap<BeanDefinition, String>();

    String id = element.getAttribute(ID_ATTR);

    List<Element> interceptMessages = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_MESSAGE);
    for (Element interceptMessage : interceptMessages) {
        String matcherPattern = interceptMessage.getAttribute(PATTERN_ATTR);
        String accessExpression = interceptMessage.getAttribute(ACCESS_ATTR);
        BeanDefinitionBuilder matcher = BeanDefinitionBuilder
                .rootBeanDefinition(SimpDestinationMessageMatcher.class);
        matcher.addConstructorArgValue(matcherPattern);
        matcherToExpression.put(matcher.getBeanDefinition(), accessExpression);
    }

    BeanDefinitionBuilder mds = BeanDefinitionBuilder
            .rootBeanDefinition(ExpressionBasedMessageSecurityMetadataSourceFactory.class);
    mds.setFactoryMethod("createExpressionMessageMetadataSource");
    mds.addConstructorArgValue(matcherToExpression);

    String mdsId = context.registerWithGeneratedName(mds.getBeanDefinition());

    ManagedList<BeanDefinition> voters = new ManagedList<BeanDefinition>();
    voters.add(new RootBeanDefinition(MessageExpressionVoter.class));
    BeanDefinitionBuilder adm = BeanDefinitionBuilder.rootBeanDefinition(ConsensusBased.class);
    adm.addConstructorArgValue(voters);

    BeanDefinitionBuilder inboundChannelSecurityInterceptor = BeanDefinitionBuilder
            .rootBeanDefinition(ChannelSecurityInterceptor.class);
    inboundChannelSecurityInterceptor.addConstructorArgValue(registry.getBeanDefinition(mdsId));
    inboundChannelSecurityInterceptor.addPropertyValue("accessDecisionManager", adm.getBeanDefinition());
    String inSecurityInterceptorName = context
            .registerWithGeneratedName(inboundChannelSecurityInterceptor.getBeanDefinition());

    if (StringUtils.hasText(id)) {
        registry.registerAlias(inSecurityInterceptorName, id);
    } else {
        BeanDefinitionBuilder mspp = BeanDefinitionBuilder
                .rootBeanDefinition(MessageSecurityPostProcessor.class);
        mspp.addConstructorArgValue(inSecurityInterceptorName);
        context.registerWithGeneratedName(mspp.getBeanDefinition());
    }

    return null;
}