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

📄 htmldocumentimpl.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * org/ozone-db/xml/dom/html/HTMLDocumentImpl.java
 *
 * The contents of this file are subject to the OpenXML Public
 * License Version 1.0; you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.openxml.org/license.html
 *
 * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
 * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
 * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
 *
 * The Initial Developer of this code under the License is Assaf Arkin.
 * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
 * All Rights Reserved.
 */

package org.ozoneDB.xml.dom.html;

import org.ozoneDB.xml.dom.DOMExceptionImpl;
import org.ozoneDB.xml.dom.DocumentImpl;
import org.ozoneDB.xml.dom.NodeImpl;
import org.w3c.dom.*;
import org.w3c.dom.html.*;

import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.util.Hashtable;


/**
 * Implements an HTML document. Provides access to the top level element in the
 * document, its body and title.
 * <P>
 * Several methods create new nodes of all basic types (comment, text, element,
 * etc.). These methods create new nodes but do not place them in the document
 * tree. The nodes may be placed in the document tree using {@link
 * org.w3c.dom.Node#appendChild} or {@link org.w3c.dom.Node#insertBefore}, or
 * they may be placed in some other document tree.
 * <P>
 * Note: &lt;FRAMESET&gt; documents are not supported at the moment, neither
 * are direct document writing ({@link #open}, {@link #write}) and HTTP attribute
 * methods ({@link #getURL}, {@link #getCookie}).
 *
 *
 * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
 * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
 * @see org.w3c.dom.html.HTMLDocument
 * @see org.ozoneDB.xml.XMLDocument
 */
public final class HTMLDocumentImpl extends DocumentImpl implements HTMLDocument {


    public synchronized Element getDocumentElement() {
        Node html;
        Node child;
        Node next;

        // The document element is the top-level HTML element of the HTML
        // document. Only this element should exist at the top level.
        // If the HTML element is found, all other elements that might
        // precede it are placed inside the HTML element.
        html = getFirstChild();
        while (html != null) {
            if (html instanceof HTMLHtmlElement) {
                synchronized (html) {
                    child = getFirstChild();
                    while (child != null && child != html) {
                        next = child.getNextSibling();
                        html.appendChild( child );
                        child = next;
                    }
                }
                return (HTMLElement)html;
            }
            html = html.getNextSibling();
        }

        // HTML element must exist. Create a new element and dump the
        // entire contents of the document into it in the same order as
        // they appear now.
        html = new HTMLHtmlElementImpl( (HTMLDocumentImpl)getOwnerDocument(), "HTML" );
        child = getFirstChild();
        while (child != null) {
            next = child.getNextSibling();
            html.appendChild( child );
            child = next;
        }
        appendChild( html );
        return (HTMLElement)html;
    }


    /**
     * Obtains the &lt;HEAD&gt; element in the document, creating one if does
     * not exist before. The &lt;HEAD&gt; element is the first element in the
     * &lt;HTML&gt; in the document. The &lt;HTML&gt; element is obtained by
     * calling {@link #getDocumentElement}. If the element does not exist, one
     * is created.
     * <P>
     * Called by {@link #getTitle}, {@link #setTitle}, {@link #getBody} and
     * {@link #setBody} to assure the document has the &lt;HEAD&gt; element
     * correctly placed.
     *
     * @return The &lt;HEAD&gt; element
     */
    public synchronized HTMLElement getHead() {
        Node head;
        Node html;
        Node child;
        Node next;

        // Call getDocumentElement() to get the HTML element that is also the
        // top-level element in the document. Get the first element in the
        // document that is called HEAD. Work with that.
        html = getDocumentElement();
        synchronized (html) {
            head = html.getFirstChild();
            while (head != null && !(head instanceof HTMLHeadElement)) {
                head = head.getNextSibling();
            }
            // HEAD exists but might not be first element in HTML: make sure
            // it is and return it.
            if (head != null) {
                synchronized (head) {
                    child = html.getFirstChild();
                    while (child != null && child != head) {
                        next = child.getNextSibling();
                        head.insertBefore( child, head.getFirstChild() );
                        child = next;
                    }
                }
                return (HTMLElement)head;
            }

            // Head does not exist, create a new one, place it at the top of the
            // HTML element and return it.
            head = new HTMLHeadElementImpl( (HTMLDocumentImpl)getOwnerDocument(), "HEAD" );
            html.insertBefore( head, html.getFirstChild() );
        }
        return (HTMLElement)head;
    }


    public synchronized String getTitle() {
        HTMLElement head;
        Node title;

        // Get the HEAD element and look for the TITLE element within.
        // When found, make sure the TITLE is a direct child of HEAD,
        // and return the title's text (the Text node contained within).
        head = getHead();
        title = head.getElementsByTagName( "TITLE" ).item( 0 );
        if (title != null) {
            if (title.getParentNode() != head) {
                head.appendChild( title );
            }
            return ((HTMLTitleElement)title).getText();
        }
        // No TITLE found, return an empty string.
        return "";
    }


    public synchronized void setTitle( String newTitle ) {
        HTMLElement head;
        Node title;

        // Get the HEAD element and look for the TITLE element within.
        // When found, make sure the TITLE is a direct child of HEAD,
        // and set the title's text (the Text node contained within).
        head = getHead();
        title = head.getElementsByTagName( "TITLE" ).item( 0 );
        if (title != null) {
            if (title.getParentNode() != head) {
                head.appendChild( title );
            }
            ((HTMLTitleElement)title).setText( newTitle );
        } else {
            // No TITLE found, create a new element and place it at the end
            // of the HEAD element.
            title = new HTMLTitleElementImpl( (HTMLDocumentImpl)getOwnerDocument(), "TITLE" );
            head.appendChild( title );
        }
    }


    public synchronized HTMLElement getBody() {
        Node html;
        Node head;
        Node body;
        Node child;
        Node next;

        // Call getDocumentElement() to get the HTML element that is also the
        // top-level element in the document. Get the first element in the
        // document that is called BODY. Work with that.
        html = getDocumentElement();
        head = getHead();
        synchronized (html) {
            body = head.getNextSibling();
            while (body != null && !(body instanceof HTMLBodyElement)) {
                body = body.getNextSibling();
            }
            // If BODY was not found, try looking for FRAMESET instead.
            if (body == null) {
                body = head.getNextSibling();
                while (body != null && !(body instanceof HTMLFrameSetElement)) {
                    body = body.getNextSibling();
                }
            }

            // BODY/FRAMESET exists but might not be second element in HTML
            // (after HEAD): make sure it is and return it.
            if (body != null) {
                synchronized (body) {
                    child = head.getNextSibling();
                    while (child != null && child != body) {
                        next = child.getNextSibling();
                        body.insertBefore( child, body.getFirstChild() );
                        child = next;
                    }
                }
                return (HTMLElement)body;
            }

            // BODY does not exist, create a new one, place it in the HTML element
            // right after the HEAD and return it.
            body = new HTMLBodyElementImpl( (HTMLDocumentImpl)getOwnerDocument(), "BODY" );
            html.appendChild( body );
        }
        return (HTMLElement)body;
    }


    public synchronized void setBody( HTMLElement newBody ) {
        Node html;
        Node body;
        Node head;
        Node child;

        synchronized (newBody) {
            // Call getDocumentElement() to get the HTML element that is also the
            // top-level element in the document. Get the first element in the
            // document that is called BODY. Work with that.
            html = getDocumentElement();
            head = getHead();
            synchronized (html) {
                body = this.getElementsByTagName( "BODY" ).item( 0 );
                // BODY exists but might not follow HEAD in HTML. If not,
                // make it so and replce it. Start with the HEAD and make
                // sure the BODY is the first element after the HEAD.
                if (body != null) {
                    synchronized (body) {
                        child = head;
                        while (child != null) {
                            if (child instanceof Element) {
                                if (child != body) {
                                    html.insertBefore( newBody, child );
                                } else {
                                    html.replaceChild( newBody, body );
                                }
                                return;
                            }
                            child = child.getNextSibling();
                        }
                        html.appendChild( newBody );
                    }
                    return;
                }
                // BODY does not exist, place it in the HTML element
                // right after the HEAD.
                html.appendChild( newBody );
            }
        }
    }


    public Element getElementById( String elementId ) {
        return getElementById( elementId, this );
    }


    public NodeList getElementsByName( String elementName ) {
        return new HTMLElementListImpl( this, "name" );
    }


    public Element createElement( String tagName ) throws DOMException {
        Class elemClass;
        Constructor cnst;

        // First, make sure tag name is all upper case, next get the associated
        // element class. If no class is found, generate a generic HTML element.
        // Do so also if an unexpected exception occurs.
        tagName = tagName.toUpperCase();
        elemClass = (Class)_elementTypesHTML.get( tagName );
        if (elemClass != null) {
            // Get the constructor for the element. The signature specifies an
            // owner document and a tag name. Use the constructor to instantiate
            // a new object and return it.
            try {
                cnst = elemClass.getConstructor( _elemClassSigHTML );
                return (Element)cnst.newInstance( new Object[] {this, tagName} );
            } catch (Exception except) {
                Throwable thrw;

                if (except instanceof java.lang.reflect.InvocationTargetException) {
                    thrw = ((java.lang.reflect.InvocationTargetException)except).getTargetException();
                } else {
                    thrw = except;
                }
                System.out.println( "Exception " + thrw.getClass().getName() );
                System.out.println( thrw.getMessage() );

                throw new IllegalStateException( "Tag '" + tagName
                        + "' associated with an Element class that failed to construct." );
            }
        }
        return new HTMLElementImpl( this, tagName );
    }


    public String getReferrer() {

⌨️ 快捷键说明

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