1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. */
  55. package javax.servlet.jsp.tagext;
  56. import java.io.Reader;
  57. import java.io.Writer;
  58. import java.io.IOException;
  59. import javax.servlet.jsp.*;
  60. /**
  61. * An encapsulation of the evaluation of the body of an action so it is
  62. * available to a tag handler. BodyContent is a subclass of JspWriter.
  63. *
  64. * <p>
  65. * Note that the content of BodyContent is the result of evaluation, so
  66. * it will not contain actions and the like, but the result of their
  67. * invocation.
  68. *
  69. * <p>
  70. * BodyContent has methods to convert its contents into
  71. * a String, to read its contents, and to clear the contents.
  72. *
  73. * <p>
  74. * The buffer size of a BodyContent object is unbounded. A
  75. * BodyContent object cannot be in autoFlush mode. It is not possible to
  76. * invoke flush on a BodyContent object, as there is no backing stream.
  77. *
  78. * <p>
  79. * Instances of BodyContent are created by invoking the pushBody and
  80. * popBody methods of the PageContext class. A BodyContent is enclosed
  81. * within another JspWriter (maybe another BodyContent object) following
  82. * the structure of their associated actions.
  83. *
  84. * <p>
  85. * A BodyContent is made available to a BodyTag through a setBodyContent()
  86. * call. The tag handler can use the object until after the call to
  87. * doEndTag().
  88. */
  89. public abstract class BodyContent extends JspWriter {
  90. /**
  91. * Protected constructor.
  92. *
  93. * Unbounded buffer, no autoflushing.
  94. */
  95. protected BodyContent(JspWriter e) {
  96. super(UNBOUNDED_BUFFER , false);
  97. this.enclosingWriter = e;
  98. }
  99. /**
  100. * Redefined flush() so it is not legal.
  101. *
  102. * <p>
  103. * It is not valid to flush a BodyContent because there is no backing
  104. * stream behind it.
  105. */
  106. public void flush() throws IOException {
  107. throw new IOException("Illegal to flush within a custom tag");
  108. }
  109. /**
  110. * Clear the body without throwing any exceptions.
  111. */
  112. public void clearBody() {
  113. try {
  114. this.clear();
  115. } catch (IOException ex) {
  116. // TODO -- clean this one up.
  117. throw new Error("internal error!;");
  118. }
  119. }
  120. /**
  121. * Return the value of this BodyContent as a Reader.
  122. *
  123. * @return the value of this BodyContent as a Reader
  124. */
  125. public abstract Reader getReader();
  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. * Write the contents of this BodyContent into a Writer.
  134. * Subclasses may optimize common invocation patterns.
  135. *
  136. * @param out The writer into which to place the contents of
  137. * this body evaluation
  138. */
  139. public abstract void writeOut(Writer out) throws IOException;
  140. /**
  141. * Get the enclosing JspWriter.
  142. *
  143. * @return the enclosing JspWriter passed at construction time
  144. */
  145. public JspWriter getEnclosingWriter() {
  146. return enclosingWriter;
  147. }
  148. /**
  149. * private fields
  150. */
  151. private JspWriter enclosingWriter;
  152. }