1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * Portions Copyright Apache Software Foundation.
7 *
8 * The contents of this file are subject to the terms of either the GNU
9 * General Public License Version 2 only ("GPL") or the Common Development
10 * and Distribution License("CDDL") (collectively, the "License"). You
11 * may not use this file except in compliance with the License. You can obtain
12 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
13 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
18 * Sun designates this particular file as subject to the "Classpath" exception
19 * as provided by Sun in the GPL Version 2 section of the License file that
20 * accompanied this code. If applicable, add the following below the License
21 * Header, with the fields enclosed by brackets [] replaced by your own
22 * identifying information: "Portions Copyrighted [year]
23 * [name of copyright owner]"
24 *
25 * Contributor(s):
26 *
27 * If you wish your version of this file to be governed by only the CDDL or
28 * only the GPL Version 2, indicate your decision by adding "[Contributor]
29 * elects to include this software in this distribution under the [CDDL or GPL
30 * Version 2] license." If you don't indicate a single choice of license, a
31 * recipient has the option to distribute your version of this file under
32 * either the CDDL, the GPL Version 2 or to extend the choice of license to
33 * its licensees as provided above. However, if you add GPL Version 2 code
34 * and therefore, elected the GPL Version 2 license, then the option applies
35 * only if the new code is made subject to such option by the copyright
36 * holder.
37 */
38
39 package javax.servlet.jsp.tagext;
40
41 import java.io.Reader;
42 import java.io.Writer;
43 import java.io.IOException;
44 import javax.servlet.jsp;
45
46 /**
47 * An encapsulation of the evaluation of the body of an action so it is
48 * available to a tag handler. BodyContent is a subclass of JspWriter.
49 *
50 * <p>
51 * Note that the content of BodyContent is the result of evaluation, so
52 * it will not contain actions and the like, but the result of their
53 * invocation.
54 *
55 * <p>
56 * BodyContent has methods to convert its contents into
57 * a String, to read its contents, and to clear the contents.
58 *
59 * <p>
60 * The buffer size of a BodyContent object is unbounded. A
61 * BodyContent object cannot be in autoFlush mode. It is not possible to
62 * invoke flush on a BodyContent object, as there is no backing stream.
63 *
64 * <p>
65 * Instances of BodyContent are created by invoking the pushBody and
66 * popBody methods of the PageContext class. A BodyContent is enclosed
67 * within another JspWriter (maybe another BodyContent object) following
68 * the structure of their associated actions.
69 *
70 * <p>
71 * A BodyContent is made available to a BodyTag through a setBodyContent()
72 * call. The tag handler can use the object until after the call to
73 * doEndTag().
74 */
75
76 public abstract class BodyContent extends JspWriter {
77
78 /**
79 * Protected constructor.
80 *
81 * Unbounded buffer, no autoflushing.
82 *
83 * @param e the enclosing JspWriter
84 */
85
86 protected BodyContent(JspWriter e) {
87 super(UNBOUNDED_BUFFER , false);
88 this.enclosingWriter = e;
89 }
90
91 /**
92 * Redefined flush() so it is not legal.
93 *
94 * <p>
95 * It is not valid to flush a BodyContent because there is no backing
96 * stream behind it.
97 *
98 * @throws IOException always thrown
99 */
100
101 public void flush() throws IOException {
102 throw new IOException("Illegal to flush within a custom tag");
103 }
104
105 /**
106 * Clear the body without throwing any exceptions.
107 */
108
109 public void clearBody() {
110 try {
111 this.clear();
112 } catch (IOException ex) {
113 // TODO -- clean this one up.
114 throw new Error("internal error!;");
115 }
116 }
117
118 /**
119 * Return the value of this BodyContent as a Reader.
120 *
121 * @return the value of this BodyContent as a Reader
122 */
123 public abstract Reader getReader();
124
125
126 /**
127 * Return the value of the BodyContent as a String.
128 *
129 * @return the value of the BodyContent as a String
130 */
131 public abstract String getString();
132
133
134 /**
135 * Write the contents of this BodyContent into a Writer.
136 * Subclasses may optimize common invocation patterns.
137 *
138 * @param out The writer into which to place the contents of
139 * this body evaluation
140 * @throws IOException if an I/O error occurred while writing the
141 * contents of this BodyContent to the given Writer
142 */
143
144 public abstract void writeOut(Writer out) throws IOException;
145
146
147 /**
148 * Get the enclosing JspWriter.
149 *
150 * @return the enclosing JspWriter passed at construction time
151 */
152
153 public JspWriter getEnclosingWriter() {
154 return enclosingWriter;
155 }
156
157
158 // private fields
159
160 private JspWriter enclosingWriter;
161 }