htmldocumentimpl.java

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

JAVA
815
字号
                    // 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.                    body = list.item( 0 );                    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 synchronized Element getElementById( String elementId )    {        return getElementById( elementId, this );    }    public NodeList getElementsByName( String elementName )    {        return new NameNodeListImpl( this, elementName );    }    public final NodeList getElementsByTagName( String tagName )    {        return super.getElementsByTagName( tagName.toUpperCase(Locale.ENGLISH) );    }    public final NodeList getElementsByTagNameNS( String namespaceURI,                                                  String localName )    {        if ( namespaceURI != null && namespaceURI.length() > 0 )            return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase(Locale.ENGLISH) );        else            return super.getElementsByTagName( localName.toUpperCase(Locale.ENGLISH) );    }    /**     * Xerces-specific constructor. "localName" is passed in, so we don't need     * to create a new String for it.     *     * @param namespaceURI The namespace URI of the element to     *                     create.     * @param qualifiedName The qualified name of the element type to     *                      instantiate.     * @param localName     The local name of the element to instantiate.     * @return Element A new Element object with the following attributes:     * @throws DOMException INVALID_CHARACTER_ERR: Raised if the specified     *                      name contains an invalid character.     */    public Element createElementNS(String namespaceURI, String qualifiedName,                                   String localpart)        throws DOMException    {        return createElementNS(namespaceURI, qualifiedName);    }    public Element createElementNS( String namespaceURI, String qualifiedName )    {        if ( namespaceURI == null || namespaceURI.length() == 0 )            return createElement( qualifiedName );        else {            return super.createElementNS( namespaceURI, qualifiedName );        }    }    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(Locale.ENGLISH);        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( "HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName);            }        }        return new HTMLElementImpl( this, tagName );    }    /**     * Creates an Attribute having this Document as its OwnerDoc.     * Overrides {@link DocumentImpl#createAttribute} and returns     * and attribute whose name is lower case.     *     * @param name The name of the attribute     * @return An attribute whose name is all lower case     * @throws DOMException(INVALID_NAME_ERR) if the attribute name     *   is not acceptable     */    public Attr createAttribute( String name )        throws DOMException    {        return super.createAttribute( name.toLowerCase(Locale.ENGLISH) );    }    public String getReferrer()    {        // Information not available on server side.        return null;    }    public String getDomain()    {        // Information not available on server side.        return null;    }    public String getURL()    {        // Information not available on server side.        return null;    }    public String getCookie()    {        // Information not available on server side.        return null;    }    public void setCookie( String cookie )    {        // Information not available on server side.    }    public HTMLCollection getImages()    {        // For more information see HTMLCollection#collectionMatch        if ( _images == null )            _images = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.IMAGE );        return _images;    }    public HTMLCollection getApplets()    {        // For more information see HTMLCollection#collectionMatch        if ( _applets == null )            _applets = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.APPLET );        return _applets;    }    public HTMLCollection getLinks()    {        // For more information see HTMLCollection#collectionMatch        if ( _links == null )            _links = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.LINK );        return _links;    }    public HTMLCollection getForms()    {        // For more information see HTMLCollection#collectionMatch        if ( _forms == null )            _forms = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.FORM );        return _forms;    }    public HTMLCollection getAnchors()    {        // For more information see HTMLCollection#collectionMatch        if ( _anchors == null )            _anchors = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.ANCHOR );        return _anchors;    }    public void open()    {        // When called an in-memory is prepared. The document tree is still        // accessible the old way, until this writer is closed.        if ( _writer == null )            _writer = new StringWriter();    }    public void close()    {        // ! NOT IMPLEMENTED, REQUIRES PARSER !        if ( _writer != null )        {            _writer = null;        }    }    public void write( String text )    {        // Write a string into the in-memory writer.        if ( _writer != null )            _writer.write( text );    }    public void writeln( String text )    {        // Write a line into the in-memory writer.        if ( _writer != null )            _writer.write( text + "\n" );    }    public Node cloneNode( boolean deep )    {        HTMLDocumentImpl    clone;        NodeImpl            node;        clone = new HTMLDocumentImpl();        if ( deep ) {            node = (NodeImpl) getFirstChild();            while ( node != null ) {                clone.appendChild( clone.importNode( node, true ) );                node = (NodeImpl) node.getNextSibling();            }        }        return clone;    }    /**     * Recursive method retreives an element by its <code>id</code> attribute.     * Called by {@link #getElementById(String)}.     *     * @param elementId The <code>id</code> value to look for     * @return The node in which to look for     */    private Element getElementById( String elementId, Node node )    {        Node    child;        Element    result;        child = node.getFirstChild();        while ( child != null )        {            if ( child instanceof Element )            {                if ( elementId.equals( ( (Element) child ).getAttribute( "id" ) ) )                    return (Element) child;                result = getElementById( elementId, child );                if ( result != null )                    return result;            }            child = child.getNextSibling();        }        return null;    }    /**     * Called by the constructor to populate the element types list (see {@link     * #_elementTypesHTML}). Will be called multiple times but populate the list     * only the first time. Replacement for static constructor.     */    private synchronized static void populateElementTypes()    {        // This class looks like it is due to some strange        // (read: inconsistent) JVM bugs.        // Initially all this code was placed in the static constructor,        // but that caused some early JVMs (1.1) to go mad, and if a        // class could not be found (as happened during development),        // the JVM would die.        // Bertrand Delacretaz <bdelacretaz@worldcom.ch> pointed out        // several configurations where HTMLAnchorElementImpl.class        // failed, forcing me to revert back to Class.forName().        if ( _elementTypesHTML != null )            return;        _elementTypesHTML = new Hashtable( 63 );        populateElementType( "A", "HTMLAnchorElementImpl" );        populateElementType( "APPLET", "HTMLAppletElementImpl" );        populateElementType( "AREA", "HTMLAreaElementImpl" );        populateElementType( "BASE",  "HTMLBaseElementImpl" );        populateElementType( "BASEFONT", "HTMLBaseFontElementImpl" );        populateElementType( "BLOCKQUOTE", "HTMLQuoteElementImpl" );        populateElementType( "BODY", "HTMLBodyElementImpl" );        populateElementType( "BR", "HTMLBRElementImpl" );        populateElementType( "BUTTON", "HTMLButtonElementImpl" );        populateElementType( "DEL", "HTMLModElementImpl" );        populateElementType( "DIR", "HTMLDirectoryElementImpl" );        populateElementType( "DIV",  "HTMLDivElementImpl" );        populateElementType( "DL", "HTMLDListElementImpl" );        populateElementType( "FIELDSET", "HTMLFieldSetElementImpl" );        populateElementType( "FONT", "HTMLFontElementImpl" );        populateElementType( "FORM", "HTMLFormElementImpl" );        populateElementType( "FRAME","HTMLFrameElementImpl" );        populateElementType( "FRAMESET", "HTMLFrameSetElementImpl" );        populateElementType( "HEAD", "HTMLHeadElementImpl" );        populateElementType( "H1", "HTMLHeadingElementImpl" );        populateElementType( "H2", "HTMLHeadingElementImpl" );        populateElementType( "H3", "HTMLHeadingElementImpl" );        populateElementType( "H4", "HTMLHeadingElementImpl" );        populateElementType( "H5", "HTMLHeadingElementImpl" );        populateElementType( "H6", "HTMLHeadingElementImpl" );        populateElementType( "HR", "HTMLHRElementImpl" );        populateElementType( "HTML", "HTMLHtmlElementImpl" );        populateElementType( "IFRAME", "HTMLIFrameElementImpl" );        populateElementType( "IMG", "HTMLImageElementImpl" );        populateElementType( "INPUT", "HTMLInputElementImpl" );        populateElementType( "INS", "HTMLModElementImpl" );        populateElementType( "ISINDEX", "HTMLIsIndexElementImpl" );        populateElementType( "LABEL", "HTMLLabelElementImpl" );        populateElementType( "LEGEND", "HTMLLegendElementImpl" );        populateElementType( "LI", "HTMLLIElementImpl" );        populateElementType( "LINK", "HTMLLinkElementImpl" );        populateElementType( "MAP", "HTMLMapElementImpl" );        populateElementType( "MENU", "HTMLMenuElementImpl" );        populateElementType( "META", "HTMLMetaElementImpl" );        populateElementType( "OBJECT", "HTMLObjectElementImpl" );        populateElementType( "OL", "HTMLOListElementImpl" );        populateElementType( "OPTGROUP", "HTMLOptGroupElementImpl" );        populateElementType( "OPTION", "HTMLOptionElementImpl" );        populateElementType( "P", "HTMLParagraphElementImpl" );        populateElementType( "PARAM", "HTMLParamElementImpl" );        populateElementType( "PRE", "HTMLPreElementImpl" );        populateElementType( "Q", "HTMLQuoteElementImpl" );        populateElementType( "SCRIPT", "HTMLScriptElementImpl" );        populateElementType( "SELECT", "HTMLSelectElementImpl" );        populateElementType( "STYLE", "HTMLStyleElementImpl" );        populateElementType( "TABLE", "HTMLTableElementImpl" );        populateElementType( "CAPTION", "HTMLTableCaptionElementImpl" );        populateElementType( "TD", "HTMLTableCellElementImpl" );        populateElementType( "TH", "HTMLTableCellElementImpl" );        populateElementType( "COL", "HTMLTableColElementImpl" );        populateElementType( "COLGROUP", "HTMLTableColElementImpl" );        populateElementType( "TR", "HTMLTableRowElementImpl" );        populateElementType( "TBODY", "HTMLTableSectionElementImpl" );        populateElementType( "THEAD", "HTMLTableSectionElementImpl" );        populateElementType( "TFOOT", "HTMLTableSectionElementImpl" );        populateElementType( "TEXTAREA", "HTMLTextAreaElementImpl" );        populateElementType( "TITLE", "HTMLTitleElementImpl" );        populateElementType( "UL", "HTMLUListElementImpl" );    }    private static void populateElementType( String tagName, String className )    {        try {            _elementTypesHTML.put( tagName,                ObjectFactory.findProviderClass("com.sun.org.apache.html.internal.dom." + className,                    HTMLDocumentImpl.class.getClassLoader(), true) );        } catch ( Exception except ) {            new RuntimeException( "HTM019 OpenXML Error: Could not find or execute class " + className + " implementing HTML element " + tagName                                  + "\n" + className + "\t" + tagName);        }    }}

⌨️ 快捷键说明

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