From: Juergen H. <jho...@us...> - 2008-03-23 22:41:12
|
Update of /cvsroot/springframework/spring/src/org/springframework/util/xml In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv4366/src/org/springframework/util/xml Modified Files: DomUtils.java Log Message: added "getChildElementsByTagNames" method with tag names Collection argument to DomUtils Index: DomUtils.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/util/xml/DomUtils.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** DomUtils.java 1 Oct 2007 13:35:44 -0000 1.9 --- DomUtils.java 23 Mar 2008 22:41:08 -0000 1.10 *************** *** 1,4 **** /* ! * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 18,21 **** --- 18,23 ---- import java.util.ArrayList; + import java.util.Collection; + import java.util.Collections; import java.util.List; *************** *** 43,62 **** /** ! * Retrieve all child elements of the given DOM element that match ! * the given element name. Only look at the direct child level of the * given element; do not go into further depth (in contrast to the * DOM API's <code>getElementsByTagName</code> method). * @param ele the DOM element to analyze ! * @param childEleName the child element name to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element * @see org.w3c.dom.Element#getElementsByTagName */ ! public static List getChildElementsByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); List childEles = new ArrayList(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); ! if (node instanceof Element && nodeNameEquals(node, childEleName)) { childEles.add(node); } --- 45,66 ---- /** ! * Retrieve all child elements of the given DOM element that match any of ! * the given element names. Only look at the direct child level of the * given element; do not go into further depth (in contrast to the * DOM API's <code>getElementsByTagName</code> method). * @param ele the DOM element to analyze ! * @param childEleNames the child element names to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element * @see org.w3c.dom.Element#getElementsByTagName */ ! public static List getChildElementsByTagNames(Element ele, Collection childEleNames) { ! Assert.notNull(ele, "Element must not be null"); ! Assert.notNull(childEleNames, "Element names collection must not be null"); NodeList nl = ele.getChildNodes(); List childEles = new ArrayList(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); ! if (node instanceof Element && nodeNameMatch(node, childEleNames)) { childEles.add(node); } *************** *** 66,69 **** --- 70,88 ---- /** + * Retrieve all child elements of the given DOM element that match + * the given element name. Only look at the direct child level of the + * given element; do not go into further depth (in contrast to the + * DOM API's <code>getElementsByTagName</code> method). + * @param ele the DOM element to analyze + * @param childEleName the child element name to look for + * @return a List of child <code>org.w3c.dom.Element</code> instances + * @see org.w3c.dom.Element + * @see org.w3c.dom.Element#getElementsByTagName + */ + public static List getChildElementsByTagName(Element ele, String childEleName) { + return getChildElementsByTagNames(ele, Collections.singleton(childEleName)); + } + + /** * Utility method that returns the first child element * identified by its name. *************** *** 74,81 **** */ public static Element getChildElementByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); ! if (node instanceof Element && nodeNameEquals(node, childEleName)) { return (Element) node; } --- 93,102 ---- */ public static Element getChildElementByTagName(Element ele, String childEleName) { + Assert.notNull(ele, "Element must not be null"); + Assert.notNull(childEleName, "Element name must not be null"); NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); ! if (node instanceof Element && nodeNameMatch(node, childEleName)) { return (Element) node; } *************** *** 98,112 **** /** - * Namespace-aware equals comparison. Returns <code>true</code> if either - * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>, - * otherwise returns <code>false</code>. - */ - public static boolean nodeNameEquals(Node node, String desiredName) { - Assert.notNull(node, "Node must not be null"); - Assert.notNull(desiredName, "Desired name must not be null"); - return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName())); - } - - /** * Extract the text value from the given DOM element, ignoring XML comments. * <p>Appends all CharacterData nodes and EntityReference nodes --- 119,122 ---- *************** *** 117,120 **** --- 127,131 ---- */ public static String getTextValue(Element valueEle) { + Assert.notNull(valueEle, "Element must not be null"); StringBuffer value = new StringBuffer(); NodeList nl = valueEle.getChildNodes(); *************** *** 128,130 **** --- 139,166 ---- } + /** + * Namespace-aware equals comparison. Returns <code>true</code> if either + * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>, + * otherwise returns <code>false</code>. + */ + public static boolean nodeNameEquals(Node node, String desiredName) { + Assert.notNull(node, "Node must not be null"); + Assert.notNull(desiredName, "Desired name must not be null"); + return nodeNameMatch(node, desiredName); + } + + /** + * Matches the given node's name and local name against the given desired name. + */ + private static boolean nodeNameMatch(Node node, String desiredName) { + return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName())); + } + + /** + * Matches the given node's name and local name against the given desired names. + */ + private static boolean nodeNameMatch(Node node, Collection desiredNames) { + return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName())); + } + } |