如何调用DaisyDiff来比较两个HTML文件?

时间:2021-10-12 12:02:47

I need to create a diff between two HTML documents in my app. I found a library called DaisyDiff that can do it. It has an API that looks like this:

我需要在我的应用程序中的两个HTML文档之间创建差异。我找到了一个名为DaisyDiff的库,可以做到这一点。它有一个如下所示的API:

/**
 * Diffs two html files, outputting the result to the specified consumer.
 */
public static void diffHTML(InputSource oldSource, InputSource newSource,
                            ContentHandler consumer, String prefix, Locale locale)
        throws SAXException, IOException

I know absolutely nothing about SAX and I can't figure out what to pass as the third argument. After poking through https://code.google.com/p/daisydiff/source/browse/trunk/daisydiff/src/java/org/outerj/daisy/diff/Main.java I wrote this method:

我对SAX一无所知,我无法弄清楚要传递什么作为第三个参数。在浏览https://code.google.com/p/daisydiff/source/browse/trunk/daisydiff/src/java/org/outerj/daisy/diff/Main.java后,我写了这个方法:

@Override
    protected String doInBackground(String... params)
    {
        try {
            String oldFileName = params[0],
                    newFileName = params[1];
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            FileInputStream oldis = null, newis = null;
            oldis = openFileInput(oldFileName);
            newis = openFileInput(newFileName);

            SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
                    .newInstance();

            TransformerHandler result = tf.newTransformerHandler();
            result.setResult(new StreamResult(os));
            DaisyDiff.diffHTML(new InputSource(oldis), new InputSource(newis), result, "", Locale.getDefault());

            Log.d("diff", "output length = " + os.size());
            return os.toString("Utf-8");

        }catch (Exception e){
            return e.toString();
        }
    }

I have no idea if that even makes sense. It doesn't work, nothing is written to the output. Please help me with this. Thanks in advance.

我不知道这是否有意义。它不起作用,没有任何内容写入输出。请帮我解决一下这个。提前致谢。

1 个解决方案

#1


1  

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?

根据HtmlTestFixture.diff的编码方式(在DaisyDiff的src / test / java中,你需要给出关于如何格式化结果的说明。你试过添加下面的setOutputProperty(...)调用吗?

@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.

这样做,我的输出如下。现在,我可以将其粘贴到HTML文件中,包含正确的css和js文件,并具有漂亮的输出。

<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>

var v2 Hello world

#1


1  

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?

根据HtmlTestFixture.diff的编码方式(在DaisyDiff的src / test / java中,你需要给出关于如何格式化结果的说明。你试过添加下面的setOutputProperty(...)调用吗?

@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.

这样做,我的输出如下。现在,我可以将其粘贴到HTML文件中,包含正确的css和js文件,并具有漂亮的输出。

<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>

var v2 Hello world