Java 类org.w3c.dom.html.HTMLHeadElement 实例源码

项目:openid4java    文件:CyberNekoDOMHtmlParser.java   
public void parseHtml(String htmlData, HtmlResult result)
        throws DiscoveryException
{
    if (DEBUG)
        _log.debug("Parsing HTML data:\n" + htmlData);

    HTMLDocumentImpl doc = this.parseDocument(htmlData);

    NodeList heads = doc.getElementsByTagName("head");
    if (heads.getLength() != 1)
        throw new DiscoveryException(
                "HTML response must have exactly one HEAD element, "
                        + "found " + heads.getLength() + " : "
                        + heads.toString(),
                OpenIDException.DISCOVERY_HTML_PARSE_ERROR);

    HTMLHeadElement head = (HTMLHeadElement) doc.getHead();
    NodeList linkElements = head.getElementsByTagName("LINK");
    for (int i = 0, len = linkElements.getLength(); i < len; i++)
    {
        HTMLLinkElement linkElement = (HTMLLinkElement) linkElements.item(i);
        setResult(linkElement.getRel(), linkElement.getHref(), result);
    }

    if (DEBUG)
        _log.debug("HTML discovery result:\n" + result);
}
项目:openid4java    文件:CyberNekoDOMYadisHtmlParser.java   
public String getHtmlMeta(String input) throws YadisException
{
    String xrdsLocation = null;

    HTMLDocumentImpl doc = this.parseDocument(input);
    if (DEBUG)
    {
        try
        {
            _log.debug("document:\n" + OpenID4JavaDOMParser.toXmlString(doc));
        } catch (TransformerException e)
        {
            _log.debug("An exception occurs while transforming the document to string in debugging.", e);
        }
    }

    NodeList heads = doc.getElementsByTagName("head");
    if (heads.getLength() != 1)
        throw new YadisException(
                "HTML response must have exactly one HEAD element, "
                        + "found " + heads.getLength() + " : "
                        + heads.toString(),
                OpenIDException.YADIS_HTMLMETA_INVALID_RESPONSE);

    HTMLHeadElement head = (HTMLHeadElement) doc.getHead();
    NodeList metaElements = head.getElementsByTagName("META");
    if (metaElements == null || metaElements.getLength() == 0)
    {
        if (DEBUG)
            _log.debug("No <meta> element found under <html><head>. " +
            "See Yadis specification, section 6.2.5/1.");
    }
    else
    {
        for (int i = 0, len = metaElements.getLength(); i < len; i++)
        {
            HTMLMetaElement metaElement = (HTMLMetaElement) metaElements.item(i);

            String httpEquiv = metaElement.getHttpEquiv();
            if (YadisResolver.YADIS_XRDS_LOCATION.equalsIgnoreCase(httpEquiv))
            {
                if (xrdsLocation != null)
                    throw new YadisException(
                        "More than one "
                            + YadisResolver.YADIS_XRDS_LOCATION
                            + "META tags found in HEAD: "
                            + head.toString(),
                        OpenIDException.YADIS_HTMLMETA_INVALID_RESPONSE);

                xrdsLocation = metaElement.getContent();
                if (DEBUG)
                    _log.debug("Found " + YadisResolver.YADIS_XRDS_LOCATION
                        + " META tags.");
            }
        }
    }
    return xrdsLocation;
}
项目:SplitCharater    文件:HTMLDocumentImpl.java   
/**
 * 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( this, "HEAD" );
        html.insertBefore( head, html.getFirstChild() );
    }
    return (HTMLElement) head;
}