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.jsp.JspParseException;
032: import com.caucho.jsp.TagInstance;
033: import com.caucho.vfs.WriteStream;
034:
035: import javax.servlet.jsp.tagext.VariableInfo;
036: import java.io.IOException;
037:
038: /**
039: * Represents the body for a SimpleTag
040: */
041: public class JspBody extends JspFragmentNode {
042: private TagInstance _tag;
043:
044: /**
045: * Adds a text node.
046: */
047: public JspNode addText(String text) throws JspParseException {
048: JspNode node = new StaticText(_gen, text, this );
049:
050: addChild(node);
051:
052: return node;
053: }
054:
055: /**
056: * Returns the tag name for the current tag.
057: */
058: public String getCustomTagName() {
059: if (isJspFragment())
060: return "_jsp_parent_tag";
061: else
062: return getParent().getCustomTagName();
063: }
064:
065: /**
066: * Returns true if the children are static.
067: */
068: public boolean isStatic() {
069: if (_children == null)
070: return true;
071:
072: for (int i = 0; i < _children.size(); i++) {
073: if (!_children.get(i).isStatic())
074: return false;
075: }
076:
077: return true;
078: }
079:
080: /**
081: * Returns the TagInstance of the enclosing parent.
082: */
083: public TagInstance getTag() {
084: JspNode parent = getParent();
085:
086: if (parent == null)
087: return _gen.getRootTag();
088: else if (parent instanceof CustomSimpleTag
089: || parent instanceof TagFileTag) {
090: if (_tag == null)
091: _tag = new TagInstance(_gen.getTagManager());
092:
093: return _tag;
094: } else
095: return parent.getTag();
096: }
097:
098: /**
099: * Called after all the attributes from the tag.
100: */
101: public void endAttributes() throws JspParseException {
102: super .endAttributes();
103:
104: JspNode parent = getParent();
105:
106: if (parent == null ||
107: // parent instanceof JspRoot ||
108: parent instanceof JspTop) {
109: throw error(L
110: .l("jsp:body must be contained in a valid tag."));
111: } else if (parent instanceof JspBody
112: || parent instanceof JspAttribute) {
113: throw error(L.l("jsp:body is not allowed in <{0}>", parent
114: .getTagName()));
115: }
116: }
117:
118: /**
119: * Generates the XML text representation for the tag validation.
120: *
121: * @param os write stream to the generated XML.
122: */
123: public void printXml(WriteStream os) throws IOException {
124: os.print("<jsp:body");
125: os.print(" jsp:id=\"" + _gen.generateJspId() + "\">");
126: printXmlChildren(os);
127: os.print("</jsp:body>");
128: }
129:
130: /**
131: * Generates the prologue.
132: */
133: public void generatePrologue(JspJavaWriter out) throws Exception {
134: JspNode parent = getParent();
135:
136: if (!isJspFragment()) {
137: generatePrologueChildren(out);
138: return;
139: }
140:
141: super .generatePrologue(out);
142:
143: TagInstance parentTag = getParent().getTag();
144: boolean isSimple = false;
145:
146: if (parentTag == null || parentTag.isTop()) {
147: } else if (!getTag().isTop()) {
148: } else if (parentTag.isSimpleTag()) {
149: getTag().setId(TagInstance.FRAGMENT_WITH_SIMPLE_PARENT);
150: } else
151: getTag().setId(TagInstance.FRAGMENT_WITH_TAG_PARENT);
152: }
153:
154: /**
155: * Generates the prologue as a child, i.e. in the fragment
156: * definition itself.
157: */
158: public void generatePrologueChildren(JspJavaWriter out)
159: throws Exception {
160: super .generatePrologueChildren(out);
161:
162: JspNode parent = getParent();
163:
164: if (parent instanceof GenericTag) {
165: GenericTag tag = (GenericTag) parent;
166:
167: tag.printVarDeclaration(out, VariableInfo.AT_BEGIN);
168: tag.printVarDeclaration(out, VariableInfo.NESTED);
169: }
170: }
171:
172: /**
173: * Direct generation of the body is forbidden.
174: */
175: /*
176: public void generate(JspJavaWriter out)
177: throws Exception
178: {
179: throw error(L.l("jsp:body must be contained in a valid tag."));
180: }
181: */
182: }
|