001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.core;
040:
041: import java.io.IOException;
042: import java.io.Reader;
043:
044: import javax.servlet.jsp.JspException;
045: import javax.servlet.jsp.JspWriter;
046: import javax.servlet.jsp.PageContext;
047: import javax.servlet.jsp.tagext.BodyTagSupport;
048:
049: /**
050: * <p>Support for handlers of the <out> tag, which simply evalutes and
051: * prints the result of the expression it's passed. If the result is
052: * null, we print the value of the 'default' attribute's expression or
053: * our body (which two are mutually exclusive, although this constraint
054: * is enforced outside this handler, in our TagLibraryValidator).</p>
055: *
056: * @author Shawn Bayern
057: */
058: public class OutSupport extends BodyTagSupport {
059:
060: /*
061: * (One almost wishes XML and JSP could support "anonymous tags,"
062: * given the amount of trouble we had naming this one!) :-) - sb
063: */
064:
065: //*********************************************************************
066: // Internal state
067: protected Object value; // tag attribute
068: protected String def; // tag attribute
069: protected boolean escapeXml; // tag attribute
070: private boolean needBody; // non-space body needed?
071:
072: //*********************************************************************
073: // Construction and initialization
074:
075: /**
076: * Constructs a new handler. As with TagSupport, subclasses should
077: * not provide other constructors and are expected to call the
078: * superclass constructor.
079: */
080: public OutSupport() {
081: super ();
082: init();
083: }
084:
085: // resets local state
086: private void init() {
087: value = def = null;
088: escapeXml = true;
089: needBody = false;
090: }
091:
092: // Releases any resources we may have (or inherit)
093: public void release() {
094: super .release();
095: init();
096: }
097:
098: //*********************************************************************
099: // Tag logic
100:
101: // evaluates 'value' and determines if the body should be evaluted
102: public int doStartTag() throws JspException {
103:
104: needBody = false; // reset state related to 'default'
105: this .bodyContent = null; // clean-up body (just in case container is pooling tag handlers)
106:
107: try {
108: // print value if available; otherwise, try 'default'
109: if (value != null) {
110: out(pageContext, escapeXml, value);
111: return SKIP_BODY;
112: } else {
113: // if we don't have a 'default' attribute, just go to the body
114: if (def == null) {
115: needBody = true;
116: return EVAL_BODY_BUFFERED;
117: }
118:
119: // if we do have 'default', print it
120: if (def != null) {
121: // good 'default'
122: out(pageContext, escapeXml, def);
123: }
124: return SKIP_BODY;
125: }
126: } catch (IOException ex) {
127: throw new JspException(ex.toString(), ex);
128: }
129: }
130:
131: // prints the body if necessary; reports errors
132: public int doEndTag() throws JspException {
133: try {
134: if (!needBody)
135: return EVAL_PAGE; // nothing more to do
136:
137: // trim and print out the body
138: if (bodyContent != null && bodyContent.getString() != null)
139: out(pageContext, escapeXml, bodyContent.getString()
140: .trim());
141: return EVAL_PAGE;
142: } catch (IOException ex) {
143: throw new JspException(ex.toString(), ex);
144: }
145: }
146:
147: //*********************************************************************
148: // Public utility methods
149:
150: /**
151: * Outputs <tt>text</tt> to <tt>pageContext</tt>'s current JspWriter.
152: * If <tt>escapeXml</tt> is true, performs the following substring
153: * replacements (to facilitate output to XML/HTML pages):
154: *
155: * & -> &
156: * < -> <
157: * > -> >
158: * " -> "
159: * ' -> '
160: *
161: * See also Util.escapeXml().
162: */
163: public static void out(PageContext pageContext, boolean escapeXml,
164: Object obj) throws IOException {
165: JspWriter w = pageContext.getOut();
166: if (!escapeXml) {
167: // write chars as is
168: if (obj instanceof Reader) {
169: Reader reader = (Reader) obj;
170: char[] buf = new char[4096];
171: int count;
172: while ((count = reader.read(buf, 0, 4096)) != -1) {
173: w.write(buf, 0, count);
174: }
175: } else {
176: w.write(obj.toString());
177: }
178: } else {
179: // escape XML chars
180: if (obj instanceof Reader) {
181: Reader reader = (Reader) obj;
182: char[] buf = new char[4096];
183: int count;
184: while ((count = reader.read(buf, 0, 4096)) != -1) {
185: writeEscapedXml(buf, count, w);
186: }
187: } else {
188: String text = obj.toString();
189: writeEscapedXml(text.toCharArray(), text.length(), w);
190: }
191: }
192: }
193:
194: /**
195: *
196: * Optimized to create no extra objects and write directly
197: * to the JspWriter using blocks of escaped and unescaped characters
198: *
199: */
200: private static void writeEscapedXml(char[] buffer, int length,
201: JspWriter w) throws IOException {
202: int start = 0;
203:
204: for (int i = 0; i < length; i++) {
205: char c = buffer[i];
206: if (c <= Util.HIGHEST_SPECIAL) {
207: char[] escaped = Util.specialCharactersRepresentation[c];
208: if (escaped != null) {
209: // add unescaped portion
210: if (start < i) {
211: w.write(buffer, start, i - start);
212: }
213: // add escaped xml
214: w.write(escaped);
215: start = i + 1;
216: }
217: }
218: }
219: // add rest of unescaped portion
220: if (start < length) {
221: w.write(buffer, start, length - start);
222: }
223: }
224: }
|