KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.TagInstance;
34 import com.caucho.util.CharBuffer;
35
36 /**
37  * Represents the body for a fragment (jsp:attribute or jsp:body)
38  */

39 abstract public class JspFragmentNode extends JspContainerNode
40   implements JspSegmentNode
41 {
42   private int _fragmentCode;
43   private String JavaDoc _fragmentName;
44
45   private boolean _isValueFragment;
46   private boolean _isJspFragment;
47
48   public JspFragmentNode()
49   {
50   }
51   
52   /**
53    * Called after all the attributes from the tag.
54    */

55   public void endAttributes()
56     throws JspParseException
57   {
58     _fragmentCode = _gen.uniqueId();
59     
60     _fragmentName = "_jsp_fragment_" + _fragmentCode;
61   }
62
63   /**
64    * Returns the fragment name.
65    */

66   public String JavaDoc getFragmentName()
67   {
68     return _fragmentName;
69   }
70
71   /**
72    * Returns the tag name for the current tag.
73    */

74   public String JavaDoc getCustomTagName()
75   {
76     return "_jsp_parent_tag";
77   }
78   
79   /**
80    * Adds a text node.
81    */

82   public JspNode addText(String JavaDoc text)
83     throws JspParseException
84   {
85     JspNode node = new StaticText(_gen, text, this);
86     
87     addChild(node);
88
89     return node;
90   }
91
92   /**
93    * Returns true if trimming is enabled.
94    */

95   public boolean isTrim()
96   {
97     return false;
98   }
99
100   /**
101    * Returns true if the children are static.
102    */

103   public boolean isStatic()
104   {
105     if (_children == null)
106       return true;
107     
108     for (int i = 0; i < _children.size(); i++) {
109       if (! _children.get(i).isStatic())
110     return false;
111     }
112
113     return true;
114   }
115   
116   /**
117    * Returns true if the children are static.
118    */

119   public boolean isValueFragment()
120   {
121     return _isValueFragment;
122   }
123   
124   /**
125    * Set true if the fragment is used as a fragment object.
126    */

127   public void setJspFragment(boolean isFragment)
128   {
129     _isJspFragment = isFragment;
130   }
131   
132   /**
133    * Set true if the fragment is used as a fragment object.
134    */

135   public boolean isJspFragment()
136   {
137     return _isJspFragment;
138   }
139
140   /**
141    * Generates code for the fragment variables.
142    */

143   public void generateFragmentPrologue(JspJavaWriter out)
144     throws Exception JavaDoc
145   {
146     if (_isValueFragment)
147       return;
148
149     _isJspFragment = true;
150
151     if (isStatic())
152       out.println("com.caucho.jsp.StaticJspFragmentSupport " + _fragmentName + " = null;");
153     else
154       out.println("_CauchoFragment " + _fragmentName + " = null;");
155   }
156
157   /**
158    * Generates the children.
159    */

160   public void generate(JspJavaWriter out)
161     throws Exception JavaDoc
162   {
163     if (hasScripting() && isJspFragment())
164       throw error(L.l("Fragments may not contain scripting elements"));
165     
166     generateChildren(out);
167   }
168
169   /**
170    * Generates the code for a fragment.
171    */

172   protected String JavaDoc generateValue()
173     throws Exception JavaDoc
174   {
175     if (isStatic())
176       return '"' + escapeJavaString(getStaticText()) + '"';
177     
178     _isValueFragment = true;
179
180     if (hasScripting() && isJspFragment())
181       throw error(L.l("Fragments may not contain scripting elements"));
182
183     _gen.addFragment(this);
184     
185     TagInstance parent = getParent().getTag();
186
187     CharBuffer cb = new CharBuffer();
188
189     cb.append("_CauchoFragment." + _fragmentName + "(pageContext, ");
190
191     for (;
192      parent != null && parent.isTagFileTag();
193      parent = parent.getParent()) {
194     }
195
196     if (parent == null || parent.getId() == TagInstance.TOP_TAG)
197       cb.append("null");
198     else if (parent.getId().startsWith("top_"))
199       cb.append("_jsp_parent_tag");
200     else if (! hasCustomTag())
201       cb.append(parent.getId());
202     else if (parent.isSimpleTag())
203       cb.append(parent.getId() + "_adapter");
204     else
205       cb.append(parent.getId());
206
207     if (_gen instanceof JavaTagGenerator)
208       cb.append(", _jspBody");
209     else
210       cb.append(", null");
211       
212     cb.append(")");
213
214     return cb.close();
215   }
216
217   /**
218    * Generates the code for the fragment method.
219    */

220   void generateValueMethod(JspJavaWriter out)
221     throws Exception JavaDoc
222   {
223     if (hasScripting() && isJspFragment())
224       throw error(L.l("Fragments may not contain scripting elements"));
225     
226     out.println();
227     out.println("static String " + _fragmentName + "(");
228     out.println(" com.caucho.jsp.PageContextImpl pageContext,");
229     out.println(" javax.servlet.jsp.tagext.JspTag _jsp_parent_tag,");
230     out.println(" javax.servlet.jsp.tagext.JspFragment _jspBody)");
231     out.println(" throws Throwable");
232     out.println("{");
233     out.pushDepth();
234
235     out.println("JspWriter out = pageContext.pushBody();");
236     out.println("javax.el.ELContext _jsp_env = pageContext.getELContext();");
237
238     out.println("try {");
239     out.pushDepth();
240
241     generatePrologue(out);
242       
243     generate(out);
244
245     out.print("return ((com.caucho.jsp.BodyContentImpl) out)");
246     /*
247     if (isTrim())
248       out.println(".getTrimString();");
249     else
250       out.println(".getString();");
251     */

252     // jsp/18do
253
out.println(".getString();");
254
255     out.popDepth();
256     out.println("} finally {");
257     out.pushDepth();
258     out.println("pageContext.popAndReleaseBody();");
259     out.popDepth();
260     out.println("}");
261       
262     out.popDepth();
263     out.println("}");
264   }
265 }
266
Popular Tags