📄 documentnavigator.java
字号:
// 1. Look for the namespace of the element itself String myNamespace = n.getNamespaceURI(); if (myNamespace != null && ! "".equals(myNamespace)) { String myPrefix = n.getPrefix(); if (!nsMap.containsKey(myPrefix)) { NamespaceNode ns = new NamespaceNode((Node) contextNode, myPrefix, myNamespace); nsMap.put(myPrefix, ns); } } if (n.hasAttributes()) { NamedNodeMap atts = n.getAttributes(); int length = atts.getLength(); // 2. Look for namespaces of attributes for (int i = 0; i < length; i++) { Attr att = (Attr) atts.item(i); // Work around Crimson bug by testing URI rather than name String attributeNamespace = att.getNamespaceURI(); if ("http://www.w3.org/2000/xmlns/".equals(attributeNamespace)) { } else if (attributeNamespace != null) { String prefix = att.getPrefix(); NamespaceNode ns = new NamespaceNode((Node)contextNode, prefix, attributeNamespace); // Add only if there's not a closer declaration in force. if (!nsMap.containsKey(prefix)) nsMap.put(prefix, ns); } } // 3. Look for namespace declaration attributes for (int i = 0; i < length; i++) { Attr att = (Attr) atts.item(i); // work around crimson bug by testing URI rather than name String attributeNamespace = att.getNamespaceURI(); if ("http://www.w3.org/2000/xmlns/".equals(attributeNamespace)) { NamespaceNode ns = new NamespaceNode( (Node)contextNode, att); // Add only if there's not a closer declaration in force. String name = ns.getNodeName(); if (!nsMap.containsKey(name)) nsMap.put(name, ns); } } } } // Section 5.4 of the XPath rec requires // this to be present. nsMap.put("xml", new NamespaceNode((Node)contextNode, "xml", "http://www.w3.org/XML/1998/namespace")); // An empty default namespace cancels // any previous default. NamespaceNode defaultNS = (NamespaceNode)nsMap.get(""); if (defaultNS != null && defaultNS.getNodeValue().length() == 0) { nsMap.remove(""); } return nsMap.values().iterator(); } else { return JaxenConstants.EMPTY_ITERATOR; } } /** Returns a parsed form of the given XPath string, which will be suitable * for queries on DOM documents. * * @param xpath the XPath expression * @return a parsed form of the given XPath string * @throws org.jaxen.saxpath.SAXPathException if the string is syntactically incorrect */ public XPath parseXPath (String xpath) throws org.jaxen.saxpath.SAXPathException { return new DOMXPath(xpath); } /** * Get the top-level document node. * * @param contextNode any node in the document * @return the root node */ public Object getDocumentNode (Object contextNode) { if (isDocument(contextNode)) return contextNode; else return ((Node)contextNode).getOwnerDocument(); } // Why are there separate methods for getElementNamespaceURI and // getAttributeNamespaceURI when they do exactly the same thing? // This should be combined in a future version. /** * Get the namespace URI of an element. * * @param element the target node * @return a string (possibly empty) if the node is an element, * and null otherwise */ public String getElementNamespaceUri (Object element) { try { Node node = (Node) element; if (node.getNodeType() == Node.ELEMENT_NODE) { return node.getNamespaceURI(); } } catch (ClassCastException ex) { } return null; } /** * Get the local name of an element. * * @param element the target node * @return a string representing the unqualified local name * if the node is an element, or null otherwise */ public String getElementName (Object element) { if (isElement(element)) { String name = ((Node)element).getLocalName(); if (name == null) name = ((Node)element).getNodeName(); return name; } return null; } /** * Get the qualified name of an element. * * @param element the target node * @return a string representing the qualified (i.e. possibly * prefixed) name if the argument is an element, or null otherwise */ public String getElementQName (Object element) { try { Node node = (Node) element; if (node.getNodeType() == Node.ELEMENT_NODE) { return node.getNodeName(); } } catch (ClassCastException ex) { } return null; } /** * Get the namespace URI of an attribute. * * @param attribute the target node * * @return the namespace name of the specified node * */ public String getAttributeNamespaceUri (Object attribute) { try { Node node = (Node) attribute; if (node.getNodeType() == Node.ATTRIBUTE_NODE) { return node.getNamespaceURI(); } } catch (ClassCastException ex) { } return null; } /** * Get the local name of an attribute. * * @param attribute the target node * @return a string representing the unqualified local name * if the node is an attribute, or null otherwise */ public String getAttributeName (Object attribute) { if (isAttribute(attribute)) { String name = ((Node)attribute).getLocalName(); if (name == null) name = ((Node)attribute).getNodeName(); return name; } return null; } /** * Get the qualified name of an attribute. * * @param attribute the target node * * @return a string representing the qualified (i.e. possibly * prefixed) name if the argument is an attribute, or null otherwise */ public String getAttributeQName (Object attribute) { try { Node node = (Node) attribute; if (node.getNodeType() == Node.ATTRIBUTE_NODE) { return node.getNodeName(); } } catch (ClassCastException ex) { } return null; } /** * Test if a node is a top-level document. * * @param object the target node * @return true if the node is the document root, false otherwise */ public boolean isDocument (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.DOCUMENT_NODE); } /** * Test if a node is a namespace. * * @param object the target node * @return true if the node is a namespace, false otherwise */ public boolean isNamespace (Object object) { return (object instanceof NamespaceNode); } /** * Test if a node is an element. * * @param object the target node * @return true if the node is an element, false otherwise */ public boolean isElement (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.ELEMENT_NODE); } /** * Test if a node is an attribute. <code>xmlns</code> and * <code>xmlns:pre</code> attributes do not count as attributes * for the purposes of XPath. * * @param object the target node * @return true if the node is an attribute, false otherwise */ public boolean isAttribute (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.ATTRIBUTE_NODE) && ! "http://www.w3.org/2000/xmlns/".equals(((Node) object).getNamespaceURI()); } /** * Test if a node is a comment. * * @param object the target node * @return true if the node is a comment, false otherwise */ public boolean isComment (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.COMMENT_NODE); } /** * Test if a node is plain text. * * @param object the target node * @return true if the node is a text node, false otherwise */ public boolean isText (Object object) { if (object instanceof Node) { switch (((Node)object).getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: return true; default: return false; } } else { return false; } } /** * Test if a node is a processing instruction. * * @param object the target node * @return true if the node is a processing instruction, false otherwise */ public boolean isProcessingInstruction (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.PROCESSING_INSTRUCTION_NODE); } /** * Get the string value of an element node. * * @param object the target node * @return the text inside the node and its descendants if the node * is an element, null otherwise */ public String getElementStringValue (Object object) { if (isElement(object)) { return getStringValue((Node)object, new StringBuffer()).toString(); } else { return null; } } /** * Construct a node's string value recursively. * * @param node the current node * @param buffer the buffer for building the text * @return the buffer passed as a parameter (for convenience) */ private StringBuffer getStringValue (Node node, StringBuffer buffer) { if (isText(node)) { buffer.append(node.getNodeValue()); } else { NodeList children = node.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { getStringValue(children.item(i), buffer); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -