domutil.java

来自「JAVA 所有包」· Java 代码 · 共 832 行 · 第 1/2 页

JAVA
832
字号
/* * Copyright 1999-2002,2004,2005 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.util;import java.util.Hashtable;import com.sun.org.apache.xerces.internal.dom.AttrImpl;import com.sun.org.apache.xerces.internal.dom.DocumentImpl;import com.sun.org.apache.xerces.internal.impl.xs.opti.ElementImpl;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;/** * Some useful utility methods. * This class was modified in Xerces2 with a view to abstracting as * much as possible away from the representation of the underlying * parsed structure (i.e., the DOM).  This was done so that, if Xerces * ever adopts an in-memory representation more efficient than the DOM * (such as a DTM), we should easily be able to convert our schema * parsing to utilize it. * * @version $Id: DOMUtil.java,v 1.2.6.1 2005/09/05 07:48:07 neerajbj Exp $ */public class DOMUtil {        //    // Constructors    //        /** This class cannot be instantiated. */    protected DOMUtil() {}        //    // Public static methods    //        /**     * Copies the source tree into the specified place in a destination     * tree. The source node and its children are appended as children     * of the destination node.     * <p>     * <em>Note:</em> This is an iterative implementation.     */    public static void copyInto(Node src, Node dest) throws DOMException {                // get node factory        Document factory = dest.getOwnerDocument();        boolean domimpl = factory instanceof DocumentImpl;                // placement variables        Node start  = src;        Node parent = src;        Node place  = src;                // traverse source tree        while (place != null) {                        // copy this node            Node node = null;            int  type = place.getNodeType();            switch (type) {            case Node.CDATA_SECTION_NODE: {                node = factory.createCDATASection(place.getNodeValue());                break;            }            case Node.COMMENT_NODE: {                node = factory.createComment(place.getNodeValue());                break;            }            case Node.ELEMENT_NODE: {                Element element = factory.createElement(place.getNodeName());                node = element;                NamedNodeMap attrs  = place.getAttributes();                int attrCount = attrs.getLength();                for (int i = 0; i < attrCount; i++) {                    Attr attr = (Attr)attrs.item(i);                    String attrName = attr.getNodeName();                    String attrValue = attr.getNodeValue();                    element.setAttribute(attrName, attrValue);                    if (domimpl && !attr.getSpecified()) {                        ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);                    }                }                break;            }            case Node.ENTITY_REFERENCE_NODE: {                node = factory.createEntityReference(place.getNodeName());                break;            }            case Node.PROCESSING_INSTRUCTION_NODE: {                node = factory.createProcessingInstruction(place.getNodeName(),                        place.getNodeValue());                break;            }            case Node.TEXT_NODE: {                node = factory.createTextNode(place.getNodeValue());                break;            }            default: {                throw new IllegalArgumentException("can't copy node type, "+                        type+" ("+                        node.getNodeName()+')');            }            }            dest.appendChild(node);                        // iterate over children            if (place.hasChildNodes()) {                parent = place;                place  = place.getFirstChild();                dest   = node;            }                        // advance            else {                place = place.getNextSibling();                while (place == null && parent != start) {                    place  = parent.getNextSibling();                    parent = parent.getParentNode();                    dest   = dest.getParentNode();                }            }                    }            } // copyInto(Node,Node)        /** Finds and returns the first child element node. */    public static Element getFirstChildElement(Node parent) {                // search for node        Node child = parent.getFirstChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE) {                return (Element)child;            }            child = child.getNextSibling();        }                // not found        return null;            } // getFirstChildElement(Node):Element        /** Finds and returns the first visible child element node. */    public static Element getFirstVisibleChildElement(Node parent) {                // search for node        Node child = parent.getFirstChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(child)) {                return (Element)child;            }            child = child.getNextSibling();        }                // not found        return null;            } // getFirstChildElement(Node):Element        /** Finds and returns the first visible child element node. */    public static Element getFirstVisibleChildElement(Node parent, Hashtable hiddenNodes) {                // search for node        Node child = parent.getFirstChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(child, hiddenNodes)) {                return (Element)child;            }            child = child.getNextSibling();        }                // not found        return null;            } // getFirstChildElement(Node):Element        /** Finds and returns the last child element node.      *  Overload previous method for non-Xerces node impl.     */    public static Element getLastChildElement(Node parent) {                // search for node        Node child = parent.getLastChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE) {                return (Element)child;            }            child = child.getPreviousSibling();        }                // not found        return null;            } // getLastChildElement(Node):Element        /** Finds and returns the last visible child element node. */    public static Element getLastVisibleChildElement(Node parent) {                // search for node        Node child = parent.getLastChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(child)) {                return (Element)child;            }            child = child.getPreviousSibling();        }                // not found        return null;            } // getLastChildElement(Node):Element        /** Finds and returns the last visible child element node.      *  Overload previous method for non-Xerces node impl     */    public static Element getLastVisibleChildElement(Node parent, Hashtable hiddenNodes) {                // search for node        Node child = parent.getLastChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(child, hiddenNodes)) {                return (Element)child;            }            child = child.getPreviousSibling();        }                // not found        return null;            } // getLastChildElement(Node):Element    /** Finds and returns the next sibling element node. */    public static Element getNextSiblingElement(Node node) {                // search for node        Node sibling = node.getNextSibling();        while (sibling != null) {            if (sibling.getNodeType() == Node.ELEMENT_NODE) {                return (Element)sibling;            }            sibling = sibling.getNextSibling();        }                // not found        return null;            } // getNextSiblingElement(Node):Element        // get next visible (un-hidden) node.    public static Element getNextVisibleSiblingElement(Node node) {                // search for node        Node sibling = node.getNextSibling();        while (sibling != null) {            if (sibling.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(sibling)) {                return (Element)sibling;            }            sibling = sibling.getNextSibling();        }                // not found        return null;            } // getNextSiblingdElement(Node):Element        // get next visible (un-hidden) node, overload previous method for non Xerces node impl    public static Element getNextVisibleSiblingElement(Node node, Hashtable hiddenNodes) {                // search for node        Node sibling = node.getNextSibling();        while (sibling != null) {            if (sibling.getNodeType() == Node.ELEMENT_NODE &&                    !isHidden(sibling, hiddenNodes)) {                return (Element)sibling;            }            sibling = sibling.getNextSibling();        }                // not found        return null;            } // getNextSiblingdElement(Node):Element        // set this Node as being hidden    public static void setHidden(Node node) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(true, false);    } // setHidden(node):void    // set this Node as being hidden, overloaded method    public static void setHidden(Node node, Hashtable hiddenNodes) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(true, false);        else        	hiddenNodes.put(node, "");    } // setHidden(node):void        // set this Node as being visible    public static void setVisible(Node node) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(false, false);    } // setVisible(node):void       // set this Node as being visible, overloaded method    public static void setVisible(Node node, Hashtable hiddenNodes) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(false, false);        else        	hiddenNodes.remove(node);    } // setVisible(node):void        // is this node hidden?    public static boolean isHidden(Node node) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            return ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).getReadOnly();        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            return ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).getReadOnly();        return false;    } // isHidden(Node):boolean    // is this node hidden? overloaded method    public static boolean isHidden(Node node, Hashtable hiddenNodes) {        if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)            return ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).getReadOnly();        else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl)            return ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).getReadOnly();        else        	return hiddenNodes.containsKey(node);    } // isHidden(Node):boolean        /** Finds and returns the first child node with the given name. */    public static Element getFirstChildElement(Node parent, String elemName) {                // search for node        Node child = parent.getFirstChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE) {                if (child.getNodeName().equals(elemName)) {                    return (Element)child;                }            }            child = child.getNextSibling();        }                // not found        return null;            } // getFirstChildElement(Node,String):Element        /** Finds and returns the last child node with the given name. */    public static Element getLastChildElement(Node parent, String elemName) {                // search for node        Node child = parent.getLastChild();        while (child != null) {            if (child.getNodeType() == Node.ELEMENT_NODE) {                if (child.getNodeName().equals(elemName)) {                    return (Element)child;                }            }            child = child.getPreviousSibling();        }                // not found        return null;            } // getLastChildElement(Node,String):Element        /** Finds and returns the next sibling node with the given name. */    public static Element getNextSiblingElement(Node node, String elemName) {                // search for node        Node sibling = node.getNextSibling();        while (sibling != null) {            if (sibling.getNodeType() == Node.ELEMENT_NODE) {                if (sibling.getNodeName().equals(elemName)) {                    return (Element)sibling;                }            }            sibling = sibling.getNextSibling();        }                // not found

⌨️ 快捷键说明

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