Get error string from Jericho SourceFormatter

179 views Asked by At

I'm using jericho's SourceFormatter to do indentation of HTML. Right now if there is an issue in my HTML formatter sends it to server console.

How can I catch error and output it into my log system (actually I want to get it as a String/Object)?

Here is example of code I use

private String indent(String html) {
    SourceFormatter formatter = new SourceFormatter(new Source(html));
    formatter.setIndentString("\t");
    formatter.setTidyTags(false);
    formatter.setCollapseWhiteSpace(true);

    return formatter.toString(); // if HTML have issues, they go to server's consol
}

LoggerProvider - represent loggin system of hericho

1

There are 1 answers

1
Dmytro Pastovenskyi On

I found that it is possible to realize Logger interface and use it as your own logger.

class LoggerCustom implements net.htmlparser.jericho.Logger {
    ...
}

And than pass it's object to Source object.

Source source = new Source(html);
LoggerCustom logger = new LoggerCustom();
source.setLogger(logger); // here I pass my Logger object.

SourceFormatter formatter = new SourceFormatter(source);

formatter.setIndentString("\t");
formatter.setTidyTags(false);
formatter.setCollapseWhiteSpace(true);

String result = formatter.toString();