Java Code Examples for org.apache.html.dom.HTMLDocumentImpl#getHead()

The following examples show how to use org.apache.html.dom.HTMLDocumentImpl#getHead() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CyberNekoDOMHtmlParser.java    From openid4java with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 2
Source File: CyberNekoDOMYadisHtmlParser.java    From openid4java with Apache License 2.0 4 votes vote down vote up
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;
}