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();
}
}