我如何调用DaisyDiff来比较两个HTML文件? [英] How do I call DaisyDiff to compare two HTML files?

查看:285
本文介绍了我如何调用DaisyDiff来比较两个HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  / ** 
*区分两个html文件,输出结果为指定的消费者。
* /
public static void diffHTML(InputSource oldSource,InputSource newSource,
ContentHandler consumer,String prefix,Locale locale)
抛出SAXException,IOException

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

  @Override 
保护字符串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结果= 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();
}
}

我不知道这是否合理。它不起作用,没有任何内容写入输出。请帮我解决一下这个。根据 HtmlTestFixture.diff 的编码方式进行编码。 src / test / java DaisyDiff ),你需要给它指示如何格式化结果。你尝试添加下面的 setOutputProperty(...)调用吗?

  @Test 
// @ Test来自TestNG,与DaisyDiff无关
public void daisyDiffTest()抛出Exception {
String html1 =< html>< body> var v2< / body>< / html>;
String html2 =< html> \\\
< body> \\\
Hello world \\\
< / body> \\\
< / HTML>;

try {
StringWriter finalResult = new StringWriter();
SAXTransformerFactory tf =(SAXTransformerFactory)SAXTransformerFactory.newInstance();
TransformerHandler result = tf .newTransformerHandler();
result.getTransformer()。s etOutputProperty(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(新的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自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

完成这项工作后,我的输出如下。现在我可以将它粘贴到一个HTML文件中,包含正确的css和js文件并且有一个漂亮的输出。

< span class = diff-html-removedid =removed-test-0previous =first-testchangeId =removed-test-0next =added-test-0> var v2< / span>< span class =diff-html-addedprevious =removed-test-0changeId =added-test-0next =last-test> < / span>< span class =diff-html-addedid =added-test-0previous =removed-test-0changeId =added-test-0next =last-test > Hello world< / span>


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:

/**
 * 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:

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

解决方案

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>

这篇关于我如何调用DaisyDiff来比较两个HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆