Skip to content

Commit

Permalink
returns iterator only for DocumentTraversal instances so that other p…
Browse files Browse the repository at this point in the history
…arsers (like Shani Xml Parser) do not fail with ClassCastException
  • Loading branch information
Kunnar Klauks, Marek Kusmin committed Jan 16, 2017
1 parent ff6b2e8 commit e0a9c36
Showing 1 changed file with 10 additions and 2 deletions.
Expand Up @@ -19,12 +19,14 @@
*/
package org.xhtmlrenderer.pdf;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
Expand Down Expand Up @@ -143,6 +145,10 @@ private HTMLOutline(int level, String name, HTMLOutline parent) {
public static List<Bookmark> generate(Element context, Box box) {
NodeIterator iterator = NestedSectioningFilter.iterator(context);

if (iterator == null) {
return Collections.emptyList();
}

HTMLOutline root = new HTMLOutline();
HTMLOutline current = root;
Map<Element,Bookmark> map = new IdentityHashMap();
Expand Down Expand Up @@ -214,8 +220,10 @@ private static class NestedSectioningFilter implements NodeFilter {
static final NestedSectioningFilter INSTANCE = new NestedSectioningFilter();

static NodeIterator iterator(Element root) {
return ((DocumentTraversal) root.getOwnerDocument())
.createNodeIterator(root, SHOW_ELEMENT, INSTANCE, true);
Document ownerDocument = root.getOwnerDocument();
return (ownerDocument instanceof DocumentTraversal)
? ((DocumentTraversal) ownerDocument).createNodeIterator(root, SHOW_ELEMENT, INSTANCE, true)
: null;
}

@Override
Expand Down

0 comments on commit e0a9c36

Please sign in to comment.