Support varargs for DomUtils.getChildElementsByTagName

Issue: SPR-11272
This commit is contained in:
Juergen Hoeller 2014-01-03 12:18:19 +01:00
parent 2e123b01e5
commit e3344898cd
2 changed files with 14 additions and 12 deletions

View File

@ -32,7 +32,8 @@ import org.xml.sax.ContentHandler;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Convenience methods for working with the DOM API, in particular for working with DOM Nodes and DOM Elements. * Convenience methods for working with the DOM API,
* in particular for working with DOM Nodes and DOM Elements.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
@ -55,7 +56,7 @@ public abstract class DomUtils {
* @see org.w3c.dom.Element * @see org.w3c.dom.Element
* @see org.w3c.dom.Element#getElementsByTagName * @see org.w3c.dom.Element#getElementsByTagName
*/ */
public static List<Element> getChildElementsByTagName(Element ele, String[] childEleNames) { public static List<Element> getChildElementsByTagName(Element ele, String... childEleNames) {
Assert.notNull(ele, "Element must not be null"); Assert.notNull(ele, "Element must not be null");
Assert.notNull(childEleNames, "Element names collection must not be null"); Assert.notNull(childEleNames, "Element names collection must not be null");
List<String> childEleNameList = Arrays.asList(childEleNames); List<String> childEleNameList = Arrays.asList(childEleNames);
@ -81,7 +82,7 @@ public abstract class DomUtils {
* @see org.w3c.dom.Element#getElementsByTagName * @see org.w3c.dom.Element#getElementsByTagName
*/ */
public static List<Element> getChildElementsByTagName(Element ele, String childEleName) { public static List<Element> getChildElementsByTagName(Element ele, String childEleName) {
return getChildElementsByTagName(ele, new String[]{childEleName}); return getChildElementsByTagName(ele, new String[] {childEleName});
} }
/** /**
@ -184,7 +185,7 @@ public abstract class DomUtils {
/** /**
* Matches the given node's name and local name against the given desired names. * Matches the given node's name and local name against the given desired names.
*/ */
private static boolean nodeNameMatch(Node node, Collection desiredNames) { private static boolean nodeNameMatch(Node node, Collection<?> desiredNames) {
return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName())); return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName()));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@ package org.springframework.web.servlet.config;
import java.util.List; import java.util.List;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition; import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
@ -27,11 +29,10 @@ import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
import org.springframework.web.servlet.handler.MappedInterceptor; import org.springframework.web.servlet.handler.MappedInterceptor;
import org.w3c.dom.Element;
/** /**
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses a {@code interceptors} element to register * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses a
* a set of {@link MappedInterceptor} definitions. * {@code interceptors} element to register a set of {@link MappedInterceptor} definitions.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
@ -42,7 +43,7 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser {
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element)); CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
parserContext.pushContainingComponent(compDefinition); parserContext.pushContainingComponent(compDefinition);
List<Element> interceptors = DomUtils.getChildElementsByTagName(element, new String[] { "bean", "ref", "interceptor" }); List<Element> interceptors = DomUtils.getChildElementsByTagName(element, "bean", "ref", "interceptor");
for (Element interceptor : interceptors) { for (Element interceptor : interceptors) {
RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class); RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
mappedInterceptorDef.setSource(parserContext.extractSource(interceptor)); mappedInterceptorDef.setSource(parserContext.extractSource(interceptor));
@ -54,7 +55,7 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser {
if ("interceptor".equals(interceptor.getLocalName())) { if ("interceptor".equals(interceptor.getLocalName())) {
includePatterns = getIncludePatterns(interceptor, "mapping"); includePatterns = getIncludePatterns(interceptor, "mapping");
excludePatterns = getIncludePatterns(interceptor, "exclude-mapping"); excludePatterns = getIncludePatterns(interceptor, "exclude-mapping");
Element beanElem = DomUtils.getChildElementsByTagName(interceptor, new String[] { "bean", "ref"}).get(0); Element beanElem = DomUtils.getChildElementsByTagName(interceptor, "bean", "ref").get(0);
interceptorBean = parserContext.getDelegate().parsePropertySubElement(beanElem, null); interceptorBean = parserContext.getDelegate().parsePropertySubElement(beanElem, null);
} }
else { else {
@ -75,8 +76,8 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser {
private ManagedList<String> getIncludePatterns(Element interceptor, String elementName) { private ManagedList<String> getIncludePatterns(Element interceptor, String elementName) {
List<Element> paths = DomUtils.getChildElementsByTagName(interceptor, elementName); List<Element> paths = DomUtils.getChildElementsByTagName(interceptor, elementName);
ManagedList<String> patterns = new ManagedList<String>(paths.size()); ManagedList<String> patterns = new ManagedList<String>(paths.size());
for (int i = 0; i < paths.size(); i++) { for (Element path : paths) {
patterns.add(paths.get(i).getAttribute("path")); patterns.add(path.getAttribute("path"));
} }
return patterns; return patterns;
} }