FileDocCategorySizeDatePackage
OutSupport.javaAPI DocGlassfish v2 API7819Sat May 05 19:17:52 BST 2007org.apache.taglibs.standard.tag.common.core

OutSupport

public class OutSupport extends javax.servlet.jsp.tagext.BodyTagSupport

Support for handlers of the <out> tag, which simply evalutes and prints the result of the expression it's passed. If the result is null, we print the value of the 'default' attribute's expression or our body (which two are mutually exclusive, although this constraint is enforced outside this handler, in our TagLibraryValidator).

author
Shawn Bayern

Fields Summary
protected Object
value
protected String
def
protected boolean
escapeXml
private boolean
needBody
Constructors Summary
public OutSupport()
Constructs a new handler. As with TagSupport, subclasses should not provide other constructors and are expected to call the superclass constructor.

        super();
        init();
    
Methods Summary
public intdoEndTag()

      try {
	if (!needBody)
	    return EVAL_PAGE;		// nothing more to do

	// trim and print out the body
	if (bodyContent != null && bodyContent.getString() != null)
            out(pageContext, escapeXml, bodyContent.getString().trim());
	return EVAL_PAGE;
      } catch (IOException ex) {
	throw new JspException(ex.toString(), ex);
      }
    
public intdoStartTag()


      needBody = false;			// reset state related to 'default'
      this.bodyContent = null;  // clean-up body (just in case container is pooling tag handlers)
      
      try {
	// print value if available; otherwise, try 'default'
	if (value != null) {
            out(pageContext, escapeXml, value);
	    return SKIP_BODY;
	} else {
	    // if we don't have a 'default' attribute, just go to the body
	    if (def == null) {
		needBody = true;
		return EVAL_BODY_BUFFERED;
	    }

	    // if we do have 'default', print it
	    if (def != null) {
		// good 'default'
                out(pageContext, escapeXml, def);
	    }
	    return SKIP_BODY;
	}
      } catch (IOException ex) {
	throw new JspException(ex.toString(), ex);
      }
    
private voidinit()

        value = def = null;
        escapeXml = true;
	needBody = false;
    
public static voidout(javax.servlet.jsp.PageContext pageContext, boolean escapeXml, java.lang.Object obj)
Outputs text to pageContext's current JspWriter. If escapeXml is true, performs the following substring replacements (to facilitate output to XML/HTML pages): & -> & < -> < > -> > " -> " ' -> ' See also Util.escapeXml().

        JspWriter w = pageContext.getOut();
	if (!escapeXml) {
            // write chars as is
            if (obj instanceof Reader) {
                Reader reader = (Reader)obj;
                char[] buf = new char[4096];
                int count;
                while ((count=reader.read(buf, 0, 4096)) != -1) {
                    w.write(buf, 0, count);
                }
            } else {
                w.write(obj.toString());
            }
        } else {
            // escape XML chars
            if (obj instanceof Reader) {
                Reader reader = (Reader)obj;
                char[] buf = new char[4096];
                int count;
                while ((count = reader.read(buf, 0, 4096)) != -1) {
                    writeEscapedXml(buf, count, w);
                }
            } else {
                String text = obj.toString();
                writeEscapedXml(text.toCharArray(), text.length(), w);
            }
        }
    
public voidrelease()

        super.release();
        init();
    
private static voidwriteEscapedXml(char[] buffer, int length, javax.servlet.jsp.JspWriter w)
Optimized to create no extra objects and write directly to the JspWriter using blocks of escaped and unescaped characters

        int start = 0;

        for (int i = 0; i < length; i++) {
            char c = buffer[i];
            if (c <= Util.HIGHEST_SPECIAL) {
                char[] escaped = Util.specialCharactersRepresentation[c];
                if (escaped != null) {
                    // add unescaped portion
                    if (start < i) {
                        w.write(buffer,start,i-start);
                    }
                    // add escaped xml
                    w.write(escaped);
                    start = i + 1;
                }
            }
        }
        // add rest of unescaped portion
        if (start < length) {
            w.write(buffer,start,length-start);
        }