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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.QName;
035:
036: import java.io.IOException;
037:
038: public class JstlCoreSet extends JstlNode {
039: private static final QName VALUE = new QName("value");
040: private static final QName VAR = new QName("var");
041: private static final QName SCOPE = new QName("scope");
042: private static final QName TARGET = new QName("target");
043: private static final QName PROPERTY = new QName("property");
044:
045: private String _value;
046: private JspAttribute _valueAttr;
047:
048: private String _var;
049: private String _scope;
050:
051: private String _target;
052: private JspAttribute _targetAttr;
053:
054: private String _property;
055: private JspAttribute _propertyAttr;
056:
057: /**
058: * Adds an attribute.
059: */
060: public void addAttribute(QName name, String value)
061: throws JspParseException {
062: if (VALUE.equals(name))
063: _value = value;
064: else if (VAR.equals(name))
065: _var = value;
066: else if (SCOPE.equals(name))
067: _scope = value;
068: else if (TARGET.equals(name))
069: _target = value;
070: else if (PROPERTY.equals(name))
071: _property = value;
072: else
073: throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
074: name.getName(), getTagName()));
075: }
076:
077: /**
078: * Adds an attribute.
079: */
080: public void addAttribute(QName name, JspAttribute value)
081: throws JspParseException {
082: if (VALUE.equals(name))
083: _valueAttr = value;
084: else if (TARGET.equals(name))
085: _targetAttr = value;
086: else if (PROPERTY.equals(name))
087: _propertyAttr = value;
088: else
089: throw error(L.l(
090: "`{0}' is an unsupported jsp:attribute for <{1}>.",
091: name.getName(), getTagName()));
092: }
093:
094: /**
095: * Returns true if the tag has scripting values.
096: */
097: public boolean hasScripting() {
098: return (super .hasScripting() || hasScripting(_value)
099: || hasScripting(_valueAttr) || hasScripting(_target)
100: || hasScripting(_targetAttr) || hasScripting(_property) || hasScripting(_propertyAttr));
101: }
102:
103: /**
104: * Generates the XML text representation for the tag validation.
105: *
106: * @param os write stream to the generated XML.
107: */
108: public void printXml(WriteStream os) throws IOException {
109: os.print("<c:set");
110:
111: if (_value != null) {
112: os.print(" value=\"");
113: printXmlText(os, _value);
114: os.print("\"");
115: }
116:
117: if (_var != null)
118: os.print(" var=\"" + _var + "\"");
119:
120: if (_scope != null)
121: os.print(" scope=\"" + _scope + "\"");
122:
123: if (_target != null)
124: os.print(" target=\"" + _target + "\"");
125:
126: if (_property != null)
127: os.print(" property=\"" + _property + "\"");
128:
129: os.print(">");
130:
131: printXmlChildren(os);
132:
133: os.print("</c:set>");
134: }
135:
136: /**
137: * Generates the code for the c:set tag.
138: */
139: public void generate(JspJavaWriter out) throws Exception {
140: if (_value != null) {
141: String value = generateValue(Object.class, _value);
142:
143: generateSet(out, value);
144: } else if (_valueAttr != null) {
145: generateSet(out, _valueAttr.generateValue());
146: } else if (!hasChildren()) {
147: generateSet(out, "\"\"");
148: } else if (isChildrenStatic()) {
149: generateSet(out, '"' + escapeJavaString(getStaticText()
150: .trim()) + '"');
151: } else if (hasChildren()) {
152: out.println("out = pageContext.pushBody();");
153:
154: generateChildren(out);
155:
156: String cauchoVar = "_caucho_var_" + _gen.uniqueId();
157:
158: out
159: .println("Object "
160: + cauchoVar
161: + " = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
162:
163: out.println("out = pageContext.popAndReleaseBody();");
164:
165: generateSet(out, cauchoVar);
166: }
167: }
168:
169: private void generateSet(JspJavaWriter out, String value)
170: throws Exception {
171: if (_var != null)
172: generateSetOrRemove(out, _var, _scope, value);
173: else {
174: out.print("com.caucho.el.Expr.setProperty(");
175: if (_targetAttr != null)
176: out.print(_targetAttr.generateValue() + ", ");
177: else
178: out.print(generateValue(Object.class, _target) + ", ");
179:
180: if (_propertyAttr != null)
181: out.print(_propertyAttr.generateValue() + ", ");
182: else
183: out
184: .print(generateValue(String.class, _property)
185: + ", ");
186:
187: out.println(value + ");");
188: }
189: }
190: }
|