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.jsp.TagInstance;
034: import com.caucho.util.CharBuffer;
035:
036: /**
037: * Represents the body for a fragment (jsp:attribute or jsp:body)
038: */
039: abstract public class JspFragmentNode extends JspContainerNode
040: implements JspSegmentNode {
041: private int _fragmentCode;
042: private String _fragmentName;
043:
044: private boolean _isValueFragment;
045: private boolean _isJspFragment;
046:
047: public JspFragmentNode() {
048: }
049:
050: /**
051: * Called after all the attributes from the tag.
052: */
053: public void endAttributes() throws JspParseException {
054: _fragmentCode = _gen.uniqueId();
055:
056: _fragmentName = "_jsp_fragment_" + _fragmentCode;
057: }
058:
059: /**
060: * Returns the fragment name.
061: */
062: public String getFragmentName() {
063: return _fragmentName;
064: }
065:
066: /**
067: * Returns the tag name for the current tag.
068: */
069: public String getCustomTagName() {
070: return "_jsp_parent_tag";
071: }
072:
073: /**
074: * Adds a text node.
075: */
076: public JspNode addText(String text) throws JspParseException {
077: JspNode node = new StaticText(_gen, text, this );
078:
079: addChild(node);
080:
081: return node;
082: }
083:
084: /**
085: * Returns true if trimming is enabled.
086: */
087: public boolean isTrim() {
088: return false;
089: }
090:
091: /**
092: * Returns true if the children are static.
093: */
094: public boolean isStatic() {
095: if (_children == null)
096: return true;
097:
098: for (int i = 0; i < _children.size(); i++) {
099: if (!_children.get(i).isStatic())
100: return false;
101: }
102:
103: return true;
104: }
105:
106: /**
107: * Returns true if the children are static.
108: */
109: public boolean isValueFragment() {
110: return _isValueFragment;
111: }
112:
113: /**
114: * Set true if the fragment is used as a fragment object.
115: */
116: public void setJspFragment(boolean isFragment) {
117: _isJspFragment = isFragment;
118: }
119:
120: /**
121: * Set true if the fragment is used as a fragment object.
122: */
123: public boolean isJspFragment() {
124: return _isJspFragment;
125: }
126:
127: /**
128: * Generates code for the fragment variables.
129: */
130: public void generateFragmentPrologue(JspJavaWriter out)
131: throws Exception {
132: if (_isValueFragment)
133: return;
134:
135: _isJspFragment = true;
136:
137: if (isStatic())
138: out.println("com.caucho.jsp.StaticJspFragmentSupport "
139: + _fragmentName + " = null;");
140: else
141: out
142: .println("_CauchoFragment " + _fragmentName
143: + " = null;");
144: }
145:
146: /**
147: * Generates the children.
148: */
149: public void generate(JspJavaWriter out) throws Exception {
150: if (hasScriptingElement() && isJspFragment()) {
151: JspNode node = findScriptingNode();
152:
153: if (node != null)
154: throw node
155: .error(L
156: .l("Fragments may not contain scripting elements"));
157: else
158: throw error(L
159: .l("Fragments may not contain scripting elements"));
160: }
161:
162: generateChildren(out);
163: }
164:
165: /**
166: * Generates the code for a fragment.
167: */
168: protected String generateValue() throws Exception {
169: if (isStatic())
170: return '"' + escapeJavaString(getStaticText()) + '"';
171:
172: _isValueFragment = true;
173:
174: if (hasScriptingElement() && isJspFragment()) {
175: JspNode node = findScriptingNode();
176:
177: if (node != null)
178: throw node
179: .error(L
180: .l("Fragments may not contain scripting elements"));
181: else
182: throw error(L
183: .l("Fragments may not contain scripting elements"));
184: }
185:
186: _gen.addFragment(this );
187:
188: TagInstance parent = getParent().getTag();
189:
190: CharBuffer cb = new CharBuffer();
191:
192: cb
193: .append("_CauchoFragment." + _fragmentName
194: + "(pageContext, ");
195:
196: for (; parent != null && parent.isTagFileTag(); parent = parent
197: .getParent()) {
198: }
199:
200: if (parent == null || parent.getId() == TagInstance.TOP_TAG)
201: cb.append("null");
202: else if (parent.getId().startsWith("top_"))
203: cb.append("_jsp_parent_tag");
204: else if (!hasCustomTag())
205: cb.append(parent.getId());
206: else if (parent.isSimpleTag())
207: cb.append(parent.getId() + "_adapter");
208: else
209: cb.append(parent.getId());
210:
211: if (_gen instanceof JavaTagGenerator)
212: cb.append(", _jspBody");
213: else
214: cb.append(", null");
215:
216: cb.append(")");
217:
218: return cb.close();
219: }
220:
221: /**
222: * Generates the code for the fragment method.
223: */
224: void generateValueMethod(JspJavaWriter out) throws Exception {
225: if (hasScriptingElement() && isJspFragment()) {
226: JspNode node = findScriptingNode();
227:
228: if (node != null)
229: throw node
230: .error(L
231: .l("Fragments may not contain scripting elements"));
232: else
233: throw error(L
234: .l("Fragments may not contain scripting elements"));
235: }
236:
237: out.println();
238: out.println("static String " + _fragmentName + "(");
239: out.println(" com.caucho.jsp.PageContextImpl pageContext,");
240: out
241: .println(" javax.servlet.jsp.tagext.JspTag _jsp_parent_tag,");
242: out.println(" javax.servlet.jsp.tagext.JspFragment _jspBody)");
243: out.println(" throws Throwable");
244: out.println("{");
245: out.pushDepth();
246:
247: out.println("JspWriter out = pageContext.pushBody();");
248: out
249: .println("javax.el.ELContext _jsp_env = pageContext.getELContext();");
250:
251: if (hasScripting()) {
252: out
253: .println("javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest) pageContext.getRequest();");
254: out
255: .println("javax.servlet.http.HttpServletResponse response = (javax.servlet.http.HttpServletResponse) pageContext.getResponse();");
256: out
257: .println("javax.servlet.http.HttpSession session = pageContext.getSession();");
258: out
259: .println("javax.servlet.ServletContext application = pageContext.getServletContext();");
260: out
261: .println("javax.servlet.ServletConfig config = pageContext.getServletConfig();");
262: }
263:
264: out.println("try {");
265: out.pushDepth();
266:
267: generatePrologue(out);
268:
269: generate(out);
270:
271: out.print("return ((com.caucho.jsp.BodyContentImpl) out)");
272: /*
273: if (isTrim())
274: out.println(".getTrimString();");
275: else
276: out.println(".getString();");
277: */
278: // jsp/18do
279: out.println(".getString();");
280:
281: out.popDepth();
282: out.println("} finally {");
283: out.pushDepth();
284: out.println("pageContext.popAndReleaseBody();");
285: out.popDepth();
286: out.println("}");
287:
288: out.popDepth();
289: out.println("}");
290: }
291: }
|