简体   繁体   English

我可以配置JTidy来忽略某些错误和警告吗?

[英]Can I configure JTidy to ignore certain errors and warnings?

I am using JTidy to validate snippets of HTML generated in Java a rendering class. 我正在使用JTidy来验证在Java中生成的HTML的渲染类的片段。 I would like to ignore certain warnings and errors . 我想忽略某些警告 和错误

(EDIT: On second thoughts I might not want to suppress errors ) (编辑:再想一想,我可能不想压制错误

For example, the following snippet that is generated: 例如,生成以下代码段:

<img src='/images/icon.gif'>

results in this warning: 导致此警告:

line 5 column 7 - Warning: img lacks "alt" attribute

Can I configure JTidy to ignore specific checks such as this one? 我可以配置JTidy来忽略这样的特定检查吗?

The method I use to check is: 我用来检查的方法是:

public static boolean isValidHtml(String htmlSnippet) {
    String untestedHtml = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><HTML>" +
            "<head><title>Wrapper HTML For Testing</title></head>" +
            htmlSnippet +
            "</HTML>";

    final Tidy tidy = new Tidy();
    final Writer writer = new StringWriter();
    tidy.setErrout(new PrintWriter(writer, true));

    tidy.parseDOM(new StringReader(untestedHtml), writer);

    if (tidy.getParseErrors() > 0 || tidy.getParseWarnings() > 0) {
        System.err.println(writer);
        return false;
    }
    return true;
}

Use the configuration class to do this: 使用配置类执行此操作:

  Tidy tidy = new Tidy();
  tidy.setShowErrors(0);
  tidy.setForceOutput(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM