webwork/src/main/webwork/view/taglib BasicPropertyTag.java,1.6,1.7
[email protected] Thu, 06 Nov 2003 06:23:56 -0800
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/opensymphony/webwork/src/main/webwork/view/taglib In directory sc8-pr-cvs1:/tmp/cvs-serv19662 Modified Files: BasicPropertyTag.java Log Message: Check if bodyContent is null to avoid unnecessary exceptions from being thrown on the Resin container. Index: BasicPropertyTag.java =================================================================== RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/view/taglib/BasicPropertyTag.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- BasicPropertyTag.java 26 May 2003 03:40:56 -0000 1.6 +++ BasicPropertyTag.java 6 Nov 2003 14:23:54 -0000 1.7 @@ -1,157 +1,160 @@ -/* - * WebWork, Web Application Framework - * - * Distributable under Apache license. - * See terms of license at opensource.org - */ -package webwork.view.taglib; - -import webwork.util.TextUtil; -import webwork.util.BeanUtil; - -import javax.servlet.ServletException; -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.JspTagException; -import javax.servlet.jsp.PageContext; -import java.util.Iterator; - -import org.apache.commons.logging.*; - -/** - * Access the value of a named property. By default (implicitly), - * this tag will escape its contents if it does *not* have a body. - * If it does have a body, the tag will not escape the contents. - * You can explicitly tell the tag to escape or not. - * Quoted text that is escaped will have its quotes stripped off. - * - * @author Rickard �berg ([email protected]) - * @author Matt Baldree ([email protected]) - * @version $Revision$ - */ -public class BasicPropertyTag - extends WebWorkBodyTagSupport -{ - protected static Log log = LogFactory.getLog(BasicPropertyTag.class); - - // Attributes ---------------------------------------------------- - protected String valueAttr; - protected Boolean escape; - protected boolean hadBody; - - // Public -------------------------------------------------------- - public void setValue(String inName) - { - valueAttr = inName; - } - - public void setEscape(boolean inEscape) - { - escape = new Boolean(inEscape); - } - - // BodyTag implementation ---------------------------------------- - public int doStartTag() throws JspException - { - hadBody = false; - Object value = findValue(valueAttr); - getStack().pushValue(value); - - String id = getId(); - if (id != null && value != null) { - pageContext.setAttribute(id, value); - pageContext.setAttribute(id, value, PageContext.REQUEST_SCOPE); - } - return EVAL_BODY_BUFFERED; - } - - public int doAfterBody() throws JspException - { - // This is a workaround for a bug in Jasper. - // http://znutar.cortexity.com/BugRatViewer/ShowReport/652 - // Resin 1.2.1 also has this bug (i.e. body is called even though it is empty) - String body = null; - try { - body = getBodyContent().getString(); - } catch (Exception e) { - //do nothing - leave body as "" - } - - //Added null check - Fixes WW-195 on Resin. - hadBody = body != null && body.length() != 0; - - if (hadBody) { - try { - if (shouldEscape()) { - body = TextUtil.escapeHTML(body); - } - bodyContent.getEnclosingWriter().write(body); - } catch (java.io.IOException e) { - throw new JspTagException("Could not show attribute " + valueAttr + ":" + e); - } - } - return SKIP_BODY; - } - - public int doEndTag() throws JspException - { - Object value = getStack().popValue(); - - if (!hadBody) { - // No body used. This means that we want to print the parameter out - try { - //LogFactory.getLog(this.getClass()).debug("Property value:"+value); - if (value != null && getId() == null) - if (value instanceof Iterator) { - Iterator enum = (Iterator) value; - if (enum.hasNext()) { - if (shouldEscape()) { - pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(enum.next()))); - } else { - pageContext.getOut().write(BeanUtil.toStringValue(enum.next())); - } - } - } else if (value instanceof char[]) { - if (shouldEscape()) { - pageContext.getOut().write(TextUtil.escapeHTML(String.valueOf((char[]) value))); - } else { - pageContext.getOut().write(String.valueOf((char[]) value)); - } - } else { - if (shouldEscape()) { - pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(value))); - } else { - pageContext.getOut().write(BeanUtil.toStringValue(value)); - } - } - else - pageContext.getOut().write(""); // Printing out null gives no output - } catch (Throwable t) { - String msg = "Could not show value: " + valueAttr; - log.error(msg, t); - throw new JspException(msg + ", throwable: " + t); - } - } - - return EVAL_PAGE; - } - - /** - * The BasicPropertyTag should escape IF the escape value has been set to true. At - * all other times it will false (ie not escape data). - * - * CHANGE THIS METHOD ON PAIN OF DEATH -mike :) - * - * @see PropertyTag#shouldEscape() - */ - protected boolean shouldEscape() - { - if (escape == null) - { - return false; - } - - return escape.booleanValue(); - } -} - - +/* + * WebWork, Web Application Framework + * + * Distributable under Apache license. + * See terms of license at opensource.org + */ +package webwork.view.taglib; + +import webwork.util.TextUtil; +import webwork.util.BeanUtil; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.JspTagException; +import javax.servlet.jsp.PageContext; +import java.util.Iterator; + +import org.apache.commons.logging.*; + +/** + * Access the value of a named property. By default (implicitly), + * this tag will escape its contents if it does *not* have a body. + * If it does have a body, the tag will not escape the contents. + * You can explicitly tell the tag to escape or not. + * Quoted text that is escaped will have its quotes stripped off. + * + * @author Rickard Öberg ([email protected]) + * @author Matt Baldree ([email protected]) + * @version $Revision$ + */ +public class BasicPropertyTag + extends WebWorkBodyTagSupport +{ + protected static Log log = LogFactory.getLog(BasicPropertyTag.class); + + // Attributes ---------------------------------------------------- + protected String valueAttr; + protected Boolean escape; + protected boolean hadBody; + + // Public -------------------------------------------------------- + public void setValue(String inName) + { + valueAttr = inName; + } + + public void setEscape(boolean inEscape) + { + escape = new Boolean(inEscape); + } + + // BodyTag implementation ---------------------------------------- + public int doStartTag() throws JspException + { + hadBody = false; + Object value = findValue(valueAttr); + getStack().pushValue(value); + + String id = getId(); + if (id != null && value != null) { + pageContext.setAttribute(id, value); + pageContext.setAttribute(id, value, PageContext.REQUEST_SCOPE); + } + return EVAL_BODY_BUFFERED; + } + + public int doAfterBody() throws JspException + { + // This is a workaround for a bug in Jasper. + // http://znutar.cortexity.com/BugRatViewer/ShowReport/652 + // Resin 1.2.1 also has this bug (i.e. body is called even though it is empty) + + // If the bodyContent is null then just return + if (bodyContent == null) + return SKIP_BODY; + String body = null; + try { + body = bodyContent.getString(); + } catch (Exception e) { + //do nothing - leave body as null + } + + //Added null check - Fixes WW-195 on Resin. + hadBody = body != null && body.length() != 0; + + if (hadBody) { + try { + if (shouldEscape()) { + body = TextUtil.escapeHTML(body); + } + bodyContent.getEnclosingWriter().write(body); + } catch (java.io.IOException e) { + throw new JspTagException("Could not show attribute " + valueAttr + ":" + e); + } + } + return SKIP_BODY; + } + + public int doEndTag() throws JspException + { + Object value = getStack().popValue(); + + if (!hadBody) { + // No body used. This means that we want to print the parameter out + try { + //LogFactory.getLog(this.getClass()).debug("Property value:"+value); + if (value != null && getId() == null) + if (value instanceof Iterator) { + Iterator enum = (Iterator) value; + if (enum.hasNext()) { + if (shouldEscape()) { + pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(enum.next()))); + } else { + pageContext.getOut().write(BeanUtil.toStringValue(enum.next())); + } + } + } else if (value instanceof char[]) { + if (shouldEscape()) { + pageContext.getOut().write(TextUtil.escapeHTML(String.valueOf((char[]) value))); + } else { + pageContext.getOut().write(String.valueOf((char[]) value)); + } + } else { + if (shouldEscape()) { + pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(value))); + } else { + pageContext.getOut().write(BeanUtil.toStringValue(value)); + } + } + else + pageContext.getOut().write(""); // Printing out null gives no output + } catch (Throwable t) { + String msg = "Could not show value: " + valueAttr; + log.error(msg, t); + throw new JspException(msg + ", throwable: " + t); + } + } + + return EVAL_PAGE; + } + + /** + * The BasicPropertyTag should escape IF the escape value has been set to true. At + * all other times it will false (ie not escape data). + * + * CHANGE THIS METHOD ON PAIN OF DEATH -mike :) + * + * @see PropertyTag#shouldEscape() + */ + protected boolean shouldEscape() + { + if (escape == null) + { + return false; + } + + return escape.booleanValue(); + } +} + + ------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/