/** * 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; }
private void parse(Document doc) { NodeList nodeList = doc.getChildNodes(); for (int count = 0; count < nodeList.getLength(); count++) { Node node = nodeList.item(count); if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { parseAttrList(node.getChildNodes()); } } }
public static void removeChildren(Node e) { NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); e.removeChild(n); } } private static void getMatchingNodes(Node node, Pattern[] nodePath, int cur, List<Node> res) {
private String getTextValue(String def, Element doc, String tag) { String value = def; NodeList nl; nl = doc.getElementsByTagName(tag); if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) { value = nl.item(0).getFirstChild().getNodeValue(); } return value; }
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) { if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if ("property".equals(node.getNodeName()) || "property".equals(node.getLocalName())) { String name = ((Element) node).getAttribute("name"); if (name != null && name.length() > 0) { String value = ((Element) node).getAttribute("value"); String ref = ((Element) node).getAttribute("ref"); if (value != null && value.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, value); } else if (ref != null && ref.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref)); } else { throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />"); } } } } } } }
/** * For the given element, finds the first child Element with the given tag. */ private static Element getFirstTag(Element element, String tag) { try { NodeList nodeList = element.getElementsByTagName(tag); if (nodeList.getLength() == 0) return null; 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 tag "+tag+" under element="+element); } return null; }
private List<String> extractUsesPermissionNames(NodeList usesPermissionNodes) { List<String> usesPermissionQualifiedNames = new ArrayList<>(); for (int i = 0; i < usesPermissionNodes.getLength(); i++) { Node usesPermissionNode = usesPermissionNodes.item(i); Node nameAttribute = usesPermissionNode.getAttributes().getNamedItem("android:name"); if (nameAttribute == null) { return null; } usesPermissionQualifiedNames.add(nameAttribute.getNodeValue()); } return usesPermissionQualifiedNames; }
/** * 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; }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
/** * 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(); }
protected String getString(String tagName, Element element) { NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { NodeList subList = list.item(0).getChildNodes(); if (subList != null && subList.getLength() > 0) { return subList.item(0).getNodeValue(); } } return null; }
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) { if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if ("property".equals(node.getNodeName()) || "property".equals(node.getLocalName())) { String name = ((Element) node).getAttribute("name"); if (name != null && name.length() > 0) { String value = ((Element) node).getAttribute("value"); String ref = ((Element) node).getAttribute("ref"); if (value != null && value.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, value); } else if (ref != null && ref.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref)); } else { throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />"); } } } } } } }
private void addToDependencyMapFromXML (Map<String, List<ExternalExtensionDependency>> dependencies, Element eElement, String platform) { if (eElement.getElementsByTagName(platform).item(0) != null) { Element project = (Element)eElement.getElementsByTagName(platform).item(0); ArrayList<ExternalExtensionDependency> deps = new ArrayList<ExternalExtensionDependency>(); if (project.getTextContent().trim().equals("")) { // No dependencies required } else if (project.getTextContent().trim().equals("null")) { // Not supported deps = null; } else { NodeList nList = project.getElementsByTagName("dependency"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element dependencyNode = (Element)nNode; boolean external = Boolean.parseBoolean(dependencyNode.getAttribute("external")); deps.add(new ExternalExtensionDependency(dependencyNode.getTextContent(), external)); } } } dependencies.put(platform, deps); } }
private void populateContent(Node item, Object object) { for (int i = 0; i < item.getChildNodes().getLength(); i++) { Node child = item.getChildNodes().item(i); if (child instanceof Text) { setText(object, (Text) child); } } }
protected void parseCollectionElements( NodeList elementNodes, Collection<Object> target, @Nullable BeanDefinition bd, String defaultElementType) { for (int i = 0; i < elementNodes.getLength(); i++) { Node node = elementNodes.item(i); if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT)) { target.add(parsePropertySubElement((Element) node, bd, defaultElementType)); } } }
/** * 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; }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
/** * 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; }
public static Element loadElement(Element parent, String tagName) { NodeList nodeList = parent.getElementsByTagName(tagName); if (nodeList.getLength() > 1) { throw new ConfigException(tagName + " elements length over one!"); } if (nodeList.getLength() == 1) { return (Element) nodeList.item(0); } else { return null; } }
@SuppressWarnings("unchecked") private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) { if (nodeList != null && nodeList.getLength() > 0) { ManagedMap parameters = null; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if ("parameter".equals(node.getNodeName()) || "parameter".equals(node.getLocalName())) { if (parameters == null) { parameters = new ManagedMap(); } String key = ((Element) node).getAttribute("key"); String value = ((Element) node).getAttribute("value"); boolean hide = "true".equals(((Element) node).getAttribute("hide")); if (hide) { key = Constants.HIDE_KEY_PREFIX + key; } parameters.put(key, new TypedStringValue(value, String.class)); } } } return parameters; } return null; }