KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > OutSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.core;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 /**
28  * <p>Support for handlers of the &lt;out&gt; tag, which simply evalutes and
29  * prints the result of the expression it's passed. If the result is
30  * null, we print the value of the 'default' attribute's expression or
31  * our body (which two are mutually exclusive, although this constraint
32  * is enforced outside this handler, in our TagLibraryValidator).</p>
33  *
34  * @author Shawn Bayern
35  */

36 public class OutSupport extends BodyTagSupport JavaDoc {
37
38     /*
39      * (One almost wishes XML and JSP could support "anonymous tags,"
40      * given the amount of trouble we had naming this one!) :-) - sb
41      */

42
43     //*********************************************************************
44
// Internal state
45

46     protected Object JavaDoc value; // tag attribute
47
protected String JavaDoc def; // tag attribute
48
protected boolean escapeXml; // tag attribute
49
private boolean needBody; // non-space body needed?
50

51     //*********************************************************************
52
// Construction and initialization
53

54     /**
55      * Constructs a new handler. As with TagSupport, subclasses should
56      * not provide other constructors and are expected to call the
57      * superclass constructor.
58      */

59     public OutSupport() {
60         super();
61         init();
62     }
63
64     // resets local state
65
private void init() {
66         value = def = null;
67         escapeXml = true;
68     needBody = false;
69     }
70
71     // Releases any resources we may have (or inherit)
72
public void release() {
73         super.release();
74         init();
75     }
76
77
78     //*********************************************************************
79
// Tag logic
80

81     // evaluates 'value' and determines if the body should be evaluted
82
public int doStartTag() throws JspException JavaDoc {
83
84       needBody = false; // reset state related to 'default'
85
this.bodyContent = null; // clean-up body (just in case container is pooling tag handlers)
86

87       try {
88     // print value if available; otherwise, try 'default'
89
if (value != null) {
90             out(pageContext, escapeXml, value);
91         return SKIP_BODY;
92     } else {
93         // if we don't have a 'default' attribute, just go to the body
94
if (def == null) {
95         needBody = true;
96         return EVAL_BODY_BUFFERED;
97         }
98
99         // if we do have 'default', print it
100
if (def != null) {
101         // good 'default'
102
out(pageContext, escapeXml, def);
103         }
104         return SKIP_BODY;
105     }
106       } catch (IOException JavaDoc ex) {
107     throw new JspException JavaDoc(ex.toString(), ex);
108       }
109     }
110
111     // prints the body if necessary; reports errors
112
public int doEndTag() throws JspException JavaDoc {
113       try {
114     if (!needBody)
115         return EVAL_PAGE; // nothing more to do
116

117     // trim and print out the body
118
if (bodyContent != null && bodyContent.getString() != null)
119             out(pageContext, escapeXml, bodyContent.getString().trim());
120     return EVAL_PAGE;
121       } catch (IOException JavaDoc ex) {
122     throw new JspException JavaDoc(ex.toString(), ex);
123       }
124     }
125
126
127     //*********************************************************************
128
// Public utility methods
129

130     /**
131      * Outputs <tt>text</tt> to <tt>pageContext</tt>'s current JspWriter.
132      * If <tt>escapeXml</tt> is true, performs the following substring
133      * replacements (to facilitate output to XML/HTML pages):
134      *
135      * & -> &amp;
136      * < -> &lt;
137      * > -> &gt;
138      * " -> &#034;
139      * ' -> &#039;
140      *
141      * See also Util.escapeXml().
142      */

143     public static void out(PageContext JavaDoc pageContext,
144                            boolean escapeXml,
145                            Object JavaDoc obj) throws IOException JavaDoc {
146         JspWriter JavaDoc w = pageContext.getOut();
147     if (!escapeXml) {
148             // write chars as is
149
if (obj instanceof Reader JavaDoc) {
150                 Reader JavaDoc reader = (Reader JavaDoc)obj;
151                 char[] buf = new char[4096];
152                 int count;
153                 while ((count=reader.read(buf, 0, 4096)) != -1) {
154                     w.write(buf, 0, count);
155                 }
156             } else {
157                 w.write(obj.toString());
158             }
159         } else {
160             // escape XML chars
161
if (obj instanceof Reader JavaDoc) {
162                 Reader JavaDoc reader = (Reader JavaDoc)obj;
163                 char[] buf = new char[4096];
164                 int count;
165                 while ((count = reader.read(buf, 0, 4096)) != -1) {
166                     writeEscapedXml(buf, count, w);
167                 }
168             } else {
169                 String JavaDoc text = obj.toString();
170                 writeEscapedXml(text.toCharArray(), text.length(), w);
171             }
172         }
173     }
174
175    /**
176      *
177      * Optimized to create no extra objects and write directly
178      * to the JspWriter using blocks of escaped and unescaped characters
179      *
180      */

181     private static void writeEscapedXml(char[] buffer, int length, JspWriter JavaDoc w) throws IOException JavaDoc{
182         int start = 0;
183
184         for (int i = 0; i < length; i++) {
185             char c = buffer[i];
186             if (c <= Util.HIGHEST_SPECIAL) {
187                 char[] escaped = Util.specialCharactersRepresentation[c];
188                 if (escaped != null) {
189                     // add unescaped portion
190
if (start < i) {
191                         w.write(buffer,start,i-start);
192                     }
193                     // add escaped xml
194
w.write(escaped);
195                     start = i + 1;
196                 }
197             }
198         }
199         // add rest of unescaped portion
200
if (start < length) {
201             w.write(buffer,start,length-start);
202         }
203     }
204 }
205
Popular Tags