/** * Retrieves all child elements of the given DOM element. * @param ele the DOM element to analyze * @return a List of child {@code org.w3c.dom.Element} instances */ public static List<Element> getChildElements(Element ele) { Assert.notNull(ele, "Element must not be null"); NodeList nl = ele.getChildNodes(); List<Element> childEles = new ArrayList<>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { childEles.add((Element) node); } } return childEles; }
public BeanDefinitionHolder decorateBeanDefinitionIfRequired( Element ele, BeanDefinitionHolder definitionHolder, @Nullable BeanDefinition containingBd) { BeanDefinitionHolder finalDefinition = definitionHolder; // Decorate based on custom attributes first. NamedNodeMap attributes = ele.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node node = attributes.item(i); finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } // Decorate based on custom nested elements. NodeList children = ele.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } } return finalDefinition; }
/** * Parse a list element. */ public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedList<Object> target = new ManagedList<>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) { NodeList nodeList = element.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { boolean first = true; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if (tag.equals(node.getNodeName()) || tag.equals(node.getLocalName())) { if (first) { first = false; String isDefault = element.getAttribute("default"); if (StringUtils.isEmpty(isDefault)) { beanDefinition.getPropertyValues().addPropertyValue("default", "false"); } } BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required); if (subDefinition != null && ref != null && ref.length() > 0) { subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref)); } } } } } }
/** * Extracts the text value from the given DOM element, ignoring XML comments. * <p>Appends all CharacterData nodes and EntityReference nodes into a single * String value, excluding Comment nodes. Only exposes actual user-specified * text, no default values of any kind. * @see CharacterData * @see EntityReference * @see Comment */ public static String getTextValue(Element valueEle) { Assert.notNull(valueEle, "Element must not be null"); StringBuilder sb = new StringBuilder(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i); if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { sb.append(item.getNodeValue()); } } return sb.toString(); }
/** * Parse an array element. */ public Object parseArrayElement(Element arrayEle, @Nullable BeanDefinition bd) { String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = arrayEle.getChildNodes(); ManagedArray target = new ManagedArray(elementType, nl.getLength()); target.setSource(extractSource(arrayEle)); target.setElementTypeName(elementType); target.setMergeEnabled(parseMergeAttribute(arrayEle)); parseCollectionElements(nl, target, bd, elementType); return target; }
private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) { NodeList nodeList = element.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { boolean first = true; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if (tag.equals(node.getNodeName()) || tag.equals(node.getLocalName())) { if (first) { first = false; String isDefault = element.getAttribute("default"); if (StringUtils.isEmpty(isDefault)) { beanDefinition.getPropertyValues().addPropertyValue("default", "false"); } } BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required); if (subDefinition != null && ref != null && ref.length() > 0) { subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref)); } } } } } }
/** * Utility method that returns the first child element identified by its name. * @param ele the DOM element to analyze * @param childEleName the child element name to look for * @return the {@code org.w3c.dom.Element} instance, or {@code null} if none found */ @Nullable 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; } } return null; }
/** * Parse a set element. */ public Set<Object> parseSetElement(Element collectionEle, @Nullable BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
private List<Element> getChildrenByTagName(Element element, String tagName) { NodeList children = element.getChildNodes(); List<Element> elts = new ArrayList<>(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.ELEMENT_NODE && tagName.equals(children.item(i).getNodeName())) { elts.add((Element) children.item(i)); } } return elts; }
/** * Retrieves all child elements of the given DOM element that match any of the given element names. * Only looks at the direct child level of the given element; do not go into further depth * (in contrast to the DOM API's {@code getElementsByTagName} 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} instances * @see org.w3c.dom.Element * @see org.w3c.dom.Element#getElementsByTagName */ public static List<Element> getChildElementsByTagName(Element ele, String... childEleNames) { Assert.notNull(ele, "Element must not be null"); Assert.notNull(childEleNames, "Element names collection must not be null"); List<String> childEleNameList = Arrays.asList(childEleNames); NodeList nl = ele.getChildNodes(); List<Element> childEles = new ArrayList<>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && nodeNameMatch(node, childEleNameList)) { childEles.add((Element) node); } } return childEles; }
/** * Parse a list element. */ public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedList<Object> target = new ManagedList<>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
private static String extractContent(Element element, String defaultStr) { if ( element == null ) { return defaultStr; } NodeList children = element.getChildNodes(); StringBuilder result = new StringBuilder(""); for ( int i = 0; i < children.getLength() ; i++ ) { if ( children.item( i ).getNodeType() == Node.TEXT_NODE || children.item( i ).getNodeType() == Node.CDATA_SECTION_NODE ) { result.append( children.item( i ).getNodeValue() ); } } return result.toString().trim(); }
/** * For a given Element, treats the first child as a text element * and returns its value. */ public static String getEltText(Element element) { try { NodeList childNodeList = element.getChildNodes(); if (childNodeList.getLength() == 0) return ""; return childNodeList.item(0).getNodeValue(); } catch (Exception e) { log.warning("Exception e=" + e.getMessage() + " thrown calling getEltText on element=" + element); } return ""; }
/** * Parse a set element. */ public Set<Object> parseSetElement(Element collectionEle, @Nullable BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
/** * Returns the first child whose node type is Element under the given Element. */ private static Element getFirstChildElement(Element element) { try { NodeList nodeList = element.getChildNodes(); for (int i=0; i<nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) return (Element) node; } } catch (Exception e) { log.warning("Error getting first child Element for element=" + element+", exception=" + e); } return null; }
/** * Parse property sub-elements of the given bean element. */ public void parsePropertyElements(Element beanEle, BeanDefinition bd) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, PROPERTY_ELEMENT)) { parsePropertyElement((Element) node, bd); } } }
/** * Parse an array element. */ public Object parseArrayElement(Element arrayEle, @Nullable BeanDefinition bd) { String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = arrayEle.getChildNodes(); ManagedArray target = new ManagedArray(elementType, nl.getLength()); target.setSource(extractSource(arrayEle)); target.setElementTypeName(elementType); target.setMergeEnabled(parseMergeAttribute(arrayEle)); parseCollectionElements(nl, target, bd, elementType); return target; }
protected static void copyAndReplaceElement(Element oldElement, Element newElement) { while(oldElement.getChildNodes().getLength() > 0) { newElement.appendChild(oldElement.getChildNodes().item(0)); } NamedNodeMap attrs = oldElement.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); newElement.setAttribute(ns.getNodeName(), ns.getNodeValue()); } ((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement); } }
/** * Parse constructor-arg sub-elements of the given bean element. */ public void parseConstructorArgElements(Element beanEle, BeanDefinition bd) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, CONSTRUCTOR_ARG_ELEMENT)) { parseConstructorArgElement((Element) node, bd); } } }