001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.el.Expr;
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.jsp.el.JspELParser;
034: import com.caucho.vfs.WriteStream;
035: import com.caucho.xml.QName;
036:
037: import java.io.IOException;
038:
039: public class JstlCoreOut extends JstlNode {
040: private static final QName VALUE = new QName("value");
041: private static final QName ESCAPE_XML = new QName("escapeXml");
042: private static final QName DEFAULT = new QName("default");
043:
044: private String _value;
045: private String _escapeXml = "true";
046: private String _default;
047:
048: private JspAttribute _valueAttr;
049: private JspAttribute _escapeXmlAttr;
050: private JspAttribute _defaultAttr;
051:
052: /**
053: * Adds an attribute.
054: */
055: public void addAttribute(QName name, String value)
056: throws JspParseException {
057: if (VALUE.equals(name))
058: _value = value;
059: else if (ESCAPE_XML.equals(name))
060: _escapeXml = value;
061: else if (DEFAULT.equals(name))
062: _default = value;
063: else
064: throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
065: name.getName(), getTagName()));
066: }
067:
068: /**
069: * Adds an attribute.
070: */
071: public void addAttribute(QName name, JspAttribute value)
072: throws JspParseException {
073: if (VALUE.equals(name))
074: _valueAttr = value;
075: else if (ESCAPE_XML.equals(name))
076: _escapeXmlAttr = value;
077: else if (DEFAULT.equals(name))
078: _defaultAttr = value;
079: else
080: throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
081: name.getName(), getTagName()));
082: }
083:
084: /**
085: * Returns true if the tag has scripting values.
086: */
087: @Override
088: public boolean hasScripting() {
089: return (super .hasScripting() || hasScripting(_value)
090: || hasScripting(_valueAttr) || hasScripting(_escapeXml)
091: || hasScripting(_escapeXmlAttr)
092: || hasScripting(_default) || hasScripting(_defaultAttr));
093: }
094:
095: /**
096: * Generates the XML text representation for the tag validation.
097: *
098: * @param os write stream to the generated XML.
099: */
100: public void printXml(WriteStream os) throws IOException {
101: String prefix = getNamespacePrefix("http://java.sun.com/jsp/jstl/core");
102:
103: if (prefix == null) {
104: prefix = "c";
105: os
106: .print("<c:out xmlns:c='http://java.sun.com/jsp/jstl/core'");
107: } else
108: os.print("<" + prefix + ":out");
109:
110: if (_value != null) {
111: os.print(" value=\"");
112: printXmlText(os, _value);
113: os.print("\"");
114: }
115: os.print(">");
116:
117: printXmlChildren(os);
118:
119: os.print("</" + prefix + ":out>");
120: }
121:
122: /**
123: * Generates the code for the c:out tag.
124: */
125: public void generate(JspJavaWriter out) throws Exception {
126: if (_value == null && _valueAttr == null)
127: throw error(L.l(
128: "required attribute `value' missing from <{0}>",
129: getTagName()));
130:
131: String escapeXml = "true";
132:
133: if (_escapeXml != null)
134: escapeXml = generateValue(boolean.class, _escapeXml);
135: else if (_escapeXmlAttr != null)
136: escapeXml = _escapeXmlAttr.generateValue(String.class);
137:
138: if (_escapeXml != null
139: && (_default != null || _defaultAttr != null)) {
140: String var = "_jsp_var_" + _gen.uniqueId();
141:
142: out.println("boolean " + var + " = " + escapeXml + ";");
143: escapeXml = var;
144: }
145:
146: if (_default != null || _defaultAttr != null || hasChildren())
147: out.print("if (");
148:
149: if (_valueAttr != null) {
150: out.print("com.caucho.el.Expr.toStream(out, ");
151: out.print(_valueAttr.generateValue(String.class));
152: out.print(", " + escapeXml + ")");
153: } else if (hasRuntimeAttribute(_value)) {
154: out.print("com.caucho.el.Expr.toStream(out, ");
155: out.print(generateRTValue(String.class, _value));
156: out.print(", " + escapeXml + ")");
157: } else {
158: int index = _gen.addExpr(_value);
159:
160: out.print("_caucho_expr_" + index
161: + ".print(out, _jsp_env, " + escapeXml + ")");
162: }
163:
164: if (_default != null || _defaultAttr != null) {
165: out.println(") {");
166: out.pushDepth();
167:
168: if (_defaultAttr != null) {
169: out.print("com.caucho.el.Expr.toStream(out, ");
170: out.print(_defaultAttr.generateValue(String.class));
171: out.println(", " + escapeXml + ");");
172: } else if (hasRuntimeAttribute(_default)) {
173: out.print("com.caucho.el.Expr.toStream(out, ");
174: out.print(generateRTValue(String.class, _default));
175: out.println(", " + escapeXml + ");");
176: } else {
177: Expr defaultExpr = new JspELParser(_gen.getELContext(),
178: _default).parse();
179:
180: if (defaultExpr.isConstant()
181: && escapeXml.equals("true")) {
182: String string = defaultExpr.evalString(null);
183:
184: if (string != null && !string.equals("")) {
185: out
186: .print("com.caucho.el.Expr.toStreamEscaped(out, \"");
187: out.printJavaString(string);
188: out.println("\");");
189: }
190: } else if (defaultExpr.isConstant()
191: && escapeXml.equals("false")) {
192: String string = defaultExpr.evalString(null);
193:
194: if (string != null && !string.equals("")) {
195: out.addText(string);
196: }
197: } else {
198: int defaultIndex = _gen.addExpr(_default);
199: out.println("_caucho_expr_" + defaultIndex
200: + ".print(out, _jsp_env, " + escapeXml
201: + ");");
202: }
203: }
204:
205: out.popDepth();
206: out.println("}");
207: } else if (!hasChildren()) {
208: out.println(";");
209: } else if (isChildrenStatic()) {
210: out.println(") {");
211: out.pushDepth();
212:
213: out.print("com.caucho.el.Expr.toStream(out, ");
214: out.print("\"" + escapeJavaString(getStaticText().trim())
215: + "\"");
216: out.println(", " + escapeXml + ");");
217:
218: out.popDepth();
219: out.println("}");
220: } else {
221: out.println(") {");
222: out.pushDepth();
223:
224: out.println("out = pageContext.pushBody();");
225:
226: generateChildren(out);
227:
228: out
229: .println("pageContext.printBody((com.caucho.jsp.BodyContentImpl) out, "
230: + escapeXml + ");");
231:
232: out.println("out = pageContext.popAndReleaseBody();");
233:
234: out.popDepth();
235: out.println("}");
236: }
237: }
238: }
|