FileDocCategorySizeDatePackage
ClassicFileWriteTag.javaAPI DocExample2645Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.tags.xmp

ClassicFileWriteTag

public class ClassicFileWriteTag extends BodyTagSupport implements TryCatchFinally
This class is a custom action for writing the content of the action element's body to a file specified by an attribute, or to System.out if no file is specified. If the file name "log" is specified, the standard application log file is used.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private String
fileName
private PrintWriter
pw
Constructors Summary
Methods Summary
public intdoAfterBody()
Writes the content accumulated in bodyContent to the file.

        String content = bodyContent.getString();
        if (fileName == null) {
            System.out.println(content);
        }
        else if ("log".equals(fileName)) {
            ServletContext application = pageContext.getServletContext();
            application.log(content);
        }
        else {
            pw.print(bodyContent.getString());
        }
        return SKIP_BODY;
    
public voiddoCatch(java.lang.Throwable t)
Log the problem and rethrow the exception.

        ServletContext application = pageContext.getServletContext();
        application.log("Exception in body of " + 
            this.getClass().getName(), t);
        throw t;
    
public voiddoFinally()
Closes the file, no matter if an exception was thrown by the body evaluation or not. Note that this method is called even if an exception is thrown by doStartTag(), or any other doXXX() method. In this case, the file may not have been opened at all.

        if (pw != null) {
            pw.close();
        }
    
public intdoStartTag()
Makes sure the specified file can be opened for writing. If so, tells the container to evaluate the body.

        if (fileName != null && !"log".equals(fileName)) {
            try{
                pw = new PrintWriter(new FileWriter(fileName, true));
            }
            catch (IOException e) {
                throw new JspException("Can not open file " + fileName +
                                       " for writing", e);
            }
        }
        return EVAL_BODY_BUFFERED;
    
public voidsetFileName(java.lang.String fileName)

        this.fileName = fileName;