From 291c2fa92373b2429a0ced93e12ebca6ac76cd5d Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sun, 2 Jun 2024 12:46:21 +0200 Subject: [PATCH] simple impl to try to use https://github.com/HtmlUnit/htmlunit-neko SAX parser (see #282) --- .../org/xhtmlrenderer/resource/XMLResource.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java index 739a70dce..f3706c48c 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java @@ -66,10 +66,12 @@ public class XMLResource extends AbstractResource { private Document document; private static final XMLResourceBuilder XML_RESOURCE_BUILDER; private static boolean useConfiguredParser; + private static boolean useHtmlUnitCyberNekoParser; static { XML_RESOURCE_BUILDER = new XMLResourceBuilder(); useConfiguredParser = true; + useHtmlUnitCyberNekoParser = true; } private XMLResource(InputStream stream) { @@ -115,8 +117,8 @@ void setDocument(Document document) { public static XMLReader newXMLReader() { XMLReader xmlReader = null; - String xmlReaderClass = Configuration.valueFor("xr.load.xml-reader"); + String xmlReaderClass = Configuration.valueFor("xr.load.xml-reader"); //TODO: if it doesn't find the parser, note that in a static boolean--otherwise // you get exceptions on every load try { @@ -143,6 +145,17 @@ public static XMLReader newXMLReader() { + xmlReaderClass + ". Please check classpath. Use value 'default' in " + "FS configuration if necessary. Will now try JDK default.", ex); } + + xmlReaderClass = "org.htmlunit.cyberneko.parsers.SAXParser"; + if (xmlReaderClass != null && XMLResource.useHtmlUnitCyberNekoParser) { + try { + xmlReader = XMLReaderFactory.createXMLReader(xmlReaderClass); + } catch (Exception ex) { + XMLResource.useHtmlUnitCyberNekoParser = false; + // no need for logging here + } + } + if (xmlReader == null) { try { // JDK default @@ -189,7 +202,7 @@ private XMLResource createXMLResource(XMLResource target) { target.setElapsedLoadTime(end - start); XRLog.load("Loaded document in ~" + target.getElapsedLoadTime() + "ms"); - + target.setDocument(document); return target; }