How do I call DaisyDiff to compare two HTML files?

你离开我真会死。 提交于 2019-12-04 14:05:52

According to how HtmlTestFixture.diff is coded up (inside src/test/java of DaisyDiff, you need to give it instructions on how the result should be formatted. Have you tried adding the below setOutputProperty(...) calls?

@Test 
//@Test comes from TestNG and is not related to DaisyDiff
public 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; 
        DaisyDiff.diffHTML(new InputSource(new StringReader(html1)), new InputSource(new StringReader(html2)), postProcess, "test", Locale.ENGLISH);
        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();
    }
}

Done this way, my output is as follows. Now I can stick this into an HTML file, include the right css and js files and have a pretty output.

<span class="diff-html-removed" id="removed-test-0" previous="first-test" changeId="removed-test-0" next="added-test-0">var v2</span><span class="diff-html-added" previous="removed-test-0" changeId="added-test-0" next="last-test"> </span><span class="diff-html-added" id="added-test-0" previous="removed-test-0" changeId="added-test-0" next="last-test">Hello world </span>

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