JTidy: How to catch output

Discussion in 'Java' started by Ronald Fischer, Mar 17, 2005.

  1. 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
     
    Ronald Fischer, Mar 17, 2005
    #1
    1. Advertising

  2. Ronald Fischer wrote:
    > 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
     
    TechBookReport, Mar 17, 2005
    #2
    1. Advertising

  3. Ronald Fischer wrote:
    > 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

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Mar 17, 2005
    #3
  4. Ronald Fischer

    jakobgt

    Joined:
    Nov 8, 2010
    Messages:
    1
    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.
     
    jakobgt, Nov 8, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike

    USE OF JTIDY SYSTEM!!

    mike, May 5, 2004, in forum: Java
    Replies:
    0
    Views:
    388
  2. mike
    Replies:
    0
    Views:
    513
  3. mike
    Replies:
    1
    Views:
    467
  4. mike
    Replies:
    2
    Views:
    480
  5. mike

    A Jtidy question

    mike, Oct 26, 2004, in forum: Java
    Replies:
    2
    Views:
    456
Loading...

Share This Page