htmlcollectionimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 573 行 · 第 1/2 页

JAVA
573
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999,2000 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment:   *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.html.internal.dom;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.html.HTMLAnchorElement;import org.w3c.dom.html.HTMLAppletElement;import org.w3c.dom.html.HTMLAreaElement;import org.w3c.dom.html.HTMLCollection;import org.w3c.dom.html.HTMLElement;import org.w3c.dom.html.HTMLFormElement;import org.w3c.dom.html.HTMLImageElement;import org.w3c.dom.html.HTMLObjectElement;import org.w3c.dom.html.HTMLOptionElement;import org.w3c.dom.html.HTMLTableCellElement;import org.w3c.dom.html.HTMLTableRowElement;import org.w3c.dom.html.HTMLTableSectionElement;/** * Implements {@link org.w3c.dom.html.HTMLCollection} to traverse any named * elements on a {@link org.w3c.dom.html.HTMLDocument}. The elements type to * look for is identified in the constructor by code. This collection is not * optimized for traversing large trees. * <p> * The collection has to meet two requirements: it has to be live, and it has * to traverse depth first and always return results in that order. As such, * using an object container (such as {@link java.util.Vector}) is expensive on * insert/remove operations. Instead, the collection has been implemented using * three traversing functions. As a result, operations on large documents will * result in traversal of the entire document tree and consume a considerable * amount of time. * <p> * Note that synchronization on the traversed document cannot be achieved. * The document itself cannot be locked, and locking each traversed node is * likely to lead to a dead lock condition. Therefore, there is a chance of the * document being changed as results are fetched; in all likelihood, the results * might be out dated, but not erroneous. *  *  * @version $Revision: 1.7 $ $Date: 2003/05/08 20:13:09 $ * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a> * @see org.w3c.dom.html.HTMLCollection */class HTMLCollectionImpl    implements HTMLCollection{        /**     * Request collection of all anchors in document: &lt;A&gt; elements that     * have a <code>name</code> attribute.     */    static final short        ANCHOR = 1;            /**     * Request collection of all forms in document: &lt;FORM&gt; elements.     */    static final short        FORM = 2;            /**     * Request collection of all images in document: &lt;IMAGE&gt; elements.     */    static final short        IMAGE = 3;            /**     * Request collection of all Applets in document: &lt;APPLET&gt; and     * &lt;OBJECT&gt; elements (&lt;OBJECT&gt; must contain an Applet).     */    static final short        APPLET = 4;            /**     * Request collection of all links in document: &lt;A&gt; and &lt;AREA&gt;     * elements (must have a <code>href</code> attribute).     */    static final short        LINK = 5;            /**     * Request collection of all options in selection: &lt;OPTION&gt; elments in     * &lt;SELECT&gt; or &lt;OPTGROUP&gt;.     */    static final short        OPTION = 6;            /**     * Request collection of all rows in table: &lt;TR&gt; elements in table or     * table section.     */    static final short        ROW = 7;        /**     * Request collection of all form elements: &lt;INPUT&gt;, &lt;BUTTON&gt;,     * &lt;SELECT&gt;, &lt;TEXT&gt; and &lt;TEXTAREA&gt; elements inside form     * &lt;FORM&gt;.     */    static final short        ELEMENT = 8;            /**     * Request collection of all areas in map: &lt;AREA&gt; element in &lt;MAP&gt;     * (non recursive).     */    static final short        AREA = -1;        /**     * Request collection of all table bodies in table: &lt;TBODY&gt; element in     * table &lt;TABLE&gt; (non recursive).     */    static final short        TBODY = -2;        /**     * Request collection of all cells in row: &lt;TD&gt; elements in &lt;TR&gt;     * (non recursive).     */    static final short        CELL = -3;        /**     * Indicates what this collection is looking for. Holds one of the enumerated     * values and used by {@link #collectionMatch}. Set by the constructor and     * determine the collection's use for its life time.     */    private short            _lookingFor;            /**     * This is the top level element underneath which the collection exists.     */    private Element            _topLevel;    /**     * Construct a new collection that retrieves element of the specific type     * (<code>lookingFor</code>) from the specific document portion     * (<code>topLevel</code>).     *      * @param topLevel The element underneath which the collection exists     * @param lookingFor Code indicating what elements to look for     */    HTMLCollectionImpl( HTMLElement topLevel, short lookingFor )    {        if ( topLevel == null )            throw new NullPointerException( "HTM011 Argument 'topLevel' is null." );        _topLevel = topLevel;       _lookingFor = lookingFor;    }        /**     * Returns the length of the collection. This method might traverse the     * entire document tree.     *      * @return Length of the collection     */    public final int getLength()    {        // Call recursive function on top-level element.        return getLength( _topLevel );    }    /**     * Retrieves the indexed node from the collection. Nodes are numbered in     * tree order - depth-first traversal order. This method might traverse     * the entire document tree.     *      * @param index The index of the node to return     * @return The specified node or null if no such node found     */    public final Node item( int index )    {        if ( index < 0 )            throw new IllegalArgumentException( "HTM012 Argument 'index' is negative." );        // Call recursive function on top-level element.        return item( _topLevel, new CollectionIndex( index ) );    }            /**     * Retrieves the named node from the collection. The name is matched case     * sensitive against the <TT>id</TT> attribute of each element in the     * collection, returning the first match. The tree is traversed in     * depth-first order. This method might traverse the entire document tree.     *      * @param name The name of the node to return     * @return The specified node or null if no such node found     */    public final Node namedItem( String name )    {        if ( name == null )            throw new NullPointerException( "HTM013 Argument 'name' is null." );        // Call recursive function on top-level element.        return namedItem( _topLevel, name );    }            /**     * Recursive function returns the number of elements of a particular type     * that exist under the top level element. This is a recursive function     * and the top level element is passed along.     *      * @param topLevel Top level element from which to scan     * @return Number of elements     */    private int getLength( Element topLevel )    {        int        length;        Node    node;            synchronized ( topLevel )        {            // Always count from zero and traverse all the childs of the            // current element in the order they appear.            length = 0;            node = topLevel.getFirstChild();            while ( node != null )            {                // If a particular node is an element (could be HTML or XML),		// do two things: if it's the one we're looking for, count		// another matched element; at any rate, traverse it's		// children as well.                if ( node instanceof Element )                {

⌨️ 快捷键说明

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