📄 domutil.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.webdav.xml;import org.apache.jackrabbit.webdav.DavConstants;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Attr;import org.w3c.dom.CharacterData;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.w3c.dom.NamedNodeMap;import javax.xml.parsers.DocumentBuilderFactory;import java.util.ArrayList;import java.util.List;/** * <code>DomUtil</code> provides some common utility methods related to w3c-DOM. */public class DomUtil { private static Logger log = LoggerFactory.getLogger(DomUtil.class); /** * Constant for <code>DocumentBuilderFactory</code> which is used * widely to create new <code>Document</code>s */ public static DocumentBuilderFactory BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); static { BUILDER_FACTORY.setNamespaceAware(true); BUILDER_FACTORY.setIgnoringComments(true); BUILDER_FACTORY.setIgnoringElementContentWhitespace(true); BUILDER_FACTORY.setCoalescing(true); } /** * Returns the value of the named attribute of the current element. * * @param parent * @param localName attribute local name or 'nodeName' if no namespace is * specified. * @param namespace or <code>null</code> * @return attribute value, or <code>null</code> if not found */ public static String getAttribute(Element parent, String localName, Namespace namespace) { if (parent == null) { return null; } Attr attribute; if (namespace == null) { attribute = parent.getAttributeNode(localName); } else { attribute = parent.getAttributeNodeNS(namespace.getURI(), localName); } if (attribute != null) { return attribute.getValue(); } else { return null; } } /** * Returns the namespace attributes of the given element. * * @param element * @return the namespace attributes. */ public static Attr[] getNamespaceAttributes(Element element) { NamedNodeMap attributes = element.getAttributes(); List nsAttr = new ArrayList(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (Namespace.XMLNS_NAMESPACE.getURI().equals(attr.getNamespaceURI())) { nsAttr.add(attr); } } return (Attr[]) nsAttr.toArray(new Attr[nsAttr.size()]); } /** * Concatenates the values of all child nodes of type 'Text' or 'CDATA'/ * * @param element * @return String representing the value of all Text and CDATA child nodes or * <code>null</code> if the length of the resulting String is 0. * @see #isText(Node) */ public static String getText(Element element) { StringBuffer content = new StringBuffer(); if (element != null) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (isText(child)) { // cast to super class that contains Text and CData content.append(((CharacterData) child).getData()); } } } return (content.length()==0) ? null : content.toString(); } /** * Same as {@link #getText(Element)} except that 'defaultValue' is returned * instead of <code>null</code>, if the element does not contain any text. * * @param element * @param defaultValue * @return */ public static String getText(Element element, String defaultValue) { String txt = getText(element); return (txt == null) ? defaultValue : txt; } /** * Removes leading and trailing whitespace after calling {@link #getText(Element)}. * * @param element * @return Trimmed text or <code>null</code> */ public static String getTextTrim(Element element) { String txt = getText(element); return (txt == null) ? txt : txt.trim(); } /** * Calls {@link #getText(Element)} on the first child element that matches * the given local name and namespace. * * @param parent * @param childLocalName * @param childNamespace * @return text contained in the first child that matches the given local name * and namespace or <code>null</code>. * @see #getText(Element) */ public static String getChildText(Element parent, String childLocalName, Namespace childNamespace) { Element child = getChildElement(parent, childLocalName, childNamespace); return (child == null) ? null : getText(child); } /** * Calls {@link #getTextTrim(Element)} on the first child element that matches * the given local name and namespace. * * @param parent * @param childLocalName * @param childNamespace * @return text contained in the first child that matches the given local name * and namespace or <code>null</code>. Note, that leading and trailing whitespace * is removed from the text. * @see #getTextTrim(Element) */ public static String getChildTextTrim(Element parent, String childLocalName, Namespace childNamespace) { Element child = getChildElement(parent, childLocalName, childNamespace); return (child == null) ? null : getTextTrim(child); } /** * Returns true if the given parent node has a child element that matches * the specified local name and namespace. * * @param parent * @param childLocalName * @param childNamespace * @return returns true if a child element exists that matches the specified * local name and namespace. */ public static boolean hasChildElement(Node parent, String childLocalName, Namespace childNamespace) { return getChildElement(parent, childLocalName, childNamespace) != null; } /** * Returns the first child element that matches the given local name and * namespace. If no child element is present or no child element matches, * <code>null</code> is returned. * * @param parent * @param childLocalName * @param childNamespace * @return first child element matching the specified names or <code>null</code>. */ public static Element getChildElement(Node parent, String childLocalName, Namespace childNamespace) { if (parent != null) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (isElement(child) && matches(child, childLocalName, childNamespace)) { return (Element)child; } } } return null; } /** * Returns a <code>ElementIterator</code> containing all child elements of * the given parent node that match the given local name and namespace. * If the namespace is <code>null</code> only the localName is compared. * * @param parent the node the children elements should be retrieved from * @param childLocalName * @param childNamespace * @return an <code>ElementIterator</code> giving access to all child elements * that match the specified localName and namespace. */ public static ElementIterator getChildren(Element parent, String childLocalName, Namespace childNamespace) { return new ElementIterator(parent, childLocalName, childNamespace); } /** * Return an <code>ElementIterator</code> over all child elements. * * @param parent * @return * @see #getChildren(Element, String, Namespace) for a method that only * retrieves child elements that match a specific local name and namespace. */ public static ElementIterator getChildren(Element parent) { return new ElementIterator(parent); } /** * Return the first child element * * @return the first child element or <code>null</code> if the given node has no * child elements. */ public static Element getFirstChildElement(Node parent) { if (parent != null) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (isElement(child)) { return (Element)child; } } } return null; } /** * Return true if the given parent contains any child that is either an * Element, Text or CDATA. * * @param parent * @return */ public static boolean hasContent(Node parent) { if (parent != null) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (isAcceptedNode(child)) { return true; } } } return false; } /** * Return a list of all child nodes that are either Element, Text or CDATA. * * @param parent * @return */ public static List getContent(Node parent) { List content = new ArrayList(); if (parent != null) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (isAcceptedNode(child)) { content.add(child); } } } return content; } /** * Build a Namespace from the prefix and uri retrieved from the given element. * * @return */ public static Namespace getNamespace(Element element) { String uri = element.getNamespaceURI(); String prefix = element.getPrefix(); if (uri == null) { return Namespace.EMPTY_NAMESPACE; } else { return Namespace.getNamespace(prefix, uri); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -