JTidy: How to catch output

R

Ronald Fischer

I need to parse a fragment of HTML code for containing errors.
On this ng, JTidy was suggested to me as a solution.

The problem seems to be that the Tidy.parse() writes its errors and
warnings to standard error, but I need to catch them before presenting
them to the user, in order to filter out certain errors which we don't
regard as errors. I thought that Tidy.setErrfile could do the job.
Assuming that the name of the file containing the HTML code to parse
is stored in variable fn, I tried the following code:

Tidy tidy=new Tidy();
tidy.setErrfile("errout.txt");
tidy.setXHTML(false);
tidy.setDocType("strict");
tidy.setShowWarnings(true);
tidy.parse(new FileInputStream(new File(fn)),null);

But Tidy still writes to standard error, and the file errout.txt is
not created. What am I doing wrong?

Ronald
 
T

TechBookReport

Ronald said:
I need to parse a fragment of HTML code for containing errors.
On this ng, JTidy was suggested to me as a solution.

The problem seems to be that the Tidy.parse() writes its errors and
warnings to standard error, but I need to catch them before presenting
them to the user, in order to filter out certain errors which we don't
regard as errors. I thought that Tidy.setErrfile could do the job.
Assuming that the name of the file containing the HTML code to parse
is stored in variable fn, I tried the following code:

Tidy tidy=new Tidy();
tidy.setErrfile("errout.txt");
tidy.setXHTML(false);
tidy.setDocType("strict");
tidy.setShowWarnings(true);
tidy.parse(new FileInputStream(new File(fn)),null);

But Tidy still writes to standard error, and the file errout.txt is
not created. What am I doing wrong?

Ronald

I've not used JTidy so can't comment on that, but you can always
redirect System.err:

System.setErr(new FileOutputStream("errout.txt"));

Pan
===================================================================
TechBookReport Java: http://www.techbookreport.com/JavaIndex.html
 
T

Thomas Weidenfeller

Ronald said:
But Tidy still writes to standard error, and the file errout.txt is
not created. What am I doing wrong?

Looking into the source for one minute tells me that setErrfile() is
only interpreted by the main() method, and that you should better use
setErrout().

You probably might want to supply a patch to the project, either one
which fixes the documentation or a patch which changes the setErrfile()
method implementation to something less simple.

/Thomas
 
Joined
Nov 8, 2010
Messages
1
Reaction score
0
JTidy relies on your PrintWriter to auto flush on println (an other methods). Hence the following code will not work, because the allocated PrintWriter is not set to auto-flush:

Code:
PrintWriter errorWriter = new PrintWriter(filename);
tidy.setErrout(errorWriter);

Instead you should use
Code:
//Note that we are creating a FileWriter, which enables us to call another
//constructor where we can set autoFlush to true
PrintWriter errorWriter = new PrintWriter(new FileWriter(errfile), true);
tidy.setErrout(errorWriter);

It took me quite a while to figure this out, so I hope anybody else in the same situation will be helped by the above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top