KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35
36 import java.io.IOException JavaDoc;
37
38 public class JstlCoreSet extends JstlNode {
39   private static final QName VALUE = new QName("value");
40   private static final QName VAR = new QName("var");
41   private static final QName SCOPE = new QName("scope");
42   private static final QName TARGET = new QName("target");
43   private static final QName PROPERTY = new QName("property");
44   
45   private String JavaDoc _value;
46   private JspAttribute _valueAttr;
47   
48   private String JavaDoc _var;
49   private String JavaDoc _scope;
50   
51   private String JavaDoc _target;
52   private JspAttribute _targetAttr;
53   
54   private String JavaDoc _property;
55   private JspAttribute _propertyAttr;
56   
57   /**
58    * Adds an attribute.
59    */

60   public void addAttribute(QName name, String JavaDoc value)
61     throws JspParseException
62   {
63     if (VALUE.equals(name))
64       _value = value;
65     else if (VAR.equals(name))
66       _var = value;
67     else if (SCOPE.equals(name))
68       _scope = value;
69     else if (TARGET.equals(name))
70       _target = value;
71     else if (PROPERTY.equals(name))
72       _property = value;
73     else
74       throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
75                       name.getName(), getTagName()));
76   }
77   
78   /**
79    * Adds an attribute.
80    */

81   public void addAttribute(QName name, JspAttribute value)
82     throws JspParseException
83   {
84     if (VALUE.equals(name))
85       _valueAttr = value;
86     else if (TARGET.equals(name))
87       _targetAttr = value;
88     else if (PROPERTY.equals(name))
89       _propertyAttr = value;
90     else
91       throw error(L.l("`{0}' is an unsupported jsp:attribute for <{1}>.",
92                       name.getName(), getTagName()));
93   }
94
95   /**
96    * Returns true if the tag has scripting values.
97    */

98   public boolean hasScripting()
99   {
100     return (super.hasScripting() ||
101         hasScripting(_value) || hasScripting(_valueAttr) ||
102         hasScripting(_target) || hasScripting(_targetAttr) ||
103         hasScripting(_property) || hasScripting(_propertyAttr));
104   }
105
106   /**
107    * Generates the XML text representation for the tag validation.
108    *
109    * @param os write stream to the generated XML.
110    */

111   public void printXml(WriteStream os)
112     throws IOException JavaDoc
113   {
114     os.print("<c:set");
115
116     if (_value != null) {
117       os.print(" value=\"");
118       printXmlText(os, _value);
119       os.print("\"");
120     }
121     
122     if (_var != null)
123       os.print(" var=\"" + _var + "\"");
124     
125     if (_scope != null)
126       os.print(" scope=\"" + _scope + "\"");
127     
128     if (_target != null)
129       os.print(" target=\"" + _target + "\"");
130     
131     if (_property != null)
132       os.print(" property=\"" + _property + "\"");
133
134     os.print(">");
135
136     printXmlChildren(os);
137
138     os.print("</c:set>");
139   }
140   
141   /**
142    * Generates the code for the c:set tag.
143    */

144   public void generate(JspJavaWriter out)
145     throws Exception JavaDoc
146   {
147     if (_value != null) {
148       String JavaDoc value = generateValue(Object JavaDoc.class, _value);
149       
150       generateSet(out, value);
151     }
152     else if (_valueAttr != null) {
153       generateSet(out, _valueAttr.generateValue());
154     }
155     else if (! hasChildren()) {
156       generateSet(out, "\"\"");
157     }
158     else if (isChildrenStatic()) {
159       generateSet(out, '"' + escapeJavaString(getStaticText().trim()) + '"');
160     }
161     else if (hasChildren()) {
162       out.println("out = pageContext.pushBody();");
163
164       generateChildren(out);
165
166       String JavaDoc cauchoVar = "_caucho_var_" + _gen.uniqueId();
167
168       out.println("Object " + cauchoVar + " = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
169
170       out.println("out = pageContext.popAndReleaseBody();");
171
172       generateSet(out, cauchoVar);
173     }
174   }
175
176   private void generateSet(JspJavaWriter out, String JavaDoc value)
177     throws Exception JavaDoc
178   {
179     if (_var != null)
180       generateSetOrRemove(out, _var, _scope, value);
181     else {
182       out.print("com.caucho.el.Expr.setProperty(");
183       if (_targetAttr != null)
184     out.print(_targetAttr.generateValue() + ", ");
185       else
186     out.print(generateValue(Object JavaDoc.class, _target) + ", ");
187
188       if (_propertyAttr != null)
189     out.print(_propertyAttr.generateValue() + ", ");
190       else
191     out.print(generateValue(String JavaDoc.class, _property) + ", ");
192       
193       out.println(value + ");");
194     }
195   }
196 }
197
Popular Tags