📄 domutil.java
字号:
} /** * Returns true if the specified node matches the required names. Note, that * that tests return true if the required name is <code>null</code>. * * @param node * @param requiredLocalName * @param requiredNamespace * @return true if local name and namespace match the corresponding properties * of the given DOM node. */ public static boolean matches(Node node, String requiredLocalName, Namespace requiredNamespace) { if (node == null) { return false; } boolean matchingNamespace = matchingNamespace(node, requiredNamespace); return matchingNamespace && matchingLocalName(node, requiredLocalName); } /** * @param node * @param requiredNamespace * @return true if the required namespace is <code>null</code> or matches * the namespace of the specified node. */ private static boolean matchingNamespace(Node node, Namespace requiredNamespace) { if (requiredNamespace == null) { return true; } else { return requiredNamespace.isSame(node.getNamespaceURI()); } } /** * @param node * @param requiredLocalName * @return true if the required local name is <code>null</code> or if the * nodes local name matches. */ private static boolean matchingLocalName(Node node, String requiredLocalName) { if (requiredLocalName == null) { return true; } else { String localName = node.getLocalName(); return requiredLocalName.equals(localName); } } /** * @param node * @return true if the specified node is either an element or Text or CDATA */ private static boolean isAcceptedNode(Node node) { return isElement(node) || isText(node); } /** * @param node * @return true if the given node is of type element. */ static boolean isElement(Node node) { return node.getNodeType() == Node.ELEMENT_NODE; } /** * @param node * @return true if the given node is of type text or CDATA. */ static boolean isText(Node node) { int ntype = node.getNodeType(); return ntype == Node.TEXT_NODE || ntype == Node.CDATA_SECTION_NODE; } //----------------------------------------------------< factory methods >--- /** * Create a new DOM element with the specified local name and namespace. * * @param factory * @param localName * @param namespace * @return a new DOM element * @see Document#createElement(String) * @see Document#createElementNS(String, String) */ public static Element createElement(Document factory, String localName, Namespace namespace) { if (namespace != null) { return factory.createElementNS(namespace.getURI(), getPrefixedName(localName, namespace)); } else { return factory.createElement(localName); } } /** * Create a new DOM element with the specified local name and namespace and * add the specified text as Text node to it. * * @param factory * @param localName * @param namespace * @param text * @return a new DOM element * @see Document#createElement(String) * @see Document#createElementNS(String, String) * @see Document#createTextNode(String) * @see Node#appendChild(org.w3c.dom.Node) */ public static Element createElement(Document factory, String localName, Namespace namespace, String text) { Element elem = createElement(factory, localName, namespace); setText(elem, text); return elem; } /** * Add a new child element with the given local name and namespace to the * specified parent. * * @param parent * @param localName * @param namespace * @return the new element that was attached to the given parent. */ public static Element addChildElement(Element parent, String localName, Namespace namespace) { Element elem = createElement(parent.getOwnerDocument(), localName, namespace); parent.appendChild(elem); return elem; } /** * Add a new child element with the given local name and namespace to the * specified parent. * * @param parent * @param localName * @param namespace * @return the new element that was attached to the given parent. */ public static Element addChildElement(Node parent, String localName, Namespace namespace) { Document doc = parent.getOwnerDocument(); if (parent instanceof Document) { doc = (Document) parent; } Element elem = createElement(doc, localName, namespace); parent.appendChild(elem); return elem; } /** * Add a new child element with the given local name and namespace to the * specified parent. The specified text is added as Text node to the created * child element. * * @param parent * @param localName * @param namespace * @param text * @return child element that was added to the specified parent * @see Document#createElement(String) * @see Document#createElementNS(String, String) * @see Document#createTextNode(String) * @see Node#appendChild(org.w3c.dom.Node) */ public static Element addChildElement(Element parent, String localName, Namespace namespace, String text) { Element elem = createElement(parent.getOwnerDocument(), localName, namespace, text); parent.appendChild(elem); return elem; } /** * Create a new text node and add it as child to the given element. * * @param element * @param text */ public static void setText(Element element, String text) { if (text == null || "".equals(text)) { // ignore null/empty string text return; } Text txt = element.getOwnerDocument().createTextNode(text); element.appendChild(txt); } /** * Add an attribute node to the given element. * * @param element * @param attrLocalName * @param attrNamespace * @param attrValue */ public static void setAttribute(Element element, String attrLocalName, Namespace attrNamespace, String attrValue) { if (attrNamespace == null) { Attr attr = element.getOwnerDocument().createAttribute(attrLocalName); attr.setValue(attrValue); element.setAttributeNode(attr); } else { Attr attr = element.getOwnerDocument().createAttributeNS(attrNamespace.getURI(), getPrefixedName(attrLocalName, attrNamespace)); attr.setValue(attrValue); element.setAttributeNodeNS(attr); } } /** * Adds a namespace attribute on the given element. * * @param element * @param prefix * @param uri */ public static void setNamespaceAttribute(Element element, String prefix, String uri) { setAttribute(element, prefix, Namespace.XMLNS_NAMESPACE, uri); } /** * Converts the given timeout (long value defining the number of milli- * second until timeout is reached) to its Xml representation as defined * by RTF 2518.<br> * Note, that {@link DavConstants#INFINITE_TIMEOUT} is not represented by the String * {@link DavConstants#TIMEOUT_INFINITE 'Infinite'} defined by RFC 2518, due to a known * issue with Microsoft Office that opens the document "read only" and * never unlocks the resource if the timeout is missing or 'Infinite'. * * @param timeout number of milli-seconds until timeout is reached. * @return 'timeout' Xml element */ public static Element timeoutToXml(long timeout, Document factory) { String expString = "Second-"+ timeout/1000;; Element exp = createElement(factory, DavConstants.XML_TIMEOUT, DavConstants.NAMESPACE, expString); return exp; } /** * Returns the Xml representation of a boolean isDeep, where false * presents a depth value of '0', true a depth value of 'infinity'. * * @param isDeep * @return Xml representation */ public static Element depthToXml(boolean isDeep, Document factory) { return depthToXml(isDeep? "infinity" : "0", factory); } /** * Returns the Xml representation of a depth String. Webdav defines the * following valid values for depths: 0, 1, infinity * * @param depth * @return 'deep' JDOM element */ public static Element depthToXml(String depth, Document factory) { Element dElem = createElement(factory, DavConstants.XML_DEPTH, DavConstants.NAMESPACE, depth); return dElem; } /** * Builds a 'DAV:href' Xml element from the given href. Please note, that * the path present in the given String should be properly * {@link org.apache.jackrabbit.util.Text#escapePath(String) escaped} in order to prevent problems with * WebDAV clients. * * @param href String representing the text of the 'href' Xml element * @param factory the Document used as factory * @return Xml representation of a 'href' according to RFC 2518. */ public static Element hrefToXml(String href, Document factory) { return createElement(factory, DavConstants.XML_HREF, DavConstants.NAMESPACE, href); } /** * Return a qualified name of a DOM node consisting of "{" + namespace uri + "}" * + localName. If the specified namespace is <code>null</code> or represents * the empty namespace, the local name is returned. * * @param localName * @param namespace * @return */ public static String getQualifiedName(String localName, Namespace namespace) { if (namespace == null || namespace.equals(Namespace.EMPTY_NAMESPACE)) { return localName; } StringBuffer b = new StringBuffer("{"); b.append(namespace.getURI()).append("}"); b.append(localName); return b.toString(); } /** * Return the prefixed name of a DOM node consisting of * namespace prefix + ":" + local name. If the specified namespace is <code>null</code> * or contains an empty prefix, the local name is returned.<br> * NOTE, that this is the value to be used for the 'qualified Name' parameter * expected with the namespace sensitive factory methods. * * @param localName * @param namespace * @return qualified name consisting of prefix, ':' and local name. * @see Document#createAttributeNS(String, String) * @see Document#createElementNS(String, String) */ public static String getPrefixedName(String localName, Namespace namespace) { if (namespace == null || Namespace.EMPTY_NAMESPACE.equals(namespace) || Namespace.EMPTY_NAMESPACE.getPrefix().equals(namespace.getPrefix())) { return localName; } StringBuffer buf = new StringBuffer(namespace.getPrefix()); buf.append(":"); buf.append(localName); return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -