KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JstlCoreOut


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.jsp.el.JspELParser;
34 import com.caucho.vfs.WriteStream;
35 import com.caucho.xml.QName;
36
37 import java.io.IOException JavaDoc;
38
39 public class JstlCoreOut extends JstlNode {
40   private static final QName VALUE = new QName("value");
41   private static final QName ESCAPE_XML = new QName("escapeXml");
42   private static final QName DEFAULT = new QName("default");
43   
44   private String JavaDoc _value;
45   private String JavaDoc _escapeXml = "true";
46   private String JavaDoc _default;
47   
48   private JspAttribute _valueAttr;
49   private JspAttribute _escapeXmlAttr;
50   private JspAttribute _defaultAttr;
51   
52   /**
53    * Adds an attribute.
54    */

55   public void addAttribute(QName name, String JavaDoc value)
56     throws JspParseException
57   {
58     if (VALUE.equals(name))
59       _value = value;
60     else if (ESCAPE_XML.equals(name))
61       _escapeXml = value;
62     else if (DEFAULT.equals(name))
63       _default = value;
64     else
65       throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
66                       name.getName(), getTagName()));
67   }
68   
69   /**
70    * Adds an attribute.
71    */

72   public void addAttribute(QName name, JspAttribute value)
73     throws JspParseException
74   {
75     if (VALUE.equals(name))
76       _valueAttr = value;
77     else if (ESCAPE_XML.equals(name))
78       _escapeXmlAttr = value;
79     else if (DEFAULT.equals(name))
80       _defaultAttr = value;
81     else
82       throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
83                       name.getName(), getTagName()));
84   }
85
86   /**
87    * Generates the XML text representation for the tag validation.
88    *
89    * @param os write stream to the generated XML.
90    */

91   public void printXml(WriteStream os)
92     throws IOException JavaDoc
93   {
94     os.print("<c:out");
95
96     if (_value != null) {
97       os.print(" value=\"");
98       printXmlText(os, _value);
99       os.print("\"");
100     }
101     os.print(">");
102
103     printXmlChildren(os);
104
105     os.print("</c:out>");
106   }
107   
108   /**
109    * Generates the code for the c:out tag.
110    */

111   public void generate(JspJavaWriter out)
112     throws Exception JavaDoc
113   {
114     if (_value == null && _valueAttr == null)
115       throw error(L.l("required attribute `value' missing from <{0}>",
116                       getTagName()));
117
118     String JavaDoc escapeXml = "true";
119
120     if (_escapeXml != null)
121       escapeXml = generateValue(boolean.class, _escapeXml);
122     else if (_escapeXmlAttr != null)
123       escapeXml = _escapeXmlAttr.generateValue(String JavaDoc.class);
124
125     if (_escapeXml != null && (_default != null || _defaultAttr != null)) {
126       String JavaDoc var = "_jsp_var_" + _gen.uniqueId();
127       
128       out.println("boolean " + var + " = " + escapeXml + ";");
129       escapeXml = var;
130     }
131
132     if (_default != null || _defaultAttr != null || hasChildren())
133       out.print("if (");
134
135     if (_valueAttr != null) {
136       out.print("com.caucho.el.Expr.toStream(out, ");
137       out.print(_valueAttr.generateValue(String JavaDoc.class));
138       out.print(", " + escapeXml + ")");
139     }
140     else if (hasRuntimeAttribute(_value)) {
141       out.print("com.caucho.el.Expr.toStream(out, ");
142       out.print(generateRTValue(String JavaDoc.class, _value));
143       out.print(", " + escapeXml + ")");
144     }
145     else {
146       int index = _gen.addExpr(_value);
147     
148       out.print("_caucho_expr_" + index +
149         ".print(out, _jsp_env, " + escapeXml + ")");
150     }
151     
152     if (_default != null || _defaultAttr != null) {
153       out.println(") {");
154       out.pushDepth();
155
156       if (_defaultAttr != null) {
157     out.print("com.caucho.el.Expr.toStream(out, ");
158     out.print(_defaultAttr.generateValue(String JavaDoc.class));
159     out.println(", " + escapeXml + ");");
160       }
161       else if (hasRuntimeAttribute(_default)) {
162     out.print("com.caucho.el.Expr.toStream(out, ");
163     out.print(generateRTValue(String JavaDoc.class, _default));
164     out.println(", " + escapeXml + ");");
165       }
166       else {
167     Expr defaultExpr = new JspELParser(_gen.getELContext(),
168                        _default).parse();
169
170     if (defaultExpr.isConstant() && escapeXml.equals("true")) {
171       String JavaDoc string = defaultExpr.evalString(null);
172
173       if (string != null && ! string.equals("")) {
174         out.print("com.caucho.el.Expr.toStreamEscaped(out, \"");
175         out.printJavaString(string);
176         out.println("\");");
177       }
178     }
179     else if (defaultExpr.isConstant() && escapeXml.equals("false")) {
180       String JavaDoc string = defaultExpr.evalString(null);
181
182       if (string != null && ! string.equals("")) {
183         out.addText(string);
184       }
185     }
186     else {
187       int defaultIndex = _gen.addExpr(defaultExpr);
188       out.println("_caucho_expr_" + defaultIndex +
189               ".print(out, _jsp_env, " + escapeXml + ");");
190     }
191       }
192       
193       out.popDepth();
194       out.println("}");
195     }
196     else if (! hasChildren()) {
197       out.println(";");
198     }
199     else if (isChildrenStatic()) {
200       out.println(") {");
201       out.pushDepth();
202       
203       out.print("com.caucho.el.Expr.toStream(out, ");
204       out.print("\"" + escapeJavaString(getStaticText().trim()) + "\"");
205       out.println(", " + escapeXml + ");");
206       
207       out.popDepth();
208       out.println("}");
209     }
210     else {
211       out.println(") {");
212       out.pushDepth();
213       
214       out.println("out = pageContext.pushBody();");
215
216       generateChildren(out);
217
218       out.println("pageContext.printBody((com.caucho.jsp.BodyContentImpl) out, " + escapeXml + ");");
219
220       out.println("out = pageContext.popAndReleaseBody();");
221
222       out.popDepth();
223       out.println("}");
224     }
225   }
226 }
227
Popular Tags