KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.JspParseException;
32 import com.caucho.jsp.TagInstance;
33 import com.caucho.vfs.WriteStream;
34
35 import javax.servlet.jsp.tagext.VariableInfo JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Represents the body for a SimpleTag
40  */

41 public class JspBody extends JspFragmentNode {
42   private TagInstance _tag;
43   
44   /**
45    * Adds a text node.
46    */

47   public JspNode addText(String JavaDoc text)
48     throws JspParseException
49   {
50     JspNode node = new StaticText(_gen, text, this);
51     
52     addChild(node);
53
54     return node;
55   }
56
57   /**
58    * Returns the tag name for the current tag.
59    */

60   public String JavaDoc getCustomTagName()
61   {
62     if (isJspFragment())
63       return "_jsp_parent_tag";
64     else
65       return getParent().getCustomTagName();
66   }
67
68   /**
69    * Returns true if the children are static.
70    */

71   public boolean isStatic()
72   {
73     if (_children == null)
74       return true;
75     
76     for (int i = 0; i < _children.size(); i++) {
77       if (! _children.get(i).isStatic())
78     return false;
79     }
80
81     return true;
82   }
83
84   /**
85    * Returns the TagInstance of the enclosing parent.
86    */

87   public TagInstance getTag()
88   {
89     JspNode parent = getParent();
90
91     if (parent == null)
92       return _gen.getRootTag();
93     else if (parent instanceof CustomSimpleTag ||
94          parent instanceof TagFileTag) {
95       if (_tag == null)
96     _tag = new TagInstance(_gen.getTagManager());
97
98       return _tag;
99     }
100     else
101       return parent.getTag();
102   }
103
104   /**
105    * Called after all the attributes from the tag.
106    */

107   public void endAttributes()
108     throws JspParseException
109   {
110     super.endAttributes();
111     
112     JspNode parent = getParent();
113
114     if (parent == null ||
115     // parent instanceof JspRoot ||
116
parent instanceof JspTop) {
117       throw error(L.l("jsp:body must be contained in a valid tag."));
118     }
119     else if (parent instanceof JspBody ||
120          parent instanceof JspAttribute) {
121       throw error(L.l("jsp:body is not allowed in <{0}>",
122               parent.getTagName()));
123     }
124   }
125
126   /**
127    * Generates the XML text representation for the tag validation.
128    *
129    * @param os write stream to the generated XML.
130    */

131   public void printXml(WriteStream os)
132     throws IOException JavaDoc
133   {
134     os.print("<jsp:body");
135     os.print(" jsp:id=\"" + _gen.generateJspId() + "\">");
136     printXmlChildren(os);
137     os.print("</jsp:body>");
138   }
139
140   /**
141    * Generates the prologue.
142    */

143   public void generatePrologue(JspJavaWriter out)
144     throws Exception JavaDoc
145   {
146     JspNode parent = getParent();
147     
148     if (! isJspFragment()) {
149       generatePrologueChildren(out);
150       return;
151     }
152     
153     super.generatePrologue(out);
154
155     TagInstance parentTag = getParent().getTag();
156     boolean isSimple = false;
157
158     if (parentTag == null || parentTag.isTop()) {
159     }
160     else if (! getTag().isTop()) {
161     }
162     else if (parentTag.isSimpleTag()) {
163       getTag().setId(TagInstance.FRAGMENT_WITH_SIMPLE_PARENT);
164     }
165     else
166       getTag().setId(TagInstance.FRAGMENT_WITH_TAG_PARENT);
167   }
168
169   /**
170    * Generates the prologue as a child, i.e. in the fragment
171    * definition itself.
172    */

173   public void generatePrologueChildren(JspJavaWriter out)
174     throws Exception JavaDoc
175   {
176     super.generatePrologueChildren(out);
177     
178     JspNode parent = getParent();
179
180     if (parent instanceof GenericTag) {
181       GenericTag tag = (GenericTag) parent;
182
183       tag.printVarDeclaration(out, VariableInfo.AT_BEGIN);
184       tag.printVarDeclaration(out, VariableInfo.NESTED);
185     }
186   }
187
188   /**
189    * Direct generation of the body is forbidden.
190    */

191   /*
192   public void generate(JspJavaWriter out)
193     throws Exception
194   {
195     throw error(L.l("jsp:body must be contained in a valid tag."));
196   }
197   */

198 }
199
Popular Tags