3

I am using JTidy to validate snippets of HTML generated in Java a rendering class. 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?

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;
}
vegemite4me
  • 6,621
  • 5
  • 53
  • 79

1 Answers1

0

Use the configuration class to do this:

  Tidy tidy = new Tidy();
  tidy.setShowErrors(0);
  tidy.setForceOutput(true);
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265