📄 domnodehelper.java
字号:
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: DOMNodeHelper.java,v 1.2 2003/06/10 16:18:33 per_nyfelt Exp $ */package org.dom4j.dom;import org.dom4j.*;import org.dom4j.CharacterData;import org.w3c.dom.DOMException;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.NodeList;import java.util.List;/** <p><code>DOMNodeHelper</code> contains a collection of utility methods * for use across Node implementations.</p> * * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> * @version $Revision: 1.2 $ */public class DOMNodeHelper { public static final NodeList EMPTY_NODE_LIST = new EmptyNodeList(); public static class EmptyNodeList implements NodeList { public org.w3c.dom.Node item(int index) { return null; } public int getLength() { return 0; } } // Node API //------------------------------------------------------------------------- public static boolean supports(Node node, String feature, String version) { return false; } public static String getNamespaceURI(Node node) { return null; } public static String getPrefix(Node node) { return null; } public static String getLocalName(Node node) { return null; } public static void setPrefix(Node node, String prefix) throws DOMException { notSupported(); } public static String getNodeValue(Node node) throws DOMException { return node.getText(); } public static void setNodeValue(Node node, String nodeValue) throws DOMException { node.setText(nodeValue); } public static org.w3c.dom.Node getParentNode(Node node) { return asDOMNode( node.getParent() ); } public static NodeList getChildNodes(Node node) { return EMPTY_NODE_LIST; } public static org.w3c.dom.Node getFirstChild(Node node) { return null; } public static org.w3c.dom.Node getLastChild(Node node) { return null; } public static org.w3c.dom.Node getPreviousSibling(Node node) { Element parent = node.getParent(); if ( parent != null ) { int index = parent.indexOf( node ); if ( index > 0 ) { Node previous = parent.node(index - 1); return asDOMNode( previous ); } } return null; } public static org.w3c.dom.Node getNextSibling(Node node) { Element parent = node.getParent(); if ( parent != null ) { int index = parent.indexOf( node ); if ( index >= 0 ) { if ( ++index < parent.nodeCount() ) { Node next = parent.node(index); return asDOMNode( next ); } } } return null; } public static NamedNodeMap getAttributes(Node node) { return null; } public static org.w3c.dom.Document getOwnerDocument(Node node) { return asDOMDocument( node.getDocument() ); } public static org.w3c.dom.Node insertBefore( Node node, org.w3c.dom.Node newChild, org.w3c.dom.Node refChild ) throws DOMException { if ( node instanceof Branch ) { Branch branch = (Branch) node; List list = branch.content(); int index = list.indexOf(refChild); if ( index < 0 ) { branch.add((Node) newChild); } else { list.add(index, newChild); } return newChild; } else { throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node ); } } public static org.w3c.dom.Node replaceChild( Node node, org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild ) throws DOMException { if ( node instanceof Branch ) { Branch branch = (Branch) node; List list = branch.content(); int index = list.indexOf(oldChild); if ( index < 0 ) { throw new DOMException( DOMException.NOT_FOUND_ERR, "Tried to replace a non existing child for node: " + node ); } list.set(index, newChild); return oldChild; } else { throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node ); } } public static org.w3c.dom.Node removeChild( Node node, org.w3c.dom.Node oldChild ) throws DOMException { if ( node instanceof Branch ) { Branch branch = (Branch) node; branch.remove((Node) oldChild); return oldChild; } throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node ); } public static org.w3c.dom.Node appendChild( Node node, org.w3c.dom.Node newChild ) throws DOMException { if ( node instanceof Branch ) { Branch branch = (Branch) node; branch.add( (Node) newChild ); return newChild; } throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node ); } public static boolean hasChildNodes(Node node) { return false; } public static org.w3c.dom.Node cloneNode(Node node, boolean deep) { return asDOMNode( (Node) node.clone() ); } public static void normalize(Node node) { notSupported(); } public static boolean isSupported(Node node, String feature, String version) { return false; } public static boolean hasAttributes(Node node) { return false; } // CharacterData API //------------------------------------------------------------------------- public static String getData(CharacterData charData) throws DOMException { return charData.getText(); } public static void setData(CharacterData charData, String data) throws DOMException { charData.setText(data); } public static int getLength(CharacterData charData) { String text = charData.getText(); return ( text != null ) ? text.length() : 0; } public static String substringData( CharacterData charData, int offset, int count ) throws DOMException { String text = charData.getText(); int length = ( text != null ) ? text.length() : 0; if ( offset < 0 || offset >= length ) { throw new DOMException( DOMException.INDEX_SIZE_ERR, "No text at offset: " + offset ); } return text.substring( offset, offset + count ); } public static void appendData( CharacterData charData, String arg ) throws DOMException { if ( charData.isReadOnly() ) { throw new DOMException( DOMException.NO_MODIFICATION_ALLOWED_ERR, "CharacterData node is read only: " + charData ); } else { String text = charData.getText(); if ( text == null ) { charData.setText( text ); } else { charData.setText( text + arg ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -