FileDocCategorySizeDatePackage
FileWriteTag.javaAPI DocExample2119Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.tags

FileWriteTag

public class FileWriteTag extends SimpleTagSupport
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
2.0

Fields Summary
private String
fileName
Constructors Summary
Methods Summary
public voiddoTag()

        JspFragment body = getJspBody();
        if (body == null) {
	    throw new JspTagException("'fileWrite' used without a body");
	}
	
        PrintWriter pw = null;
        if (fileName != null && !"log".equals(fileName)) {
            try{
                pw = new PrintWriter(new FileWriter(fileName, true));
            }
            catch (IOException e) {
                throw new JspTagException("Can not open file " + fileName +
                                          " for writing", e);
            }
        }

        ServletContext application = 
            ((PageContext) getJspContext()).getServletContext();
        StringWriter evalResult = new StringWriter();
        try {
            body.invoke(evalResult);
            if (fileName == null) {
                System.out.println(evalResult);
            }
            else if ("log".equals(fileName)) {
                application.log(evalResult.toString());
            }
            else {
                pw.print(evalResult);
            }
        }
        catch (Throwable t) {
            String msg = "Exception in body of " +  this.getClass().getName();
            application.log(msg, t);
            throw new JspTagException(msg, t);
        }
        finally {
            if (pw != null) {
                pw.close();
            }
        }
    
public voidsetFileName(java.lang.String fileName)

        this.fileName = fileName;