Getting NULL pointer exception net.sf.saxon.event.ReceivingContentHandler.startElement in DaisyDiff

早过忘川 提交于 2019-12-25 18:34:11

问题


I'm using DaizyDIff library to compare two html files. I wrote a java code to implement the DaizyDiff. but while running I'm getting NULL pointer exception on net.sf.saxon.event.ReceivingContentHandler.startElement

I have tries multiple approach on SAXTransformerFactory , but I couldn't figure out

public static void daisyDiffTest() throws Exception {
    String html1 = "<html><body>var v2</body></html>";
    String html2 = "<html>  \n  <body>  \n  Hello world  \n  </body>  \n  </html>";

    try {
        StringWriter finalResult = new StringWriter();
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler result = tf.newTransformerHandler();
        result.getTransformer().setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        result.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        result.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
        result.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        result.setResult(new StreamResult(finalResult));

        ContentHandler postProcess = result;
        Locale val = Locale.ENGLISH;
        DaisyDiff.diffHTML(new InputSource(new StringReader(html1)), new InputSource(new StringReader(html2)),
                postProcess, "test", val);
        System.out.println(finalResult.toString());
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Expected result would be diff in the HTML file.


回答1:


It's hard to know without knowing what DaisyDiff is, or what calls it makes. It's quite possible that it's not tested or supported for use with Saxon.

The format of data passed to the startElement() event in a SAX ContentHandler depends on the configuration options of the XML parser, and the problem when Saxon is invoked as a ContentHandler in this way is that it has no way of discovering what configuration options the parser is using.

As stated in the Javadoc documentation here: http://www.saxonica.com/documentation/index.html#!javadoc/net.sf.saxon.event/ReceivingContentHandler@startElement if the events emitted by the parser don't correspond to what an appropriately configured parser would emit, the ReceivingContentHandler will fail in unpredictable ways.

Posting the stack trace of the exception might be useful.



来源:https://stackoverflow.com/questions/54598683/getting-null-pointer-exception-net-sf-saxon-event-receivingcontenthandler-starte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!