⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domnodehelper.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j.dom;

import java.util.List;

import org.dom4j.Branch;
import org.dom4j.CharacterData;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Node;

import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

/**
 * <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.20 $
 */
public class DOMNodeHelper {
    public static final NodeList EMPTY_NODE_LIST = new EmptyNodeList();

    protected DOMNodeHelper() {
    }

    // 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;
            org.w3c.dom.Node previousParent = newChild.getParentNode();

            if (previousParent != null) {
                previousParent.removeChild(newChild);
            }

            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 n, String feature, String version) {
        return false;
    }

    public static boolean hasAttributes(Node node) {
        if ((node != null) && node instanceof Element) {
            return ((Element) node).attributeCount() > 0;
        } else {
            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 {
        if (count < 0) {
            throw new DOMException(DOMException.INDEX_SIZE_ERR,
                    "Illegal value for count: " + count);
        }

        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);
        }

        if ((offset + count) > length) {
            return text.substring(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);
            }
        }
    }

    public static void insertData(CharacterData data, int offset, String arg)
            throws DOMException {
        if (data.isReadOnly()) {
            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
                    "CharacterData node is read only: " + data);
        } else {
            String text = data.getText();

            if (text == null) {
                data.setText(arg);
            } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -