📄 documentnavigator.java
字号:
return buffer; } /** * Get the string value of an attribute node. * * @param object the target node * @return the text of the attribute value if the node is an * attribute, null otherwise */ public String getAttributeStringValue (Object object) { if (isAttribute(object)) return ((Node)object).getNodeValue(); else return null; } /** * Get the string value of text. * * @param object the target node * @return the string of text if the node is text, null otherwise */ public String getTextStringValue (Object object) { if (isText(object)) return ((Node)object).getNodeValue(); else return null; } /** * Get the string value of a comment node. * * @param object the target node * @return the text of the comment if the node is a comment, null otherwise */ public String getCommentStringValue (Object object) { if (isComment(object)) return ((Node)object).getNodeValue(); else return null; } /** * Get the string value of a namespace node. * * @param object the target node * @return the namespace URI as a (possibly empty) string if the * node is a namespace node, null otherwise */ public String getNamespaceStringValue (Object object) { if (isNamespace(object)) return ((NamespaceNode)object).getNodeValue(); else return null; } /** * Get the prefix value of a namespace node. * * @param object the target node * @return the namespace prefix a (possibly empty) string if the * node is a namespace node, null otherwise */ public String getNamespacePrefix (Object object) { if (isNamespace(object)) return ((NamespaceNode)object).getLocalName(); else return null; } /** * Translate a namespace prefix to a URI. * * @param prefix the namespace prefix * @param element the namespace context * @return the namespace URI bound to the prefix in the scope of <code>element</code>; * null if the prefix is not bound */ public String translateNamespacePrefixToUri (String prefix, Object element) { Iterator it = getNamespaceAxisIterator(element); while (it.hasNext()) { NamespaceNode ns = (NamespaceNode)it.next(); if (prefix.equals(ns.getNodeName())) return ns.getNodeValue(); } return null; } /** * Use JAXP to load a namespace aware document from a given URI. * * @param uri the URI of the document to load * @return the new W3C DOM Level 2 Document instance * @throws FunctionCallException containing a nested exception * if a problem occurs trying to parse the given document */ public Object getDocument(String uri) throws FunctionCallException { try { // We really do need to construct a new factory here each time. // DocumentBuilderFactory is not guaranteed to be thread safe? // Possibly we could make this a thread local.???? DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse( uri ); } catch (ParserConfigurationException e) { throw new FunctionCallException("JAXP setup error in document() function: " + e.getMessage(), e); } catch (SAXException e) { throw new FunctionCallException("XML error in document() function: " + e.getMessage(), e); } catch (IOException e) { throw new FunctionCallException("I/O error in document() function: " + e.getMessage(), e); } } /** * Get the target of a processing instruction node. * * @param obj the processing instruction * @return the target of the processing instruction * @throws ClassCastException if obj is not a processing instruxtion * */ public String getProcessingInstructionTarget(Object obj) { if (isProcessingInstruction(obj)) { ProcessingInstruction pi = (ProcessingInstruction) obj; return pi.getTarget(); } throw new ClassCastException(obj + " is not a processing instruction"); } /** * Get the data of a processing instruction node. * * @param obj the processing instruction * @return the target of the processing instruction * @throws ClassCastException if obj is not a processing instruxtion * */ public String getProcessingInstructionData(Object obj) { if (isProcessingInstruction(obj)) { ProcessingInstruction pi = (ProcessingInstruction) obj; return pi.getData(); } throw new ClassCastException(obj + " is not a processing instruction"); } //////////////////////////////////////////////////////////////////// // Inner class: iterate over DOM nodes. //////////////////////////////////////////////////////////////////// // FIXME: needs to recurse into // DocumentFragment and EntityReference // to use their children. /** * A generic iterator over DOM nodes. * * <p>Concrete subclasses must implement the {@link #getFirstNode} * and {@link #getNextNode} methods for a specific iteration * strategy.</p> */ abstract class NodeIterator implements Iterator { /** * Constructor. * * @param contextNode the starting node */ public NodeIterator (Node contextNode) { node = getFirstNode(contextNode); while (!isXPathNode(node)) { node = getNextNode(node); } } public boolean hasNext () { return (node != null); } public Object next () { if (node == null) throw new NoSuchElementException(); Node ret = node; node = getNextNode(node); while (!isXPathNode(node)) { node = getNextNode(node); } return ret; } public void remove () { throw new UnsupportedOperationException(); } /** * Get the first node for iteration. * * <p>This method must derive an initial node for iteration * from a context node.</p> * * @param contextNode the starting node * @return the first node in the iteration * @see #getNextNode */ protected abstract Node getFirstNode (Node contextNode); /** * Get the next node for iteration. * * <p>This method must locate a following node from the * current context node.</p> * * @param contextNode the current node in the iteration * @return the following node in the iteration, or null * if there is none * @see #getFirstNode */ protected abstract Node getNextNode (Node contextNode); /** * Test whether a DOM node is usable by XPath. * * @param node the DOM node to test * @return true if the node is usable, false if it should be skipped */ private boolean isXPathNode (Node node) { // null is usable, because it means end if (node == null) return true; switch (node.getNodeType()) { case Node.DOCUMENT_FRAGMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: return false; default: return true; } } private Node node; } //////////////////////////////////////////////////////////////////// // Inner class: iterate over a DOM named node map. //////////////////////////////////////////////////////////////////// /** * An iterator over an attribute list. */ private static class AttributeIterator implements Iterator { /** * Constructor. * * @param parent the parent DOM element for the attributes. */ AttributeIterator (Node parent) { this.map = parent.getAttributes(); this.pos = 0; for (int i = this.map.getLength()-1; i >= 0; i--) { Node node = map.item(i); if (! "http://www.w3.org/2000/xmlns/".equals(node.getNamespaceURI())) { this.lastAttribute = i; break; } } } public boolean hasNext () { return pos <= lastAttribute; } public Object next () { Node attr = map.item(pos++); if (attr == null) throw new NoSuchElementException(); else if ("http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())) { // XPath doesn't consider namespace declarations to be attributes // so skip it and go to the next one return next(); } else return attr; } public void remove () { throw new UnsupportedOperationException(); } private NamedNodeMap map; private int pos; private int lastAttribute = -1; } /** * Returns the element whose ID is given by elementId. * If no such element exists, returns null. * Attributes with the name "ID" are not of type ID unless so defined. * Attribute types are only known if when the parser understands DTD's or * schemas that declare attributes of type ID. When JAXP is used, you * must call <code>setValidating(true)</code> on the * DocumentBuilderFactory. * * @param object a node from the document in which to look for the id * @param elementId id to look for * * @return element whose ID is given by elementId, or null if no such * element exists in the document or if the implementation * does not know about attribute types * @see javax.xml.parsers.DocumentBuilderFactory * * @throws ClassCastException if object is not an org.w3c.dom.Node object * */ public Object getElementById(Object object, String elementId) { Document doc = (Document)getDocumentNode(object); if (doc != null) return doc.getElementById(elementId); else return null; }}// end of DocumentNavigator.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -