All pastes #986253 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-15 17:32 UTC
rendered paste body
/*
 * @{#}IdiotsAtSunStdErrRedirect.java
 */

package com.xsltformation.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;

/**
 *
 * @author Matt
 */
public class IdiotsAtSunStdErrRedirect {
    
    private PrintStream old;
    private final File f = new File("c:/tempStdErr.txt");
    
    /**
     * Instantiate this thing.
     */
    public IdiotsAtSunStdErrRedirect() {
        //Keep reference so we don't break System.
        old = System.err;
        try {
            System.setErr(new PrintStream(f));
        } catch (Exception e) {
            //The file does not exist, which is impossible,IMPOSSIBLE I TELL YA!
        }
    }
    
    /**
     * Destroy this bad boy.
     */
    public void destroy() {
        f.delete();
        System.setErr(old);
    }
    
    /**
     * Since the retards who coded Xalan thought it would be great to 
     * output things in the err stdout without giving the programmer 
     * implementing the API a chance to manipulate the message, we need
     * this work around to fetch it.
     * 
     * @return The first line of the System.err when the stack dump occurs.
     */
    public String getFirstLineOfStdErr() {
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            final String line = br.readLine();
            br.close();
            f.delete();
            return line;
        } catch (Exception e) {
            e.printStackTrace();
            //If it fucks up to this point, return null and screw it.
            return null;
        }
    }

}