下面列出了org.w3c.dom.html.HTMLLinkElement#org.apache.html.dom.HTMLDocumentImpl 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private HTMLDocumentImpl parseDocument(String htmlData) throws DiscoveryException
{
OpenID4JavaDOMParser parser = new OpenID4JavaDOMParser();
try
{
parser.parse(OpenID4JavaDOMParser.createInputSource(htmlData));
}
catch (Exception e)
{
throw new DiscoveryException("Error parsing HTML message",
OpenIDException.DISCOVERY_HTML_PARSE_ERROR, e);
}
if (parser.isIgnoredHeadStartElement())
{
throw new DiscoveryException(
"HTML response must have exactly one HEAD element.",
OpenIDException.DISCOVERY_HTML_PARSE_ERROR);
}
return (HTMLDocumentImpl) parser.getDocument();
}
private HTMLDocumentImpl parseDocument(String htmlData) throws YadisException
{
OpenID4JavaDOMParser parser = new OpenID4JavaDOMParser();
try
{
parser.parse(OpenID4JavaDOMParser.createInputSource(htmlData));
}
catch (Exception e)
{
throw new YadisException("Error parsing HTML message",
OpenIDException.YADIS_HTMLMETA_INVALID_RESPONSE, e);
}
if (parser.isIgnoredHeadStartElement())
{
throw new YadisException("HTML response must have exactly one HEAD element.",
OpenIDException.YADIS_HTMLMETA_INVALID_RESPONSE);
}
return (HTMLDocumentImpl) parser.getDocument();
}
@Test
public void testFirstElement() {
final HTMLDocumentImpl owner = new HTMLDocumentImpl();
final HTMLBodyElement n = new HTMLBodyElementImpl(owner, "body");
final HTMLDivElement d = new HTMLDivElementImpl(owner, "div");
n.appendChild(d);
Assert.assertNull(OdfToolkitUtil.getFirstElement(n, "ddd"));
Assert.assertEquals(d, OdfToolkitUtil.getFirstElement(n, "div"));
}
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);
}
private DocumentFragment parseTagSoup(InputSource input) throws Exception {
HTMLDocumentImpl doc = new HTMLDocumentImpl();
DocumentFragment frag = doc.createDocumentFragment();
DOMBuilder builder = new DOMBuilder(doc, frag);
org.ccil.cowan.tagsoup.Parser reader = new org.ccil.cowan.tagsoup.Parser();
reader.setContentHandler(builder);
reader.setFeature(org.ccil.cowan.tagsoup.Parser.ignoreBogonsFeature, true);
reader.setFeature(org.ccil.cowan.tagsoup.Parser.bogonsEmptyFeature, false);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", builder);
reader.parse(input);
return frag;
}
public static DocumentFragment fromJsoup(
org.jsoup.nodes.Document jsoupDocument) {
HTMLDocumentImpl htmlDoc = new HTMLDocumentImpl();
htmlDoc.setErrorChecking(false);
DocumentFragment fragment = htmlDoc.createDocumentFragment();
org.jsoup.nodes.Element rootEl = jsoupDocument.child(0); // skip the
// #root node
NodeTraversor.traverse(new W3CBuilder(htmlDoc, fragment), rootEl);
return fragment;
}
public static Node parse( String content ) throws SAXException, IOException {
DOMFragmentParser parser = new DOMFragmentParser();
HTMLDocument document = new HTMLDocumentImpl();
DocumentFragment fragment = document.createDocumentFragment();
InputSource is = new InputSource( new StringReader( content ) );
parser.parse( is, fragment );
return fragment;
}
@Test(expected = NullPointerException.class)
public void testAttributeNull() {
final Node n = new HTMLBodyElementImpl(new HTMLDocumentImpl(), "name");
OdfToolkitUtil.getAttribute(n, "attr");
}
@Test
public void testAttribute() {
final HTMLBodyElement n = new HTMLBodyElementImpl(new HTMLDocumentImpl(), "body");
n.setBgColor("color");
Assert.assertEquals("color", OdfToolkitUtil.getAttribute(n, "bgcolor"));
}
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;
}
public W3CBuilder(HTMLDocumentImpl doc, DocumentFragment fragment) {
this.fragment = fragment;
this.doc = doc;
}